Skip to content

fix Collections::reduce and Collections::reduceRight behaviour #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2018

Conversation

MamdouhAhmed
Copy link
Contributor

@MamdouhAhmed MamdouhAhmed commented Aug 12, 2018

Hello, and thanks for contributing to php-lodash

Description

The changes I made to reduce and reduceRight makes them both behave like their JS counterparts that they were modeled after, the change is potentially breaking for any code that relied heavily on, or just worked with, the existing implementation.

In the documentation for Array.prototype.reduce and Array.prototype.reduceRight, it is mentioned that in case of calling any of those functions without including an initial value, the function uses the first element of the array to be processed as its initial value and starts iterating from the next element. That way it doesn't double-process any of the values of the array.

Type of change

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How Has This Been Tested?

Added new cases to the existing unit test to confirm that the changes have the intended effect. The snippet below shows concisely the effect of the changes.

$sum = __::reduce([2], function ($a, $b){return $a+$b;}); //before -> 4 -- processed the first element twice, after --> 2 
$sum = __::reduce([2], function ($a, $b){return $a+$b;}, 0); // before and after -> 2 -- an initial value was provided

Test Configuration:

  • PHP Version: >=7.0

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@codecov-io
Copy link

codecov-io commented Aug 12, 2018

Codecov Report

Merging #13 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #13   +/-   ##
=========================================
  Coverage     98.51%   98.51%           
  Complexity      224      224           
=========================================
  Files             8        8           
  Lines           538      538           
=========================================
  Hits            530      530           
  Misses            8        8
Impacted Files Coverage Δ Complexity Δ
src/Traits/Collections.php 98.75% <100%> (ø) 119 <0> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 393a5a9...6c65d3b. Read the comment docs.

@ziishaned ziishaned merged commit 00c8c28 into me-io:master Aug 13, 2018
@meabed
Copy link
Member

meabed commented Aug 14, 2018

Thank you @MamdouhAhmed 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants