Skip to content

Commit f433638

Browse files
committed
refactor: consolidate code
1 parent af25d12 commit f433638

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/Underscore.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,7 @@ protected function negate(callable $fn)
127127

128128
public function every(callable $fn)
129129
{
130-
foreach ($this->data as $index => $value) {
131-
if (!$fn($value, $index)) {
132-
return false;
133-
}
134-
}
135-
136-
return true;
130+
return $this->match($fn, true);
137131
}
138132

139133
public function all(callable $fn)
@@ -143,20 +137,29 @@ public function all(callable $fn)
143137

144138
public function some(callable $fn)
145139
{
146-
foreach ($this->data as $index => $value) {
147-
if ($fn($value, $index)) {
148-
return true;
149-
}
150-
}
151-
152-
return false;
140+
return $this->match($fn, false);
153141
}
154142

155143
public function any(callable $fn)
156144
{
157145
return $this->some($fn);
158146
}
159147

148+
protected function match(callable $fn, $all = true)
149+
{
150+
foreach ($this->data as $index => $value) {
151+
if ($all && !$fn($value, $index)) {
152+
return false;
153+
}
154+
155+
if (!$all && $fn($value, $index)) {
156+
return true;
157+
}
158+
}
159+
160+
return $all;
161+
}
162+
160163
public function contains($item)
161164
{
162165
return \in_array($item, $this->data);

0 commit comments

Comments
 (0)