Skip to content

Commit 3daba08

Browse files
committed
feat: add the _ methods
1 parent e61ba61 commit 3daba08

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

src/Underscore.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,170 @@ public function get($index = null)
3535
return $this->data[$index];
3636
}
3737

38+
public function each(callable $fn)
39+
{
40+
foreach ($this->data as $index => $value) {
41+
$fn($value, $index);
42+
}
43+
44+
return $this;
45+
}
46+
47+
public function forEach(callable $fn)
48+
{
49+
return $this->each($fn);
50+
}
51+
52+
public function map(callable $fn)
53+
{
54+
$data = [];
55+
56+
foreach ($this->data as $index => $value) {
57+
$data[$index] = $fn($value, $index);
58+
}
59+
60+
return new static($data);
61+
}
62+
63+
public function collect(callable $fn)
64+
{
65+
return $this->map($fn);
66+
}
67+
68+
public function reduce(callable $fn, $memo)
69+
{
70+
return array_reduce($this->data, $fn, $memo);
71+
}
72+
73+
public function foldl(callable $fn, $memo)
74+
{
75+
return $this->reduce($fn, $memo);
76+
}
77+
78+
public function inject(callable $fn, $memo)
79+
{
80+
return $this->reduce($fn, $memo);
81+
}
82+
83+
public function reduceRight(callable $fn, $memo)
84+
{
85+
// fix!
86+
return array_reduce($this->data, $fn, $memo);
87+
}
88+
89+
public function foldr(callable $fn, $memo)
90+
{
91+
return $this->reduceRight($fn, $memo);
92+
}
93+
94+
public function find(callabe $fn)
95+
{
96+
foreach ($this->data as $index => $value) {
97+
if ($fn($value, $key)) {
98+
return $value;
99+
}
100+
}
101+
}
102+
103+
public function detect(callabe $fn)
104+
{
105+
return $this->find($fn);
106+
}
107+
108+
public function filter(callable $fn)
109+
{
110+
$data = [];
111+
112+
foreach ($this->data as $index => $value) {
113+
if ($fn($value, $index)) {
114+
$data[$index] = $value;
115+
}
116+
}
117+
118+
return new static($data);
119+
}
120+
121+
public function select(callable $fn)
122+
{
123+
return $this->filter($fn);
124+
}
125+
126+
public function reject(callable $fn)
127+
{
128+
$data = [];
129+
130+
foreach ($this->data as $index => $value) {
131+
if (!$fn($value, $index)) {
132+
$data[$index] = $value;
133+
}
134+
}
135+
136+
return new static($data);
137+
}
138+
139+
public function every(callable $fn)
140+
{
141+
foreach ($this->data as $index => $value) {
142+
if (!$fn($value, $index)) {
143+
return false;
144+
}
145+
}
146+
147+
return true;
148+
}
149+
150+
public function all(callable $fn)
151+
{
152+
return $this->every($fn);
153+
}
154+
155+
public function some(callable $fn)
156+
{
157+
foreach ($this->data as $index => $value) {
158+
if ($fn($value, $index)) {
159+
return true;
160+
}
161+
}
162+
163+
return false;
164+
}
165+
166+
public function any(callable $fn)
167+
{
168+
return $this->some($fn);
169+
}
170+
171+
public function contains($item)
172+
{
173+
return \in_array($item, $this->data);
174+
}
175+
176+
public function includes($item)
177+
{
178+
return $this->contains($item);
179+
}
180+
181+
public function include($item)
182+
{
183+
return $this->contains($item);
184+
}
185+
186+
public function invoke(callable $fn)
187+
{
188+
return \call_user_func_array($fn, $this->data);
189+
}
190+
191+
public function pluck($columnKey, $indexKey = null)
192+
{
193+
if (\function_exists('array_column')) {
194+
$data = \array_column($this->data, $columnKey, $indexKey);
195+
} else {
196+
$data = Helper::arrayColumn($this->data, $columnKey, $indexKey);
197+
}
198+
199+
return new static($data);
200+
}
201+
38202
/**
39203
* {@inheritdoc}
40204
*/

0 commit comments

Comments
 (0)