@@ -18,6 +18,10 @@ public function first($n = 1)
18
18
19
19
/**
20
20
* Alias of first().
21
+ *
22
+ * @param int $n
23
+ *
24
+ * @return array
21
25
*/
22
26
public function head ($ n = 1 )
23
27
{
@@ -26,6 +30,10 @@ public function head($n = 1)
26
30
27
31
/**
28
32
* Alias of first().
33
+ *
34
+ * @param int $n
35
+ *
36
+ * @return array
29
37
*/
30
38
public function take ($ n = 1 )
31
39
{
@@ -46,6 +54,10 @@ public function last($n = 1)
46
54
47
55
/**
48
56
* Alias of last().
57
+ *
58
+ * @param int $n
59
+ *
60
+ * @return array
49
61
*/
50
62
public function tail ($ n = 1 )
51
63
{
@@ -54,6 +66,10 @@ public function tail($n = 1)
54
66
55
67
/**
56
68
* Alias of last().
69
+ *
70
+ * @param int $n
71
+ *
72
+ * @return array
57
73
*/
58
74
public function drop ($ n = 1 )
59
75
{
@@ -126,6 +142,10 @@ public function unique($fn = null)
126
142
127
143
/**
128
144
* Alias of unique().
145
+ *
146
+ * @param callback|string $fn The callback. String is resolved to value of that index.
147
+ *
148
+ * @return self
129
149
*/
130
150
public function uniq ($ fn = null )
131
151
{
@@ -149,7 +169,11 @@ public function difference($data)
149
169
}
150
170
151
171
/**
152
- * Alias of without().
172
+ * Alias of difference().
173
+ *
174
+ * @param array|mixed $data Array or array like or array convertible.
175
+ *
176
+ * @return self
153
177
*/
154
178
public function without ($ data )
155
179
{
@@ -203,7 +227,7 @@ public function zip($data)
203
227
/**
204
228
* Hydrate the items into given class or stdClass.
205
229
*
206
- * @param string $className FQCN of the class whose constructor accepts two parameters: value and index.
230
+ * @param string|null $className FQCN of the class whose constructor accepts two parameters: value and index.
207
231
*
208
232
* @return self
209
233
*/
@@ -221,19 +245,19 @@ public function object($className = null)
221
245
*
222
246
* @return mixed|null
223
247
*/
224
- public function firstIndex ($ fn = null )
248
+ public function findIndex ($ fn = null )
225
249
{
226
250
return $ this ->find ($ this ->valueFn ($ fn ), false );
227
251
}
228
252
229
253
/**
230
- * Find the larst index that passes given truth test.
254
+ * Find the last index that passes given truth test.
231
255
*
232
256
* @param callable $fn The truth test callback.
233
257
*
234
258
* @return mixed|null
235
259
*/
236
- public function lastIndex ($ fn = null )
260
+ public function findLastIndex ($ fn = null )
237
261
{
238
262
return (new static (\array_reverse ($ this ->data , true )))->find ($ this ->valueFn ($ fn ), false );
239
263
}
@@ -262,6 +286,37 @@ public function lastIndexOf($value)
262
286
return (false === $ index = \array_search ($ value , \array_reverse ($ this ->data , true ))) ? null : $ index ;
263
287
}
264
288
289
+ /**
290
+ * Gets the smallest index at which an object should be inserted so as to maintain order.
291
+ *
292
+ * Note that the initial stack must be sorted already.
293
+ *
294
+ * @param $object The new object which needs to be adjusted in stack.
295
+ * @param callback|string $fn The comparator callback.
296
+ *
297
+ * @return string|int|null
298
+ */
299
+ public function sortedIndex ($ object , $ fn )
300
+ {
301
+ $ low = 0 ;
302
+ $ high = $ this ->count ();
303
+ $ data = $ this ->values ();
304
+ $ fn = $ this ->valueFn ($ fn );
305
+ $ value = $ fn ($ object );
306
+ $ keys = $ this ->keys ();
307
+
308
+ while ($ low < $ high ) {
309
+ $ mid = \intval (($ low + $ high ) / 2 );
310
+ if ($ fn ($ data [$ mid ]) < $ value ) {
311
+ $ low = $ mid + 1 ;
312
+ } else {
313
+ $ high = $ mid ;
314
+ }
315
+ }
316
+
317
+ return isset ($ keys [$ low ]) ? $ keys [$ low ] : null ;
318
+ }
319
+
265
320
/**
266
321
* Creates a new range from start to stop with given step.
267
322
*
0 commit comments