8000 [8.x] Pass $key to closure in Collection and LazyCollection's reduce method as well by mokhosh · Pull Request #35878 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[8.x] Pass $key to closure in Collection and LazyCollection's reduce method as well #35878

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 11 commits into from
Jan 15, 2021
Prev Previous commit
Next Next commit
add test for reduce with keys and reduceWithKeys
  • Loading branch information
mokhosh committed Jan 15, 2021
commit 11bace24071db06a5c8990a2cec49ef3e9d4df00
10 changes: 9 additions & 1 deletion tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3590,6 +3590,14 @@ public function testReduce($collection)
$this->assertEquals(6, $data->reduce(function ($carry, $element) {
return $carry += $element;
}));

$data = new $collection([
'foo' => 'bar',
'baz' => 'qux',
]);
$this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) {
return $carry .= $key.$element;
}));
}

/**
Expand All @@ -3601,7 +3609,7 @@ public function testReduceWithKeys($collection)
'foo' => 'bar',
'baz' => 'qux',
]);
$this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) {
$this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) {
return $carry .= $key.$element;
}));
}
Expand Down
0