@@ -44,11 +44,6 @@ public function each(callable $fn)
44
44
return $ this ;
45
45
}
46
46
47
- public function forEach (callable $ fn )
48
- {
49
- return $ this ->each ($ fn );
50
- }
51
-
52
47
public function map (callable $ fn )
53
48
{
54
49
$ data = [];
@@ -82,38 +77,31 @@ public function inject(callable $fn, $memo)
82
77
83
78
public function reduceRight (callable $ fn , $ memo )
84
79
{
85
- // fix!
86
- return array_reduce ($ this ->data , $ fn , $ memo );
80
+ return array_reduce (array_reverse ($ this ->data ), $ fn , $ memo );
87
81
}
88
82
89
83
public function foldr (callable $ fn , $ memo )
90
84
{
91
85
return $ this ->reduceRight ($ fn , $ memo );
92
86
}
93
87
94
- public function find (callabe $ fn )
88
+ public function find (callable $ fn )
95
89
{
96
90
foreach ($ this ->data as $ index => $ value ) {
97
- if ($ fn ($ value , $ key )) {
91
+ if ($ fn ($ value , $ index )) {
98
92
return $ value ;
99
93
}
100
94
}
101
95
}
102
96
103
- public function detect (callabe $ fn )
97
+ public function detect (callable $ fn )
104
98
{
105
99
return $ this ->find ($ fn );
106
100
}
107
101
108
102
public function filter (callable $ fn )
109
103
{
110
- $ data = [];
111
-
112
- foreach ($ this ->data as $ index => $ value ) {
113
- if ($ fn ($ value , $ index )) {
114
- $ data [$ index ] = $ value ;
115
- }
116
- }
104
+ $ data = array_filter ($ this ->data , $ fn , \ARRAY_FILTER_USE_BOTH );
117
105
118
106
return new static ($ data );
119
107
}
@@ -125,26 +113,21 @@ public function select(callable $fn)
125
113
126
114
public function reject (callable $ fn )
127
115
{
128
- $ data = [];
129
-
130
- foreach ($ this ->data as $ index => $ value ) {
131
- if (!$ fn ($ value , $ index )) {
132
- $ data [$ index ] = $ value ;
133
- }
134
- }
116
+ $ data = array_filter ($ this ->data , $ this ->negate ($ fn ), \ARRAY_FILTER_USE_BOTH );
135
117
136
118
return new static ($ data );
137
119
}
138
120
139
- public function every (callable $ fn )
121
+ protected function negate (callable $ fn )
140
122
{
141
- foreach ($ this ->data as $ index => $ value ) {
142
- if (!$ fn ($ value , $ index )) {
143
- return false ;
144
- }
145
- }
123
+ return function () use ($ fn ) {
124
+ return !call_user_func_array ($ fn , func_get_args ());
125
+ };
126
+ }
146
127
147
- return true ;
128
+ public function every (callable $ fn )
129
+ {
130
+ return $ this ->match ($ fn , true );
148
131
}
149
132
150
133
public function all (callable $ fn )
@@ -154,31 +137,35 @@ public function all(callable $fn)
154
137
155
138
public function some (callable $ fn )
156
139
{
157
- foreach ($ this ->data as $ index => $ value ) {
158
- if ($ fn ($ value , $ index )) {
159
- return true ;
160
- }
161
- }
162
-
163
- return false ;
140
+ return $ this ->match ($ fn , false );
164
141
}
165
142
166
143
public function any (callable $ fn )
167
144
{
168
145
return $ this ->some ($ fn );
169
146
}
170
147
171
- public function contains ( $ item )
148
+ protected function match ( callable $ fn , $ all = true )
172
149
{
173
- return \in_array ($ item , $ this ->data );
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 ;
174
161
}
175
162
176
- public function includes ($ item )
163
+ public function contains ($ item )
177
164
{
178
- return $ this ->contains ( $ item );
165
+ return \in_array ( $ item , $ this ->data );
179
166
}
180
167
181
- public function include ($ item )
168
+ public function includes ($ item )
182
169
{
183
170
return $ this ->contains ($ item );
184
171
}
@@ -190,15 +177,34 @@ public function invoke(callable $fn)
190
177
191
178
public function pluck ($ columnKey , $ indexKey = null )
192
179
{
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
- }
180
+ $ data = \array_column ($ this ->data , $ columnKey , $ indexKey );
198
181
199
182
return new static ($ data );
200
183
}
201
184
185
+ public function where (array $ props )
186
+ {
187
+ return $ this ->filter ($ this ->matcher ($ props ));
188
+ }
189
+
190
+ public function findWhere (array $ props )
191
+ {
192
+ return $ this ->find ($ this ->matcher ($ props ));
193
+ }
194
+
195
+ protected function matcher (array $ props )
196
+ {
197
+ return function ($ value , $ index ) use ($ props ) {
198
+ foreach ($ props as $ prop => $ criteria ) {
199
+ if (\array_column ([$ value ], $ prop ) != [$ criteria ]) {
200
+ return false ;
201
+ }
202
+ }
203
+
204
+ return true ;
205
+ };
206
+ }
207
+
202
208
/**
203
209
* {@inheritdoc}
204
210
*/
@@ -262,6 +268,11 @@ public function __toString()
262
268
{
263
269
return \json_encode ($ this ->data );
264
270
}
271
+
272
+ public static function _ ($ data )
273
+ {
274
+ return new static ($ data );
275
+ }
265
276
}
266
277
267
278
\class_alias ('Ahc\Underscore\Underscore ' , 'Ahc\Underscore ' );
0 commit comments