Skip to content

Commit 470f142

Browse files
committed
fix: acc to tests
1 parent c445810 commit 470f142

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/Underscore.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,15 @@ public function inject(callable $fn, $memo)
7777

7878
public function reduceRight(callable $fn, $memo)
7979
{
80-
// fix!
81-
return array_reduce($this->data, $fn, $memo);
80+
return array_reduce(array_reverse($this->data), $fn, $memo);
8281
}
8382

8483
public function foldr(callable $fn, $memo)
8584
{
8685
return $this->reduceRight($fn, $memo);
8786
}
8887

89-
public function find(callabe $fn)
88+
public function find(callable $fn)
9089
{
9190
foreach ($this->data as $index => $value) {
9291
if ($fn($value, $index)) {
@@ -95,20 +94,14 @@ public function find(callabe $fn)
9594
}
9695
}
9796

98-
public function detect(callabe $fn)
97+
public function detect(callable $fn)
9998
{
10099
return $this->find($fn);
101100
}
102101

103102
public function filter(callable $fn)
104103
{
105-
$data = [];
106-
107-
foreach ($this->data as $index => $value) {
108-
if ($fn($value, $index)) {
109-
$data[$index] = $value;
110-
}
111-
}
104+
$data = array_filter($this->data, $fn, \ARRAY_FILTER_USE_BOTH);
112105

113106
return new static($data);
114107
}
@@ -120,17 +113,18 @@ public function select(callable $fn)
120113

121114
public function reject(callable $fn)
122115
{
123-
$data = [];
124-
125-
foreach ($this->data as $index => $value) {
126-
if (!$fn($value, $index)) {
127-
$data[$index] = $value;
128-
}
129-
}
116+
$data = array_filter($this->data, $this->negate($fn), \ARRAY_FILTER_USE_BOTH);
130117

131118
return new static($data);
132119
}
133120

121+
protected function negate(callable $fn)
122+
{
123+
return function () use ($fn) {
124+
return !call_user_func_array($fn, func_get_args());
125+
};
126+
}
127+
134128
public function every(callable $fn)
135129
{
136130
foreach ($this->data as $index => $value) {
@@ -199,7 +193,7 @@ protected function matcher(array $props)
199193
{
200194
return function ($value, $index) use ($props) {
201195
foreach ($props as $prop => $criteria) {
202-
if ($value !== $criteria || $prop != $index) {
196+
if (\array_column([$value], $prop) != [$criteria]) {
203197
return false;
204198
}
205199
}

0 commit comments

Comments
 (0)