@tolki/arr 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,2469 @@
1
- # Temporary Holding Version
1
+ <p align="center"><img src="https://raw.githubusercontent.com/abetwothree/tolki/refs/heads/master/docs/vitepress/public/tolki-logo-short.svg" width="50%" alt="Laravel Tolki JS Logo"></p>
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ # Tolki JS Arr Package
4
+
5
+ This package provides array utility functions similar to Laravel's Arr class facade.
6
+
7
+ ## Documentation
8
+
9
+ The full documentation for the array utilities can be found at [https://tolki.abe.dev](https://tolki.abe.dev/arrays-objects/).
10
+
11
+ <!-- AUTO-GENERATED-DOCS:START -->
12
+
13
+ ## Array & Object Utilities Installation
14
+
15
+ Three packages cover Laravel's `Arr` class and `data_*` helpers: [`@tolki/arr`](https://www.npmjs.com/package/@tolki/arr) for arrays, [`@tolki/obj`](https://www.npmjs.com/package/@tolki/obj) for plain objects, and [`@tolki/data`](https://www.npmjs.com/package/@tolki/data), which picks between the two at runtime. You can install any of them via npm, yarn, or pnpm:
16
+
17
+ ```bash [@tolki/arr]
18
+ # npm
19
+ npm install @tolki/arr
20
+
21
+ # pnpm
22
+ pnpm add @tolki/arr
23
+
24
+ # yarn
25
+ yarn add @tolki/arr
26
+ ```
27
+
28
+ ### Choosing a package
29
+
30
+ The packages share almost all of their helpers, so the choice comes down to what your data looks like. `@tolki/data` depends on the other two, so installing it also installs them.
31
+
32
+ | Package | Works on | Function names | Pick it when |
33
+ | ---------------------------------------------------------- | ------------------------------------------------ | ----------------------------------- | -------------------------------------------------------------------- |
34
+ | [`@tolki/arr`](https://www.npmjs.com/package/@tolki/arr) | Arrays | `get`, `first`, `pluck` | Your data is always a list. |
35
+ | [`@tolki/obj`](https://www.npmjs.com/package/@tolki/obj) | Plain objects | `get`, `first`, `pluck` | Your data is always a keyed record. |
36
+ | [`@tolki/data`](https://www.npmjs.com/package/@tolki/data) | Arrays, plain objects and Maps, decided per call | `dataGet`, `dataFirst`, `dataPluck` | The shape is only known at runtime, as with parsed JSON or API data. |
37
+
38
+ `@tolki/arr` and `@tolki/obj` use the same names for the helpers they share, so switching between them usually means changing the import. A few exist in only one or two packages: `flattenDot` is only in `@tolki/obj`, and `count`, `search`, `before` and `after` are only in `@tolki/data`. `@tolki/data` prefixes its names with `data`, so they can sit next to either package without a clash. [Differences from Laravel](https://tolki.abe.dev/arrays-objects/differences.html) lists what the three packages leave out, rename or add.
39
+
40
+ ### How `@tolki/data` decides
41
+
42
+ Every `data*` helper looks at its first argument and forwards the call:
43
+
44
+ - An array goes to the matching `@tolki/arr` helper. So does anything else that is a list of values, such as a `Set` or a generator.
45
+ - A plain object or a `Map` goes to the matching `@tolki/obj` helper.
46
+
47
+ The return types follow the same split. `dataGet` is typed as the two packages' `get` types combined, so TypeScript answers with the array result for an array and the object result for an object.
48
+
49
+ ### Importing the Package
50
+
51
+ You can import individual functions for better tree-shaking or the entire package (not recommended). On the docs site, each tab below imports from its own package and checks one small call:
52
+
53
+ ```js [@tolki/arr]
54
+ import { get } from "@tolki/arr";
55
+
56
+ get([{ price: 100 }], "0.price");
57
+ // => 100
58
+ ```
59
+
60
+ Import only the functions you use. Importing the entire package with `import * as Arr from "@tolki/arr"` is not recommended: it can pull every function into your bundle, even the ones you never call, which makes the bundles larger. Importing individual functions lets your build ship only the code that runs.
61
+
62
+ However, if you are working on a backend project where bundle size is not a concern, importing the entire package can be more convenient.
63
+
64
+ ### How to read the utilities list
65
+
66
+ The [utilities list](https://tolki.abe.dev/arrays-objects/utilities-list.html) has one section per function. Each section has one tab per package that exports it, in the order `@tolki/arr`, `@tolki/obj`, `@tolki/data`. A function that only some packages have shows only those tabs.
67
+
68
+ On the docs site, the tabs follow your choice. Pick `@tolki/obj` once and every section on the page, and the install and import examples above, switch to it. Your pick is remembered the next time you visit.
69
+
70
+ ### More documentation
71
+
72
+ - [Dot Notation](https://tolki.abe.dev/arrays-objects/dot-notation.html) explains the `"user.address.city"` paths that `get`, `set`, `pluck` and others read, and where a `*` works.
73
+ - [PHP Semantics](https://tolki.abe.dev/arrays-objects/php-semantics.html) explains the PHP rules the packages keep: loose comparison, truthiness, key casting and the exceptions they throw.
74
+ - [Differences from Laravel](https://tolki.abe.dev/arrays-objects/differences.html) lists where the packages return a value instead of changing a reference, behave differently, leave a function out, rename it or add one.
75
+
76
+ ## Tolki Array & Object Utilities List
77
+
78
+ These utilities work on arrays with `@tolki/arr`, on plain objects with `@tolki/obj`, and on either one with `@tolki/data`. On the docs site, every section has a tab for each package that exports the function, and the page remembers the tab you pick, so the next visit starts on the same one.
79
+
80
+ ### Utilities List
81
+
82
+ [accessible](#accessible) [add](#add) [arrayable](#arrayable) [arrayItem](#arrayitem) [boolean](#boolean) [chunk](#chunk) [chunkBy](#chunkby) [chunkWhile](#chunkwhile) [collapse](#collapse) [combine](#combine) [contains](#contains) [containsStrict](#containsstrict) [crossJoin](#crossjoin) [diff](#diff) [diffAssoc](#diffassoc) [diffAssocUsing](#diffassocusing) [diffKeys](#diffkeys) [diffKeysUsing](#diffkeysusing) [diffUsing](#diffusing) [divide](#divide) [dot](#dot) [every](#every) [except](#except) [exceptValues](#exceptvalues) [exists](#exists) [filter](#filter) [first](#first) [flatten](#flatten) [flip](#flip) [float](#float) [forget](#forget) [from](#from) [get](#get) [has](#has) [hasAll](#hasall) [hasAny](#hasany) [integer](#integer) [intersect](#intersect) [intersectAssoc](#intersectassoc) [intersectAssocUsing](#intersectassocusing) [intersectByKeys](#intersectbykeys) [intersectUsing](#intersectusing) [join](#join) [keyBy](#keyby) [keys](#keys) [last](#last) [map](#map) [mapSpread](#mapspread) [mapWithKeys](#mapwithkeys) [only](#only) [onlyValues](#onlyvalues) [pad](#pad) [partition](#partition) [pluck](#pluck) [pop](#pop) [prepend](#prepend) [prependKeysWith](#prependkeyswith) [pull](#pull) [push](#push) [query](#query) [random](#random) [reject](#reject) [replace](#replace) [replaceRecursive](#replacerecursive) [reverse](#reverse) [select](#select) [set](#set) [shift](#shift) [shuffle](#shuffle) [skipUntil](#skipuntil) [skipWhile](#skipwhile) [slice](#slice) [sole](#sole) [some](#some) [sort](#sort) [sortDesc](#sortdesc) [sortRecursive](#sortrecursive) [sortRecursiveDesc](#sortrecursivedesc) [splice](#splice) [string](#string) [take](#take) [takeUntil](#takeuntil) [takeWhile](#takewhile) [toCssClasses](#tocssclasses) [toCssStyles](#tocssstyles) [undot](#undot) [union](#union) [unshift](#unshift) [values](#values) [where](#where) [whereNotNull](#wherenotnull) [wrap](#wrap)
83
+
84
+ ### Utilities Details
85
+
86
+ #### accessible
87
+
88
+ Determine whether the given value is array accessible: a list in `@tolki/arr`, a plain object or a `Map` in `@tolki/obj`.
89
+
90
+ Exported as `accessible` from `@tolki/arr` and `@tolki/obj`.
91
+
92
+ Laravel: [`Arr::accessible()`](https://laravel.com/docs/helpers#method-array-accessible)
93
+
94
+ ```js [@tolki/arr]
95
+ import { accessible } from "@tolki/arr";
96
+
97
+ accessible([1, 2]);
98
+ // => true
99
+
100
+ accessible({ a: 1, b: 2 });
101
+ // => false
102
+
103
+ accessible("abc");
104
+ // => false
105
+ ```
106
+
107
+ #### add
108
+
109
+ Add an element using "dot" notation if it doesn't exist. A key that is missing or set to `null` counts as not existing.
110
+
111
+ Exported as `add` from `@tolki/arr` and `@tolki/obj`, and as `dataAdd` from `@tolki/data`.
112
+
113
+ Laravel: [`Arr::add()`](https://laravel.com/docs/helpers#method-array-add)
114
+
115
+ ```js [@tolki/arr]
116
+ import { add } from "@tolki/arr";
117
+
118
+ add([["Desk"]], "0.1", 100);
119
+ // => [["Desk", 100]]
120
+
121
+ add([["Desk", 100]], "0.1", 200);
122
+ // => [["Desk", 100]]
123
+ ```
124
+
125
+ #### arrayable
126
+
127
+ Determine whether the given value is arrayable: a list in `@tolki/arr`, a plain object in `@tolki/obj`.
128
+
129
+ Exported as `arrayable` from `@tolki/arr` and as `objectifiable` from `@tolki/obj`.
130
+
131
+ ```js [@tolki/arr]
132
+ import { arrayable } from "@tolki/arr";
133
+
134
+ arrayable([1, 2]);
135
+ // => true
136
+
137
+ arrayable({ a: 1 });
138
+ // => false
139
+ ```
140
+
141
+ #### arrayItem
142
+
143
+ Get an item using "dot" notation, throwing an `InvalidArgumentException` unless it is a list (`@tolki/arr`) or a plain object (`@tolki/obj`). `dataItem` expects the kind that matches its first argument.
144
+
145
+ Exported as `arrayItem` from `@tolki/arr`, as `objectItem` from `@tolki/obj` and as `dataItem` from `@tolki/data`.
146
+
147
+ Laravel: [`Arr::array()`](https://laravel.com/docs/helpers#method-array-array)
148
+
149
+ ```js [@tolki/arr]
150
+ import { arrayItem } from "@tolki/arr";
151
+
152
+ arrayItem([{ tags: ["a", "b"] }], "0.tags");
153
+ // => ["a", "b"]
154
+
155
+ arrayItem([{ tags: "a" }], "0.tags");
156
+ // => throws InvalidArgumentException
157
+ ```
158
+
159
+ #### boolean
160
+
161
+ Get a boolean item using "dot" notation, throwing an `InvalidArgumentException` if the value is not a boolean.
162
+
163
+ Exported as `boolean` from `@tolki/arr` and `@tolki/obj`, and as `dataBoolean` from `@tolki/data`.
164
+
165
+ Laravel: [`Arr::boolean()`](https://laravel.com/docs/helpers#method-array-boolean)
166
+
167
+ ```js [@tolki/arr]
168
+ import { boolean } from "@tolki/arr";
169
+
170
+ boolean([{ available: true }], "0.available");
171
+ // => true
172
+
173
+ boolean([{ name: "Joe" }], "0.name");
174
+ // => throws InvalidArgumentException
175
+ ```
176
+
177
+ #### chunk
178
+
179
+ Break an array or object into chunks of the given size.
180
+
181
+ Exported as `chunk` from `@tolki/arr` and `@tolki/obj`, and as `dataChunk` from `@tolki/data`.
182
+
183
+ ```js [@tolki/arr]
184
+ import { chunk } from "@tolki/arr";
185
+
186
+ chunk([1, 2, 3, 4, 5], 2);
187
+ // => [[1, 2], [3, 4], [5]]
188
+ ```
189
+
190
+ #### chunkBy
191
+
192
+ Break into chunks by comparing adjacent values, using a key path or a callback.
193
+
194
+ Exported as `chunkBy` from `@tolki/arr` and `@tolki/obj`, and as `dataChunkBy` from `@tolki/data`.
195
+
196
+ ```js [@tolki/arr]
197
+ import { chunkBy } from "@tolki/arr";
198
+
199
+ chunkBy([1, 1, 2, 2, 1], (value) => value);
200
+ // => [[1, 1], [2, 2], [1]]
201
+
202
+ chunkBy([{ type: "a" }, { type: "a" }, { type: "b" }], "type");
203
+ // => [[{ type: "a" }, { type: "a" }], [{ type: "b" }]]
204
+ ```
205
+
206
+ #### chunkWhile
207
+
208
+ Break into chunks with a callback that decides whether the next value joins the current chunk. It receives the value, its key and the chunk built so far.
209
+
210
+ Exported as `chunkWhile` from `@tolki/arr` and `@tolki/obj`, and as `dataChunkWhile` from `@tolki/data`.
211
+
212
+ ```js [@tolki/arr]
213
+ import { chunkWhile } from "@tolki/arr";
214
+
215
+ chunkWhile(["A", "A", "B"], (value, index, chunk) => chunk.at(-1) === value);
216
+ // => [["A", "A"], ["B"]]
217
+ ```
218
+
219
+ #### collapse
220
+
221
+ Collapse an array of arrays into a single array, or an object of objects into a single object.
222
+
223
+ Exported as `collapse` from `@tolki/arr` and `@tolki/obj`, and as `dataCollapse` from `@tolki/data`.
224
+
225
+ Laravel: [`Arr::collapse()`](https://laravel.com/docs/helpers#method-array-collapse)
226
+
227
+ ```js [@tolki/arr]
228
+ import { collapse } from "@tolki/arr";
229
+
230
+ collapse([
231
+ [1, 2, 3],
232
+ [4, 5, 6],
233
+ [7, 8, 9],
234
+ ]);
235
+ // => [1, 2, 3, 4, 5, 6, 7, 8, 9]
236
+
237
+ collapse([{ a: 1, b: 2 }, { c: 3 }]);
238
+ // => { a: 1, b: 2, c: 3 }
239
+ ```
240
+
241
+ #### combine
242
+
243
+ Combine two sets: the values of the first become the keys and the values of the second become the values. Both must hold the same number of items.
244
+
245
+ Exported as `combine` from `@tolki/arr` and `@tolki/obj`, and as `dataCombine` from `@tolki/data`.
246
+
247
+ ```js [@tolki/arr]
248
+ import { combine } from "@tolki/arr";
249
+
250
+ combine(["name", "price"], ["Desk", 100]);
251
+ // => { name: "Desk", price: 100 }
252
+ ```
253
+
254
+ #### contains
255
+
256
+ Determine if there is a given value, a matching item or a matching key path. The comparison is loose, so `"1"` matches `1`; see [PHP Semantics](https://tolki.abe.dev/arrays-objects/php-semantics.html#loose-and-strict-comparison).
257
+
258
+ Exported as `contains` from `@tolki/arr` and `@tolki/obj`, and as `dataContains` from `@tolki/data`.
259
+
260
+ ```js [@tolki/arr]
261
+ import { contains } from "@tolki/arr";
262
+
263
+ contains([1, 2, 3], 2);
264
+ // => true
265
+
266
+ contains(["1"], 1);
267
+ // => true
268
+
269
+ contains([1, 2, 3], (value) => value > 2);
270
+ // => true
271
+
272
+ contains([{ age: 30 }], "age", ">", 25);
273
+ // => true
274
+ ```
275
+
276
+ #### containsStrict
277
+
278
+ Determine if there is a given value, compared strictly, so `"02"` does not match `2`.
279
+
280
+ Exported as `containsStrict` from `@tolki/arr` and `@tolki/obj`, and as `dataContainsStrict` from `@tolki/data`.
281
+
282
+ ```js [@tolki/arr]
283
+ import { containsStrict } from "@tolki/arr";
284
+
285
+ containsStrict([1, 3, 5, "02"], "02");
286
+ // => true
287
+
288
+ containsStrict([1, 3, 5, "02"], 2);
289
+ // => false
290
+ ```
291
+
292
+ #### crossJoin
293
+
294
+ Cross join the given sets, returning every possible combination.
295
+
296
+ Exported as `crossJoin` from `@tolki/arr` and `@tolki/obj`, and as `dataCrossJoin` from `@tolki/data`.
297
+
298
+ Laravel: [`Arr::crossJoin()`](https://laravel.com/docs/helpers#method-array-crossjoin)
299
+
300
+ ```js [@tolki/arr]
301
+ import { crossJoin } from "@tolki/arr";
302
+
303
+ crossJoin([1, 2], ["a", "b"]);
304
+ // => [[1, "a"], [1, "b"], [2, "a"], [2, "b"]]
305
+ ```
306
+
307
+ #### diff
308
+
309
+ Get the items whose values are not present in the other set.
310
+
311
+ Exported as `diff` from `@tolki/arr` and `@tolki/obj`, and as `dataDiff` from `@tolki/data`.
312
+
313
+ ```js [@tolki/arr]
314
+ import { diff } from "@tolki/arr";
315
+
316
+ diff([1, 2, 3, 4], [2, 4]);
317
+ // => [1, 3]
318
+ ```
319
+
320
+ #### diffAssoc
321
+
322
+ Get the items whose key and value are not both present in the other set.
323
+
324
+ Exported as `diffAssoc` from `@tolki/arr` and `@tolki/obj`, and as `dataDiffAssoc` from `@tolki/data`.
325
+
326
+ ```js [@tolki/arr]
327
+ import { diffAssoc } from "@tolki/arr";
328
+
329
+ diffAssoc([1, 2, 3], [1, 9, 3]);
330
+ // => [2]
331
+ ```
332
+
333
+ #### diffAssocUsing
334
+
335
+ Get the items whose key and value are not both present in the other set, using a callback to say whether two keys match.
336
+
337
+ Exported as `diffAssocUsing` from `@tolki/arr` and `@tolki/obj`, and as `dataDiffAssocUsing` from `@tolki/data`.
338
+
339
+ ```js [@tolki/arr]
340
+ import { diffAssocUsing } from "@tolki/arr";
341
+
342
+ const same = (a, b) => String(a).toLowerCase() === String(b).toLowerCase();
343
+
344
+ diffAssocUsing([1, 2, 3], [1, 9, 3], same);
345
+ // => [2]
346
+ ```
347
+
348
+ #### diffKeys
349
+
350
+ Get the items whose key is not present in the other set. Values are ignored.
351
+
352
+ Exported as `diffKeys` from `@tolki/arr` and `@tolki/obj`, and as `dataDiffKeys` from `@tolki/data`.
353
+
354
+ ```js [@tolki/arr]
355
+ import { diffKeys } from "@tolki/arr";
356
+
357
+ diffKeys([1, 2, 3], [9, 9]);
358
+ // => [3]
359
+ ```
360
+
361
+ #### diffKeysUsing
362
+
363
+ Get the items whose key is not present in the other set, using a callback to say whether two keys match. Values are ignored.
364
+
365
+ Exported as `diffKeysUsing` from `@tolki/arr` and `@tolki/obj`, and as `dataDiffKeysUsing` from `@tolki/data`.
366
+
367
+ ```js [@tolki/arr]
368
+ import { diffKeysUsing } from "@tolki/arr";
369
+
370
+ const same = (a, b) => String(a) === String(b);
371
+
372
+ diffKeysUsing([1, 2], { a: 1, 1: 5 }, same);
373
+ // => [1]
374
+ ```
375
+
376
+ #### diffUsing
377
+
378
+ Get the items whose values are not present in the other set, using a callback to say whether two values are equal.
379
+
380
+ Exported as `diffUsing` from `@tolki/arr` and `@tolki/obj`, and as `dataDiffUsing` from `@tolki/data`.
381
+
382
+ ```js [@tolki/arr]
383
+ import { diffUsing } from "@tolki/arr";
384
+
385
+ const same = (a, b) => a.toLowerCase() === b.toLowerCase();
386
+
387
+ diffUsing(["Green", "Brown", "Blue"], ["GREEN", "Yellow"], same);
388
+ // => ["Brown", "Blue"]
389
+ ```
390
+
391
+ #### divide
392
+
393
+ Divide into two lists: one with the keys and the other with the values.
394
+
395
+ Exported as `divide` from `@tolki/arr` and `@tolki/obj`, and as `dataDivide` from `@tolki/data`.
396
+
397
+ Laravel: [`Arr::divide()`](https://laravel.com/docs/helpers#method-array-divide)
398
+
399
+ ```js [@tolki/arr]
400
+ import { divide } from "@tolki/arr";
401
+
402
+ divide(["Desk", 100]);
403
+ // => [[0, 1], ["Desk", 100]]
404
+ ```
405
+
406
+ #### dot
407
+
408
+ Flatten a multi-dimensional array or object into a single level that uses "dot" notation to show depth. See [Dot Notation](https://tolki.abe.dev/arrays-objects/dot-notation.html). The second argument is a key prefix, usually `""`. The third is a depth: how many levels to join into dotted keys. A depth of `0` or `null` joins none, as in Laravel. <!-- docs/php-parity/Arr/dot.json, "dot-depth-through-list", "dot-null-depth", "dot-zero-depth" --> A `Map` in the data is flattened in its key order. <!-- docs/php-parity/Arr/dot.json, "dot-nested-out-of-order" -->
409
+
410
+ Exported as `dot` from `@tolki/arr` and `@tolki/obj`, and as `dataDot` from `@tolki/data`.
411
+
412
+ Laravel: [`Arr::dot()`](https://laravel.com/docs/helpers#method-array-dot)
413
+
414
+ ```js [@tolki/arr]
415
+ import { dot } from "@tolki/arr";
416
+
417
+ dot(["a", ["b", "c"]]);
418
+ // => { 0: "a", "1.0": "b", "1.1": "c" }
419
+ ```
420
+
421
+ #### every
422
+
423
+ Determine if all items pass the given truth test.
424
+
425
+ Exported as `every` from `@tolki/arr` and `@tolki/obj`, and as `dataEvery` from `@tolki/data`.
426
+
427
+ Laravel: [`Arr::every()`](https://laravel.com/docs/helpers#method-array-every)
428
+
429
+ ```js [@tolki/arr]
430
+ import { every } from "@tolki/arr";
431
+
432
+ every([1, 2, 3], (value) => value > 0);
433
+ // => true
434
+
435
+ every([1, 2, 3], (value) => value > 2);
436
+ // => false
437
+ ```
438
+
439
+ #### except
440
+
441
+ Get all of the given array or object except for a specified list of keys.
442
+
443
+ Exported as `except` from `@tolki/arr` and `@tolki/obj`, and as `dataExcept` from `@tolki/data`.
444
+
445
+ Laravel: [`Arr::except()`](https://laravel.com/docs/helpers#method-array-except)
446
+
447
+ ```js [@tolki/arr]
448
+ import { except } from "@tolki/arr";
449
+
450
+ except(["a", "b", "c"], 1);
451
+ // => ["a", "c"]
452
+
453
+ except(["a", "b", "c"], [0, 2]);
454
+ // => ["b"]
455
+ ```
456
+
457
+ #### exceptValues
458
+
459
+ Get all of the given array or object except for a specified list of values. Values match loosely, unless `strict` is `true`.
460
+
461
+ Exported as `exceptValues` from `@tolki/arr` and `@tolki/obj`, and as `dataExceptValues` from `@tolki/data`.
462
+
463
+ Laravel: [`Arr::exceptValues()`](https://laravel.com/docs/helpers#method-array-except-values)
464
+
465
+ ```js [@tolki/arr]
466
+ import { exceptValues } from "@tolki/arr";
467
+
468
+ exceptValues([1, 2, 3, 2], 2);
469
+ // => [1, 3]
470
+
471
+ exceptValues([1, "1", 0, false], [1]);
472
+ // => [0, false]
473
+
474
+ exceptValues([1, "1", 0, false], [1], true);
475
+ // => ["1", 0, false]
476
+ ```
477
+
478
+ #### exists
479
+
480
+ Determine if the given key exists in the provided array or object. A key set to `null` still exists.
481
+
482
+ Exported as `exists` from `@tolki/arr` and `@tolki/obj`, and as `dataExists` from `@tolki/data`.
483
+
484
+ Laravel: [`Arr::exists()`](https://laravel.com/docs/helpers#method-array-exists)
485
+
486
+ ```js [@tolki/arr]
487
+ import { exists } from "@tolki/arr";
488
+
489
+ exists(["a", "b"], 1);
490
+ // => true
491
+
492
+ exists(["a", "b"], 5);
493
+ // => false
494
+ ```
495
+
496
+ #### filter
497
+
498
+ Filter the array or object using a callback. Without a callback, it removes the items that PHP treats as empty. See [Truthiness](https://tolki.abe.dev/arrays-objects/php-semantics.html#truthiness).
499
+
500
+ Exported as `filter` from `@tolki/arr` and `@tolki/obj`, and as `dataFilter` from `@tolki/data`.
501
+
502
+ ```js [@tolki/arr]
503
+ import { filter } from "@tolki/arr";
504
+
505
+ filter([1, 2, 3, 4], (value) => value > 2);
506
+ // => [3, 4]
507
+
508
+ filter([0, 1, "", null, "a"]);
509
+ // => [1, "a"]
510
+ ```
511
+
512
+ #### first
513
+
514
+ Get the first item, or the first item that passes a truth test. When nothing is found, it returns the default value, or `null`.
515
+
516
+ Exported as `first` from `@tolki/arr` and `@tolki/obj`, and as `dataFirst` from `@tolki/data`.
517
+
518
+ Laravel: [`Arr::first()`](https://laravel.com/docs/helpers#method-array-first)
519
+
520
+ ```js [@tolki/arr]
521
+ import { first } from "@tolki/arr";
522
+
523
+ first([100, 200, 300]);
524
+ // => 100
525
+
526
+ first([100, 200, 300], (value) => value >= 150);
527
+ // => 200
528
+
529
+ first([100, 200, 300], (value) => value > 500, "none");
530
+ // => "none"
531
+
532
+ function* naturals() {
533
+ for (let n = 1; ; n++) {
534
+ yield n;
535
+ }
536
+ }
537
+
538
+ first(naturals(), (value) => value > 3);
539
+ // => 4
540
+ ```
541
+
542
+ #### flatten
543
+
544
+ Flatten a multi-dimensional array or object into a single-level array of its values. The depth argument limits how many levels to flatten.
545
+
546
+ Exported as `flatten` from `@tolki/arr` and `@tolki/obj`, and as `dataFlatten` from `@tolki/data`.
547
+
548
+ Laravel: [`Arr::flatten()`](https://laravel.com/docs/helpers#method-array-flatten)
549
+
550
+ ```js [@tolki/arr]
551
+ import { flatten } from "@tolki/arr";
552
+
553
+ flatten(["Joe", ["PHP", "Ruby"]]);
554
+ // => ["Joe", "PHP", "Ruby"]
555
+
556
+ flatten([1, [2, [3, [4]]]], 1);
557
+ // => [1, 2, [3, [4]]]
558
+ ```
559
+
560
+ #### flip
561
+
562
+ Flip the keys and the values: each value becomes a key, and each key becomes its value.
563
+
564
+ Exported as `flip` from `@tolki/arr` and `@tolki/obj`, and as `dataFlip` from `@tolki/data`.
565
+
566
+ ```js [@tolki/arr]
567
+ import { flip } from "@tolki/arr";
568
+
569
+ flip(["a", "b", "c"]);
570
+ // => { a: 0, b: 1, c: 2 }
571
+ ```
572
+
573
+ #### float
574
+
575
+ Get a float item using "dot" notation, throwing an `InvalidArgumentException` if the value is not a number.
576
+
577
+ Exported as `float` from `@tolki/arr` and `@tolki/obj`, and as `dataFloat` from `@tolki/data`.
578
+
579
+ Laravel: [`Arr::float()`](https://laravel.com/docs/helpers#method-array-float)
580
+
581
+ ```js [@tolki/arr]
582
+ import { float } from "@tolki/arr";
583
+
584
+ float([{ price: 19.99 }], "0.price");
585
+ // => 19.99
586
+
587
+ float([{ price: 19.99 }], "0.discount", 1.5);
588
+ // => 1.5
589
+
590
+ float([{ name: "Widget" }], "0.name");
591
+ // => throws InvalidArgumentException
592
+ ```
593
+
594
+ <!-- docs/php-parity/Arr/float.json, "Arr::float rejects a whole-number int" -->
595
+
596
+ `float` accepts a whole number such as `1`, where Laravel's `Arr::float()` throws, because PHP's `is_float()` rejects an integer. JavaScript has one number type. See [Behaviour differences](https://tolki.abe.dev/arrays-objects/differences.html#behaviour-differences).
597
+
598
+ #### forget
599
+
600
+ Remove one or many items from an array or object using "dot" notation. It returns a new value and leaves the original unchanged. See [Return values instead of references](https://tolki.abe.dev/arrays-objects/differences.html#return-values-instead-of-references).
601
+
602
+ Exported as `forget` from `@tolki/arr` and `@tolki/obj`, and as `dataForget` from `@tolki/data`.
603
+
604
+ Laravel: [`Arr::forget()`](https://laravel.com/docs/helpers#method-array-forget) · [`data_forget()`](https://laravel.com/docs/helpers#method-data-forget)
605
+
606
+ ```js [@tolki/arr]
607
+ import { forget } from "@tolki/arr";
608
+
609
+ forget([["a", "b"], ["c"]], "0.1");
610
+ // => [["a"], ["c"]]
611
+
612
+ forget(["a", "b", "c"], [0, 2]);
613
+ // => ["b"]
614
+ ```
615
+
616
+ #### from
617
+
618
+ Get the underlying array or object of items from the given argument: a list stays a list, and a `Map` becomes an object. A scalar throws an `InvalidArgumentException`.
619
+
620
+ Exported as `from` from `@tolki/arr` and `@tolki/obj`, and as `dataFrom` from `@tolki/data`.
621
+
622
+ Laravel: [`Arr::from()`](https://laravel.com/docs/helpers#method-array-from)
623
+
624
+ ```js [@tolki/arr]
625
+ import { from } from "@tolki/arr";
626
+
627
+ from([1, 2]);
628
+ // => [1, 2]
629
+
630
+ from(new Map([["foo", "bar"]]));
631
+ // => { foo: "bar" }
632
+
633
+ from(1);
634
+ // => throws InvalidArgumentException
635
+ ```
636
+
637
+ #### get
638
+
639
+ Get an item from an array or object using "dot" notation. A missing path returns the default value, or `null`. See [Dot Notation](https://tolki.abe.dev/arrays-objects/dot-notation.html).
640
+
641
+ Exported as `get` from `@tolki/arr` and `@tolki/obj`, and as `dataGet` from `@tolki/data`.
642
+
643
+ Laravel: [`Arr::get()`](https://laravel.com/docs/helpers#method-array-get) · [`data_get()`](https://laravel.com/docs/helpers#method-data-get)
644
+
645
+ ```js [@tolki/arr]
646
+ import { get } from "@tolki/arr";
647
+
648
+ get([{ price: 100 }], "0.price");
649
+ // => 100
650
+
651
+ get([{ price: 100 }], "0.discount", 0);
652
+ // => 0
653
+ ```
654
+
655
+ #### has
656
+
657
+ Determine if an item or items exist in an array or object using "dot" notation. A key set to `null` still exists. When given several keys, all of them must exist. See [Dot Notation](https://tolki.abe.dev/arrays-objects/dot-notation.html).
658
+
659
+ Exported as `has` from `@tolki/arr` and `@tolki/obj`, and as `dataHas` from `@tolki/data`.
660
+
661
+ Laravel: [`Arr::has()`](https://laravel.com/docs/helpers#method-array-has)
662
+
663
+ ```js [@tolki/arr]
664
+ import { has } from "@tolki/arr";
665
+
666
+ has([["a"], ["b"]], "0.0");
667
+ // => true
668
+
669
+ has([["a"], ["b"]], ["0.0", "1.1"]);
670
+ // => false
671
+ ```
672
+
673
+ #### hasAll
674
+
675
+ Determine if all keys exist in an array or object using "dot" notation. See [Dot Notation](https://tolki.abe.dev/arrays-objects/dot-notation.html).
676
+
677
+ Exported as `hasAll` from `@tolki/arr` and `@tolki/obj`, and as `dataHasAll` from `@tolki/data`.
678
+
679
+ Laravel: [`Arr::hasAll()`](https://laravel.com/docs/helpers#method-array-hasall)
680
+
681
+ ```js [@tolki/arr]
682
+ import { hasAll } from "@tolki/arr";
683
+
684
+ hasAll([["a"], ["b"]], ["0.0", "1.0"]);
685
+ // => true
686
+
687
+ hasAll([["a"], ["b"]], ["0.0", "1.1"]);
688
+ // => false
689
+ ```
690
+
691
+ #### hasAny
692
+
693
+ Determine if any of the keys exist in an array or object using "dot" notation. See [Dot Notation](https://tolki.abe.dev/arrays-objects/dot-notation.html).
694
+
695
+ Exported as `hasAny` from `@tolki/arr` and `@tolki/obj`, and as `dataHasAny` from `@tolki/data`.
696
+
697
+ Laravel: [`Arr::hasAny()`](https://laravel.com/docs/helpers#method-array-hasany)
698
+
699
+ ```js [@tolki/arr]
700
+ import { hasAny } from "@tolki/arr";
701
+
702
+ hasAny([["a"], ["b"]], ["0.5", "1.0"]);
703
+ // => true
704
+
705
+ hasAny([["a"], ["b"]], ["0.5", "3"]);
706
+ // => false
707
+ ```
708
+
709
+ #### integer
710
+
711
+ Get an integer item using "dot" notation, throwing an `InvalidArgumentException` if the value is not an integer.
712
+
713
+ Exported as `integer` from `@tolki/arr` and `@tolki/obj`, and as `dataInteger` from `@tolki/data`.
714
+
715
+ Laravel: [`Arr::integer()`](https://laravel.com/docs/helpers#method-array-integer)
716
+
717
+ ```js [@tolki/arr]
718
+ import { integer } from "@tolki/arr";
719
+
720
+ integer([{ age: 30 }], "0.age");
721
+ // => 30
722
+
723
+ integer([{ age: 30 }], "0.score", 7);
724
+ // => 7
725
+
726
+ integer([{ age: 30.5 }], "0.age");
727
+ // => throws InvalidArgumentException
728
+ ```
729
+
730
+ #### intersect
731
+
732
+ Get the items whose values are also present in the other set. Values are compared as strings, so `2` matches `"2"`. The keys of the kept items stay with them in an object.
733
+
734
+ Exported as `intersect` from `@tolki/arr` and `@tolki/obj`, and as `dataIntersect` from `@tolki/data`.
735
+
736
+ ```js [@tolki/arr]
737
+ import { intersect } from "@tolki/arr";
738
+
739
+ intersect(["a", "b", "c"], ["b", "c", "d"]);
740
+ // => ["b", "c"]
741
+
742
+ intersect([1, 2, 3], ["2", "3"]);
743
+ // => [2, 3]
744
+ ```
745
+
746
+ #### intersectAssoc
747
+
748
+ Get the items whose key and value are both present in the other set.
749
+
750
+ Exported as `intersectAssoc` from `@tolki/arr` and `@tolki/obj`, and as `dataIntersectAssoc` from `@tolki/data`.
751
+
752
+ ```js [@tolki/arr]
753
+ import { intersectAssoc } from "@tolki/arr";
754
+
755
+ intersectAssoc(["a", "b", "c"], ["a", "x", "c"]);
756
+ // => ["a", "c"]
757
+ ```
758
+
759
+ #### intersectAssocUsing
760
+
761
+ Get the items whose key and value are both present in the other set, using a callback to say whether two keys match.
762
+
763
+ Exported as `intersectAssocUsing` from `@tolki/arr` and `@tolki/obj`, and as `dataIntersectAssocUsing` from `@tolki/data`.
764
+
765
+ ```js [@tolki/arr]
766
+ import { intersectAssocUsing } from "@tolki/arr";
767
+
768
+ const same = (a, b) => String(a) === String(b);
769
+
770
+ intersectAssocUsing(["a", "b", "c"], ["a", "x", "c"], same);
771
+ // => ["a", "c"]
772
+ ```
773
+
774
+ #### intersectByKeys
775
+
776
+ Get the items whose key is present in the other set. Values are ignored.
777
+
778
+ Exported as `intersectByKeys` from `@tolki/arr` and `@tolki/obj`, and as `dataIntersectByKeys` from `@tolki/data`.
779
+
780
+ ```js [@tolki/arr]
781
+ import { intersectByKeys } from "@tolki/arr";
782
+
783
+ intersectByKeys(["a", "b", "c"], ["x", "y"]);
784
+ // => ["a", "b"]
785
+ ```
786
+
787
+ #### intersectUsing
788
+
789
+ Get the items whose values are present in the other set, using a callback to say whether two values are equal.
790
+
791
+ Exported as `intersectUsing` from `@tolki/arr` and `@tolki/obj`, and as `dataIntersectUsing` from `@tolki/data`.
792
+
793
+ ```js [@tolki/arr]
794
+ import { intersectUsing } from "@tolki/arr";
795
+
796
+ const same = (a, b) => a.toLowerCase() === b.toLowerCase();
797
+
798
+ intersectUsing(["Green", "Brown", "Blue"], ["GREEN", "Yellow"], same);
799
+ // => ["Green"]
800
+ ```
801
+
802
+ #### join
803
+
804
+ Join all items using a string. The final item can use a separate glue string.
805
+
806
+ Exported as `join` from `@tolki/arr` and `@tolki/obj`, and as `dataJoin` from `@tolki/data`.
807
+
808
+ Laravel: [`Arr::join()`](https://laravel.com/docs/helpers#method-array-join)
809
+
810
+ ```js [@tolki/arr]
811
+ import { join } from "@tolki/arr";
812
+
813
+ join(["Tailwind", "Alpine", "Laravel", "Livewire"], ", ");
814
+ // => "Tailwind, Alpine, Laravel, Livewire"
815
+
816
+ join(["Tailwind", "Alpine", "Laravel", "Livewire"], ", ", " and ");
817
+ // => "Tailwind, Alpine, Laravel and Livewire"
818
+
819
+ join(["Tailwind", "Alpine"], ", ", " and ");
820
+ // => "Tailwind and Alpine"
821
+ ```
822
+
823
+ #### keyBy
824
+
825
+ Key an array or object of rows by a field, or by the result of a callback.
826
+
827
+ Exported as `keyBy` from `@tolki/arr` and `@tolki/obj`, and as `dataKeyBy` from `@tolki/data`.
828
+
829
+ Laravel: [`Arr::keyBy()`](https://laravel.com/docs/helpers#method-array-keyby)
830
+
831
+ ```js [@tolki/arr]
832
+ import { keyBy } from "@tolki/arr";
833
+
834
+ const products = [
835
+ { product_id: "prod-100", name: "Desk" },
836
+ { product_id: "prod-200", name: "Chair" },
837
+ ];
838
+
839
+ keyBy(products, "product_id");
840
+ // => {
841
+ // "prod-100": { product_id: "prod-100", name: "Desk" },
842
+ // "prod-200": { product_id: "prod-200", name: "Chair" },
843
+ // }
844
+
845
+ keyBy(products, (item) => item.product_id.toUpperCase());
846
+ // => {
847
+ // "PROD-100": { product_id: "prod-100", name: "Desk" },
848
+ // "PROD-200": { product_id: "prod-200", name: "Chair" },
849
+ // }
850
+ ```
851
+
852
+ #### keys
853
+
854
+ Get all of the keys of an array or object.
855
+
856
+ Exported as `keys` from `@tolki/arr` and `@tolki/obj`, and as `dataKeys` from `@tolki/data`.
857
+
858
+ ```js [@tolki/arr]
859
+ import { keys } from "@tolki/arr";
860
+
861
+ keys(["a", "b"]);
862
+ // => [0, 1]
863
+ ```
864
+
865
+ #### last
866
+
867
+ Get the last item, or the last item that passes a truth test. When nothing is found, it returns the default value, or `null`.
868
+
869
+ Exported as `last` from `@tolki/arr` and `@tolki/obj`, and as `dataLast` from `@tolki/data`.
870
+
871
+ Laravel: [`Arr::last()`](https://laravel.com/docs/helpers#method-array-last)
872
+
873
+ ```js [@tolki/arr]
874
+ import { last } from "@tolki/arr";
875
+
876
+ last([100, 200, 300]);
877
+ // => 300
878
+
879
+ last([100, 200, 300, 110], (value) => value > 150);
880
+ // => 300
881
+
882
+ last([], null, "none");
883
+ // => "none"
884
+ ```
885
+
886
+ #### map
887
+
888
+ Run a map over each of the items in an array or object. The callback gets the value, then the index or key.
889
+
890
+ Exported as `map` from `@tolki/arr` and `@tolki/obj`, and as `dataMap` from `@tolki/data`.
891
+
892
+ Laravel: [`Arr::map()`](https://laravel.com/docs/helpers#method-array-map)
893
+
894
+ ```js [@tolki/arr]
895
+ import { map } from "@tolki/arr";
896
+
897
+ map([1, 2, 3], (value) => value * 2);
898
+ // => [2, 4, 6]
899
+
900
+ map(["a", "b"], (value, index) => `${index}:${value}`);
901
+ // => ["0:a", "1:b"]
902
+ ```
903
+
904
+ #### mapSpread
905
+
906
+ Run a map over each row, spreading the row's items into the callback's arguments, followed by the index or key.
907
+
908
+ Exported as `mapSpread` from `@tolki/arr` and `@tolki/obj`, and as `dataMapSpread` from `@tolki/data`.
909
+
910
+ Laravel: [`Arr::mapSpread()`](https://laravel.com/docs/helpers#method-array-map-spread)
911
+
912
+ ```js [@tolki/arr]
913
+ import { mapSpread } from "@tolki/arr";
914
+
915
+ mapSpread(
916
+ [
917
+ ["John", 25],
918
+ ["Jane", 30],
919
+ ],
920
+ (name, age) => `${name} is ${age}`,
921
+ );
922
+ // => ["John is 25", "Jane is 30"]
923
+
924
+ mapSpread(
925
+ [
926
+ [1, 2],
927
+ [3, 4],
928
+ ],
929
+ (a, b, index) => a + b + index,
930
+ );
931
+ // => [3, 8]
932
+ ```
933
+
934
+ #### mapWithKeys
935
+
936
+ Run an associative map over each of the items. The callback returns an object whose pairs become the entries of the result.
937
+
938
+ Exported as `mapWithKeys` from `@tolki/arr` and `@tolki/obj`, and as `dataMapWithKeys` from `@tolki/data`.
939
+
940
+ Laravel: [`Arr::mapWithKeys()`](https://laravel.com/docs/helpers#method-array-map-with-keys)
941
+
942
+ ```js [@tolki/arr]
943
+ import { mapWithKeys } from "@tolki/arr";
944
+
945
+ const users = [
946
+ { name: "John", email: "john@example.com" },
947
+ { name: "Jane", email: "jane@example.com" },
948
+ ];
949
+
950
+ mapWithKeys(users, (user) => ({ [user.email]: user.name }));
951
+ // => { "john@example.com": "John", "jane@example.com": "Jane" }
952
+
953
+ mapWithKeys(["a", "b"], (value, index) => ({ [value]: index }));
954
+ // => { a: 0, b: 1 }
955
+ ```
956
+
957
+ #### only
958
+
959
+ Get a subset of the items from the given array or object. The result keeps the order of the data, not the order of the keys.
960
+
961
+ Exported as `only` from `@tolki/arr` and `@tolki/obj`, and as `dataOnly` from `@tolki/data`.
962
+
963
+ Laravel: [`Arr::only()`](https://laravel.com/docs/helpers#method-array-only)
964
+
965
+ ```js [@tolki/arr]
966
+ import { only } from "@tolki/arr";
967
+
968
+ only(["a", "b", "c", "d"], [0, 2]);
969
+ // => ["a", "c"]
970
+
971
+ only(["a", "b", "c", "d"], [3, 1]);
972
+ // => ["b", "d"]
973
+ ```
974
+
975
+ #### onlyValues
976
+
977
+ Get a subset of the items from the given array or object by value. The comparison is loose, so `"1"` matches `1`; pass `true` as the third argument to compare strictly. See [PHP Semantics](https://tolki.abe.dev/arrays-objects/php-semantics.html#loose-and-strict-comparison).
978
+
979
+ Exported as `onlyValues` from `@tolki/arr` and `@tolki/obj`, and as `dataOnlyValues` from `@tolki/data`.
980
+
981
+ Laravel: [`Arr::onlyValues()`](https://laravel.com/docs/helpers#method-array-only-values)
982
+
983
+ <!-- docs/php-parity/Arr/onlyValues.json, "onlyValues-list-keeps-gap" -->
984
+
985
+ ```js [@tolki/arr]
986
+ import { onlyValues } from "@tolki/arr";
987
+
988
+ // Laravel's list result keeps the original positions (0 and 2); a JavaScript array cannot, so it renumbers.
989
+ onlyValues(["foo", "bar", "baz", "qux"], ["foo", "baz"]);
990
+ // => ["foo", "baz"]
991
+
992
+ onlyValues([1, "1", 2], [1]);
993
+ // => [1, "1"]
994
+
995
+ onlyValues([1, "1", 2], [1], true);
996
+ // => [1]
997
+ ```
998
+
999
+ #### pad
1000
+
1001
+ Pad an array or object to the specified length with a value. A positive size pads at the end, and a negative size pads at the start.
1002
+
1003
+ Exported as `pad` from `@tolki/arr` and `@tolki/obj`, and as `dataPad` from `@tolki/data`.
1004
+
1005
+ ```js [@tolki/arr]
1006
+ import { pad } from "@tolki/arr";
1007
+
1008
+ pad([1, 2, 3], 5, 0);
1009
+ // => [1, 2, 3, 0, 0]
1010
+
1011
+ pad([1, 2, 3], -5, 0);
1012
+ // => [0, 0, 1, 2, 3]
1013
+ ```
1014
+
1015
+ #### partition
1016
+
1017
+ Partition an array or object into two using the given callback. The result is a pair: the items that pass, then the items that fail.
1018
+
1019
+ Exported as `partition` from `@tolki/arr` and `@tolki/obj`, and as `dataPartition` from `@tolki/data`.
1020
+
1021
+ Laravel: [`Arr::partition()`](https://laravel.com/docs/helpers#method-array-partition)
1022
+
1023
+ ```js [@tolki/arr]
1024
+ import { partition } from "@tolki/arr";
1025
+
1026
+ partition([1, 2, 3, 4], (value) => value > 2);
1027
+ // => [[3, 4], [1, 2]]
1028
+
1029
+ partition(["a", "b", null, "c"], (value) => value !== null);
1030
+ // => [["a", "b", "c"], [null]]
1031
+ ```
1032
+
1033
+ #### pluck
1034
+
1035
+ Pluck an array of values from an array or object of rows. The path can be a dot path, and a `*` segment collects every item at that step; see [Wildcards](https://tolki.abe.dev/arrays-objects/dot-notation.html#wildcards).
1036
+
1037
+ Exported as `pluck` from `@tolki/arr` and `@tolki/obj`, and as `dataPluck` from `@tolki/data`.
1038
+
1039
+ Laravel: [`Arr::pluck()`](https://laravel.com/docs/helpers#method-array-pluck)
1040
+
1041
+ ```js [@tolki/arr]
1042
+ import { pluck } from "@tolki/arr";
1043
+
1044
+ const posts = [
1045
+ { title: "A", author: { name: "Ann" }, tags: [{ id: 1 }, { id: 2 }] },
1046
+ { title: "B", author: { name: "Bob" }, tags: [{ id: 3 }] },
1047
+ ];
1048
+
1049
+ pluck(posts, "author.name");
1050
+ // => ["Ann", "Bob"]
1051
+
1052
+ pluck(posts, "author.name", "title");
1053
+ // => { A: "Ann", B: "Bob" }
1054
+
1055
+ pluck(posts, "tags.*.id");
1056
+ // => [[1, 2], [3]]
1057
+ ```
1058
+
1059
+ #### pop
1060
+
1061
+ Get and remove the last N items from an array or object. It changes the data you pass in, unlike most functions on this page. See [Return values instead of references](https://tolki.abe.dev/arrays-objects/differences.html#return-values-instead-of-references).
1062
+
1063
+ Exported as `pop` from `@tolki/arr` and `@tolki/obj`, and as `dataPop` from `@tolki/data`.
1064
+
1065
+ ```js [@tolki/arr]
1066
+ import { pop } from "@tolki/arr";
1067
+
1068
+ const stack = [1, 2, 3];
1069
+
1070
+ pop(stack);
1071
+ // => 3
1072
+
1073
+ stack;
1074
+ // => [1, 2]
1075
+
1076
+ pop(stack, 2);
1077
+ // => [2, 1]
1078
+
1079
+ stack;
1080
+ // => []
1081
+ ```
1082
+
1083
+ #### prepend
1084
+
1085
+ Push an item onto the beginning of an array or object. It returns a new value and leaves the original unchanged.
1086
+
1087
+ Exported as `prepend` from `@tolki/arr` and `@tolki/obj`, and as `dataPrepend` from `@tolki/data`.
1088
+
1089
+ Laravel: [`Arr::prepend()`](https://laravel.com/docs/helpers#method-array-prepend)
1090
+
1091
+ ```js [@tolki/arr]
1092
+ import { prepend } from "@tolki/arr";
1093
+
1094
+ const letters = ["b", "c"];
1095
+
1096
+ prepend(letters, "a");
1097
+ // => ["a", "b", "c"]
1098
+
1099
+ letters;
1100
+ // => ["b", "c"]
1101
+ ```
1102
+
1103
+ #### prependKeysWith
1104
+
1105
+ Prepend the key names of an array or object with the given string.
1106
+
1107
+ Exported as `prependKeysWith` from `@tolki/arr` and `@tolki/obj`, and as `dataPrependKeysWith` from `@tolki/data`.
1108
+
1109
+ Laravel: [`Arr::prependKeysWith()`](https://laravel.com/docs/helpers#method-array-prependkeyswith)
1110
+
1111
+ ```js [@tolki/arr]
1112
+ import { prependKeysWith } from "@tolki/arr";
1113
+
1114
+ // A list has no key names, so its indexes become the keys.
1115
+ prependKeysWith(["a", "b"], "item_");
1116
+ // => { item_0: "a", item_1: "b" }
1117
+ ```
1118
+
1119
+ #### pull
1120
+
1121
+ Get a value from an array or object, and remove it. It returns `{ value, data }`, where `data` is what is left, and it leaves the original unchanged. See [Return values instead of references](https://tolki.abe.dev/arrays-objects/differences.html#return-values-instead-of-references).
1122
+
1123
+ Exported as `pull` from `@tolki/arr` and `@tolki/obj`, and as `dataPull` from `@tolki/data`.
1124
+
1125
+ Laravel: [`Arr::pull()`](https://laravel.com/docs/helpers#method-array-pull)
1126
+
1127
+ ```js [@tolki/arr]
1128
+ import { pull } from "@tolki/arr";
1129
+
1130
+ const letters = ["a", ["b", "c"]];
1131
+
1132
+ pull(letters, "1.0");
1133
+ // => { value: "b", data: ["a", ["c"]] }
1134
+
1135
+ letters;
1136
+ // => ["a", ["b", "c"]]
1137
+
1138
+ pull(["a"], 5, "none");
1139
+ // => { value: "none", data: ["a"] }
1140
+ ```
1141
+
1142
+ #### push
1143
+
1144
+ Push one or more items into the array at the given key, using "dot" notation. It returns a new value and leaves the original unchanged. See [Return values instead of references](https://tolki.abe.dev/arrays-objects/differences.html#return-values-instead-of-references). When the target is an object or a `Map`, the new item goes under the next whole-number key, as in Laravel. <!-- docs/php-parity/Arr/push.json, "push-assoc-leaf-appends", "push-out-of-order-leaf-appends-after-highest" -->
1145
+
1146
+ Exported as `push` from `@tolki/arr` and `@tolki/obj`, and as `dataPush` from `@tolki/data`.
1147
+
1148
+ Laravel: [`Arr::push()`](https://laravel.com/docs/helpers#method-array-push)
1149
+
1150
+ ```js [@tolki/arr]
1151
+ import { push } from "@tolki/arr";
1152
+
1153
+ const rows = [["a"], ["b"]];
1154
+
1155
+ push(rows, "1", "c");
1156
+ // => [["a"], ["b", "c"]]
1157
+
1158
+ rows;
1159
+ // => [["a"], ["b"]]
1160
+
1161
+ push([1, 2], null, 3, 4);
1162
+ // => [1, 2, 3, 4]
1163
+ ```
1164
+
1165
+ #### query
1166
+
1167
+ Convert an array or object into a URL-encoded query string.
1168
+
1169
+ Exported as `query` from `@tolki/arr` and `@tolki/obj`, and as `dataQuery` from `@tolki/data`.
1170
+
1171
+ Laravel: [`Arr::query()`](https://laravel.com/docs/helpers#method-array-query)
1172
+
1173
+ ```js [@tolki/arr]
1174
+ import { query } from "@tolki/arr";
1175
+
1176
+ query(["a", "b"]);
1177
+ // => "0=a&1=b"
1178
+
1179
+ query([["x", "y"]]);
1180
+ // => "0%5B0%5D=x&0%5B1%5D=y"
1181
+ ```
1182
+
1183
+ #### random
1184
+
1185
+ Get one or a specified number of random values from an array or object. The picked items come back in the order they have in the data.
1186
+
1187
+ Exported as `random` from `@tolki/arr` and `@tolki/obj`, and as `dataRandom` from `@tolki/data`.
1188
+
1189
+ Laravel: [`Arr::random()`](https://laravel.com/docs/helpers#method-array-random)
1190
+
1191
+ ```js [@tolki/arr]
1192
+ import { random } from "@tolki/arr";
1193
+
1194
+ random([1, 2, 3, 4], 2);
1195
+ // e.g. [2, 4]
1196
+
1197
+ random([1, 2, 3, 4], 2).length;
1198
+ // => 2
1199
+
1200
+ [1, 2, 3].includes(random([1, 2, 3]));
1201
+ // => true
1202
+
1203
+ random([1, 2], 5);
1204
+ // => throws InvalidArgumentException
1205
+ ```
1206
+
1207
+ #### reject
1208
+
1209
+ Filter an array or object using the negation of the given callback: the items that fail the test stay.
1210
+
1211
+ Exported as `reject` from `@tolki/arr` and `@tolki/obj`, and as `dataReject` from `@tolki/data`.
1212
+
1213
+ Laravel: [`Arr::reject()`](https://laravel.com/docs/helpers#method-array-reject)
1214
+
1215
+ ```js [@tolki/arr]
1216
+ import { reject } from "@tolki/arr";
1217
+
1218
+ reject([1, 2, 3, 4], (value) => value > 2);
1219
+ // => [1, 2]
1220
+
1221
+ reject(["a", "b", null, "c"], (value) => value === null);
1222
+ // => ["a", "b", "c"]
1223
+ ```
1224
+
1225
+ #### replace
1226
+
1227
+ Replace the items of an array or object with the given replacer items. It returns a new value. Only the top level is replaced; see `replaceRecursive` for nested values.
1228
+
1229
+ Exported as `replace` from `@tolki/arr` and `@tolki/obj`, and as `dataReplace` from `@tolki/data`.
1230
+
1231
+ ```js [@tolki/arr]
1232
+ import { replace } from "@tolki/arr";
1233
+
1234
+ replace(["a", "b", "c"], ["d", "e"]);
1235
+ // => ["d", "e", "c"]
1236
+
1237
+ replace(["a", "b", "c"], { 1: "d", 2: "e", 3: "f" });
1238
+ // => ["a", "d", "e", "f"]
1239
+ ```
1240
+
1241
+ #### replaceRecursive
1242
+
1243
+ Recursively replace the items of an array or object with the given items. Two nested arrays or objects merge, and any other value is replaced. It returns a new value.
1244
+
1245
+ Exported as `replaceRecursive` from `@tolki/arr` and `@tolki/obj`, and as `dataReplaceRecursive` from `@tolki/data`.
1246
+
1247
+ ```js [@tolki/arr]
1248
+ import { replaceRecursive } from "@tolki/arr";
1249
+
1250
+ replaceRecursive([{ a: 1, b: { c: 2, d: 3 } }], [{ b: { c: 9 } }]);
1251
+ // => [{ a: 1, b: { c: 9, d: 3 } }]
1252
+
1253
+ replaceRecursive([["a", "b"], ["c"]], [["x"]]);
1254
+ // => [["x", "b"], ["c"]]
1255
+ ```
1256
+
1257
+ #### reverse
1258
+
1259
+ Reverse the order of an array or object and return the result.
1260
+
1261
+ Exported as `reverse` from `@tolki/arr` and `@tolki/obj`, and as `dataReverse` from `@tolki/data`.
1262
+
1263
+ ```js [@tolki/arr]
1264
+ import { reverse } from "@tolki/arr";
1265
+
1266
+ reverse([1, 2, 3]);
1267
+ // => [3, 2, 1]
1268
+
1269
+ reverse(["a", "b", "c"]);
1270
+ // => ["c", "b", "a"]
1271
+ ```
1272
+
1273
+ #### select
1274
+
1275
+ Select an array or object of values from each item in an array or object. Each key is an exact name, not a dot path.
1276
+
1277
+ Exported as `select` from `@tolki/arr` and `@tolki/obj`, and as `dataSelect` from `@tolki/data`.
1278
+
1279
+ Laravel: [`Arr::select()`](https://laravel.com/docs/helpers#method-array-select)
1280
+
1281
+ ```js [@tolki/arr]
1282
+ import { select } from "@tolki/arr";
1283
+
1284
+ const products = [
1285
+ { name: "Desk", price: 100, sku: "D1" },
1286
+ { name: "Chair", price: 50, sku: "C1" },
1287
+ ];
1288
+
1289
+ select(products, ["name", "price"]);
1290
+ // => [{ name: "Desk", price: 100 }, { name: "Chair", price: 50 }]
1291
+
1292
+ select(products, "name");
1293
+ // => [{ name: "Desk" }, { name: "Chair" }]
1294
+
1295
+ select([{ a: { b: 1 }, c: 2 }], "a.b");
1296
+ // => [{}]
1297
+ ```
1298
+
1299
+ #### set
1300
+
1301
+ Set an array or object item to a given value using "dot" notation. It returns a new value and leaves the original unchanged. See [Return values instead of references](https://tolki.abe.dev/arrays-objects/differences.html#return-values-instead-of-references).
1302
+
1303
+ Exported as `set` from `@tolki/arr` and `@tolki/obj`, and as `dataSet` from `@tolki/data`.
1304
+
1305
+ Laravel: [`Arr::set()`](https://laravel.com/docs/helpers#method-array-set) · [`data_set()`](https://laravel.com/docs/helpers#method-data-set)
1306
+
1307
+ ```js [@tolki/arr]
1308
+ import { set } from "@tolki/arr";
1309
+
1310
+ const grid = [["a", "b"]];
1311
+
1312
+ set(grid, "0.1", "x");
1313
+ // => [["a", "x"]]
1314
+
1315
+ grid;
1316
+ // => [["a", "b"]]
1317
+
1318
+ set(["a", "b", "c"], 1, "x");
1319
+ // => ["a", "x", "c"]
1320
+ ```
1321
+
1322
+ #### shift
1323
+
1324
+ Get and remove the first N items from an array or object. It changes the data you pass in, unlike most functions on this page. See [Return values instead of references](https://tolki.abe.dev/arrays-objects/differences.html#return-values-instead-of-references).
1325
+
1326
+ Exported as `shift` from `@tolki/arr` and `@tolki/obj`, and as `dataShift` from `@tolki/data`.
1327
+
1328
+ ```js [@tolki/arr]
1329
+ import { shift } from "@tolki/arr";
1330
+
1331
+ const queue = [1, 2, 3];
1332
+
1333
+ shift(queue);
1334
+ // => 1
1335
+
1336
+ queue;
1337
+ // => [2, 3]
1338
+
1339
+ shift(queue, 2);
1340
+ // => [2, 3]
1341
+
1342
+ queue;
1343
+ // => []
1344
+ ```
1345
+
1346
+ #### shuffle
1347
+
1348
+ Shuffle an array or object and return the result. The result is a new value; an object comes back as a list-like object whose keys are `0` to `n - 1`.
1349
+
1350
+ Exported as `shuffle` from `@tolki/arr` and `@tolki/obj`, and as `dataShuffle` from `@tolki/data`.
1351
+
1352
+ Laravel: [`Arr::shuffle()`](https://laravel.com/docs/helpers#method-array-shuffle)
1353
+
1354
+ ```js [@tolki/arr]
1355
+ import { shuffle } from "@tolki/arr";
1356
+
1357
+ shuffle([1, 2, 3, 4, 5]);
1358
+ // e.g. [3, 1, 5, 2, 4]
1359
+
1360
+ shuffle([1, 2, 3, 4, 5]).length;
1361
+ // => 5
1362
+
1363
+ shuffle([3, 1, 2]).sort();
1364
+ // => [1, 2, 3]
1365
+ ```
1366
+
1367
+ #### skipUntil
1368
+
1369
+ Skip items in an array or object until the given condition is met, and return the rest. The condition is a value, matched strictly, so `"2"` does not match `2`, or a callback.
1370
+
1371
+ Exported as `skipUntil` from `@tolki/arr` and `@tolki/obj`, and as `dataSkipUntil` from `@tolki/data`.
1372
+
1373
+ ```js [@tolki/arr]
1374
+ import { skipUntil } from "@tolki/arr";
1375
+
1376
+ skipUntil([1, 2, 3, 4], 3);
1377
+ // => [3, 4]
1378
+
1379
+ skipUntil([1, 2, 3, 4], (value) => value >= 3);
1380
+ // => [3, 4]
1381
+
1382
+ skipUntil([1, 2, 3, 4], 5);
1383
+ // => []
1384
+
1385
+ skipUntil([1, 2, 3], "2");
1386
+ // => []
1387
+ ```
1388
+
1389
+ #### skipWhile
1390
+
1391
+ Skip items in an array or object while the given condition is met, and return the rest. The condition is a value, matched strictly, or a callback.
1392
+
1393
+ Exported as `skipWhile` from `@tolki/arr` and `@tolki/obj`, and as `dataSkipWhile` from `@tolki/data`.
1394
+
1395
+ ```js [@tolki/arr]
1396
+ import { skipWhile } from "@tolki/arr";
1397
+
1398
+ skipWhile([1, 1, 2, 1], 1);
1399
+ // => [2, 1]
1400
+
1401
+ skipWhile([1, 2, 3, 4], (value) => value < 3);
1402
+ // => [3, 4]
1403
+
1404
+ skipWhile([1, 2, 3, 4], 5);
1405
+ // => [1, 2, 3, 4]
1406
+ ```
1407
+
1408
+ #### slice
1409
+
1410
+ Get a portion of an array or object, starting at an offset. The original is left unchanged; `splice` is the function that removes items.
1411
+
1412
+ Exported as `slice` from `@tolki/arr` and `@tolki/obj`, and as `dataSlice` from `@tolki/data`.
1413
+
1414
+ ```js [@tolki/arr]
1415
+ import { slice } from "@tolki/arr";
1416
+
1417
+ slice([1, 2, 3, 4, 5], 1, 3);
1418
+ // => [2, 3, 4]
1419
+
1420
+ slice([1, 2, 3, 4, 5], -2);
1421
+ // => [4, 5]
1422
+
1423
+ slice([1, 2, 3, 4, 5], 1, -1);
1424
+ // => [2, 3, 4]
1425
+ ```
1426
+
1427
+ #### sole
1428
+
1429
+ Get the first item, but only if exactly one item exists. Otherwise it throws `ItemNotFoundException` when none is found, or `MultipleItemsFoundException` when several are. With a callback, it counts only the items the callback accepts.
1430
+
1431
+ Exported as `sole` from `@tolki/arr` and `@tolki/obj`, and as `dataSole` from `@tolki/data`.
1432
+
1433
+ Laravel: [`Arr::sole()`](https://laravel.com/docs/helpers#method-array-sole)
1434
+
1435
+ ```js [@tolki/arr]
1436
+ import { sole } from "@tolki/arr";
1437
+
1438
+ sole([42]);
1439
+ // => 42
1440
+
1441
+ sole([1, 2, 3], (value) => value > 2);
1442
+ // => 3
1443
+
1444
+ sole([]);
1445
+ // => throws ItemNotFoundException
1446
+
1447
+ sole([1, 2]);
1448
+ // => throws MultipleItemsFoundException
1449
+ ```
1450
+
1451
+ #### some
1452
+
1453
+ Determine if some items pass the given truth test.
1454
+
1455
+ Exported as `some` from `@tolki/arr` and `@tolki/obj`, and as `dataSome` from `@tolki/data`.
1456
+
1457
+ Laravel: [`Arr::some()`](https://laravel.com/docs/helpers#method-array-some)
1458
+
1459
+ ```js [@tolki/arr]
1460
+ import { some } from "@tolki/arr";
1461
+
1462
+ some([1, 2, 3], (value) => value % 2 === 0);
1463
+ // => true
1464
+
1465
+ some([1, 3, 5], (value) => value % 2 === 0);
1466
+ // => false
1467
+ ```
1468
+
1469
+ #### sort
1470
+
1471
+ Sort an array or object by its values, by a callback, by a "dot" path, or by a list of sort descriptors. It returns a new value and leaves the original unchanged.
1472
+
1473
+ Exported as `sort` from `@tolki/arr` and `@tolki/obj`, and as `dataSort` from `@tolki/data`.
1474
+
1475
+ Laravel: [`Arr::sort()`](https://laravel.com/docs/helpers#method-array-sort)
1476
+
1477
+ ```js [@tolki/arr]
1478
+ import { sort } from "@tolki/arr";
1479
+
1480
+ sort([3, 1, 4, 1, 5]);
1481
+ // => [1, 1, 3, 4, 5]
1482
+
1483
+ const people = [
1484
+ { name: "John", age: 25 },
1485
+ { name: "Jane", age: 30 },
1486
+ { name: "Al", age: 20 },
1487
+ ];
1488
+
1489
+ sort(people, "age");
1490
+ // => [
1491
+ // { name: "Al", age: 20 },
1492
+ // { name: "John", age: 25 },
1493
+ // { name: "Jane", age: 30 },
1494
+ // ]
1495
+
1496
+ sort(people, (person) => person.name);
1497
+ // => [
1498
+ // { name: "Al", age: 20 },
1499
+ // { name: "Jane", age: 30 },
1500
+ // { name: "John", age: 25 },
1501
+ // ]
1502
+
1503
+ const staff = [
1504
+ { name: "John", age: 25 },
1505
+ { name: "Jane", age: 30 },
1506
+ { name: "Al", age: 25 },
1507
+ ];
1508
+
1509
+ sort(staff, [
1510
+ ["age", "desc"],
1511
+ ["name", "asc"],
1512
+ ]);
1513
+ // => [
1514
+ // { name: "Jane", age: 30 },
1515
+ // { name: "Al", age: 25 },
1516
+ // { name: "John", age: 25 },
1517
+ // ]
1518
+ ```
1519
+
1520
+ #### sortDesc
1521
+
1522
+ Sort an array or object in descending order. It takes the same callbacks, paths and descriptors as `sort`, and returns a new value.
1523
+
1524
+ Exported as `sortDesc` from `@tolki/arr` and `@tolki/obj`, and as `dataSortDesc` from `@tolki/data`.
1525
+
1526
+ Laravel: [`Arr::sortDesc()`](https://laravel.com/docs/helpers#method-array-sort-desc)
1527
+
1528
+ ```js [@tolki/arr]
1529
+ import { sortDesc } from "@tolki/arr";
1530
+
1531
+ sortDesc([3, 1, 4, 1, 5]);
1532
+ // => [5, 4, 3, 1, 1]
1533
+
1534
+ sortDesc(["banana", "apple", "cherry"]);
1535
+ // => ["cherry", "banana", "apple"]
1536
+ ```
1537
+
1538
+ #### sortRecursive
1539
+
1540
+ Recursively sort an array or object by its keys and values. Nested lists sort by value and nested objects sort by key.
1541
+
1542
+ Exported as `sortRecursive` from `@tolki/arr` and `@tolki/obj`, and as `dataSortRecursive` from `@tolki/data`.
1543
+
1544
+ Laravel: [`Arr::sortRecursive()`](https://laravel.com/docs/helpers#method-array-sort-recursive)
1545
+
1546
+ ```js [@tolki/arr]
1547
+ import { sortRecursive } from "@tolki/arr";
1548
+
1549
+ sortRecursive([
1550
+ [3, 1, 2],
1551
+ [9, 8],
1552
+ ]);
1553
+ // => [[8, 9], [1, 2, 3]]
1554
+ ```
1555
+
1556
+ #### sortRecursiveDesc
1557
+
1558
+ Recursively sort an array or object by its keys and values in descending order.
1559
+
1560
+ Exported as `sortRecursiveDesc` from `@tolki/arr` and `@tolki/obj`, and as `dataSortRecursiveDesc` from `@tolki/data`.
1561
+
1562
+ ```js [@tolki/arr]
1563
+ import { sortRecursiveDesc } from "@tolki/arr";
1564
+
1565
+ sortRecursiveDesc([
1566
+ [1, 3, 2],
1567
+ [8, 9],
1568
+ ]);
1569
+ // => [[3, 2, 1], [9, 8]]
1570
+ ```
1571
+
1572
+ #### splice
1573
+
1574
+ Remove a portion of an array or object, optionally put replacements in its place, and return what was removed. It changes the data you pass in, unlike most functions on this page. See [Return values instead of references](https://tolki.abe.dev/arrays-objects/differences.html#return-values-instead-of-references).
1575
+
1576
+ Exported as `splice` from `@tolki/arr` and `@tolki/obj`, and as `dataSplice` from `@tolki/data`.
1577
+
1578
+ ```js [@tolki/arr]
1579
+ import { splice } from "@tolki/arr";
1580
+
1581
+ const queue = [1, 2, 3, 4, 5];
1582
+
1583
+ splice(queue, 1, 2);
1584
+ // => [2, 3]
1585
+
1586
+ queue;
1587
+ // => [1, 4, 5]
1588
+
1589
+ splice(queue, 1, 0, ["a", "b"]);
1590
+ // => []
1591
+
1592
+ queue;
1593
+ // => [1, "a", "b", 4, 5]
1594
+
1595
+ splice(queue, 3);
1596
+ // => [4, 5]
1597
+
1598
+ queue;
1599
+ // => [1, "a", "b"]
1600
+ ```
1601
+
1602
+ #### string
1603
+
1604
+ Get a string item using "dot" notation, throwing an `InvalidArgumentException` if the value is not a string.
1605
+
1606
+ Exported as `string` from `@tolki/arr` and `@tolki/obj`, and as `dataString` from `@tolki/data`.
1607
+
1608
+ Laravel: [`Arr::string()`](https://laravel.com/docs/helpers#method-array-string)
1609
+
1610
+ ```js [@tolki/arr]
1611
+ import { string } from "@tolki/arr";
1612
+
1613
+ string([{ name: "John" }], "0.name");
1614
+ // => "John"
1615
+
1616
+ string([{ name: "John" }], "0.title", "n/a");
1617
+ // => "n/a"
1618
+
1619
+ string([{ name: 123 }], "0.name");
1620
+ // => throws InvalidArgumentException
1621
+ ```
1622
+
1623
+ #### take
1624
+
1625
+ Take the first or last N items of an array or object. A negative number takes from the end.
1626
+
1627
+ Exported as `take` from `@tolki/arr` and `@tolki/obj`, and as `dataTake` from `@tolki/data`.
1628
+
1629
+ Laravel: [`Arr::take()`](https://laravel.com/docs/helpers#method-array-take)
1630
+
1631
+ ```js [@tolki/arr]
1632
+ import { take } from "@tolki/arr";
1633
+
1634
+ take([1, 2, 3, 4, 5], 2);
1635
+ // => [1, 2]
1636
+
1637
+ take([1, 2, 3, 4, 5], -2);
1638
+ // => [4, 5]
1639
+
1640
+ take([1, 2, 3], 5);
1641
+ // => [1, 2, 3]
1642
+ ```
1643
+
1644
+ #### takeUntil
1645
+
1646
+ Take items from an array or object until the given condition is met. The condition is a value, matched strictly, or a callback.
1647
+
1648
+ Exported as `takeUntil` from `@tolki/arr` and `@tolki/obj`, and as `dataTakeUntil` from `@tolki/data`.
1649
+
1650
+ ```js [@tolki/arr]
1651
+ import { takeUntil } from "@tolki/arr";
1652
+
1653
+ takeUntil([1, 2, 3, 4], 3);
1654
+ // => [1, 2]
1655
+
1656
+ takeUntil([1, 2, 3, 4], (value) => value >= 3);
1657
+ // => [1, 2]
1658
+
1659
+ takeUntil([1, 2, 3, 4], 99);
1660
+ // => [1, 2, 3, 4]
1661
+ ```
1662
+
1663
+ #### takeWhile
1664
+
1665
+ Take items from an array or object while the given condition is met. The condition is a value, matched strictly, or a callback.
1666
+
1667
+ Exported as `takeWhile` from `@tolki/arr` and `@tolki/obj`, and as `dataTakeWhile` from `@tolki/data`.
1668
+
1669
+ ```js [@tolki/arr]
1670
+ import { takeWhile } from "@tolki/arr";
1671
+
1672
+ takeWhile([1, 1, 2, 2], 1);
1673
+ // => [1, 1]
1674
+
1675
+ takeWhile([1, 2, 3, 4], (value) => value < 3);
1676
+ // => [1, 2]
1677
+
1678
+ takeWhile([1, 2, 3, 4], 2);
1679
+ // => []
1680
+ ```
1681
+
1682
+ #### toCssClasses
1683
+
1684
+ Conditionally compile CSS classes into a class list. A plain entry is always kept. A `class: condition` entry is kept when its condition is true.
1685
+
1686
+ Exported as `toCssClasses` from `@tolki/arr` and `@tolki/obj`, and as `dataToCssClasses` from `@tolki/data`.
1687
+
1688
+ Laravel: [`Arr::toCssClasses()`](https://laravel.com/docs/helpers#method-array-to-css-classes)
1689
+
1690
+ ```js [@tolki/arr]
1691
+ import { toCssClasses } from "@tolki/arr";
1692
+
1693
+ toCssClasses(["font-bold", "mt-4"]);
1694
+ // => "font-bold mt-4"
1695
+
1696
+ // An array can carry extra string keys alongside its list items.
1697
+ toCssClasses(
1698
+ Object.assign(["font-bold", "mt-4"], { "ml-2": true, "mr-2": false }),
1699
+ );
1700
+ // => "font-bold mt-4 ml-2"
1701
+ ```
1702
+
1703
+ #### toCssStyles
1704
+
1705
+ Conditionally compile CSS styles into a style list. Each style ends with a semicolon, and the styles are separated by a space.
1706
+
1707
+ Exported as `toCssStyles` from `@tolki/arr` and `@tolki/obj`, and as `dataToCssStyles` from `@tolki/data`.
1708
+
1709
+ Laravel: [`Arr::toCssStyles()`](https://laravel.com/docs/helpers#method-array-to-css-styles)
1710
+
1711
+ ```js [@tolki/arr]
1712
+ import { toCssStyles } from "@tolki/arr";
1713
+
1714
+ toCssStyles(["font-weight: bold", "margin-top: 4px"]);
1715
+ // => "font-weight: bold; margin-top: 4px;"
1716
+
1717
+ toCssStyles(["font-weight: bold;", "margin-top: 4px"]);
1718
+ // => "font-weight: bold; margin-top: 4px;"
1719
+ ```
1720
+
1721
+ #### undot
1722
+
1723
+ Expand a flat object with "dot" keys into a nested array or object.
1724
+
1725
+ Exported as `undot` from `@tolki/arr` and `@tolki/obj`, and as `dataUndot` from `@tolki/data`.
1726
+
1727
+ Laravel: [`Arr::undot()`](https://laravel.com/docs/helpers#method-array-undot)
1728
+
1729
+ ```js [@tolki/arr]
1730
+ import { undot } from "@tolki/arr";
1731
+
1732
+ // Every key segment must be a whole number, because the result is a list.
1733
+ undot({ 0: "a", "1.0": "b", 1.1: "c" });
1734
+ // => ["a", ["b", "c"]]
1735
+ ```
1736
+
1737
+ #### union
1738
+
1739
+ Union arrays or objects into one. An earlier argument wins: a later one only adds the keys the result does not have yet.
1740
+
1741
+ Exported as `union` from `@tolki/arr` and `@tolki/obj`, and as `dataUnion` from `@tolki/data`.
1742
+
1743
+ ```js [@tolki/arr]
1744
+ import { union } from "@tolki/arr";
1745
+
1746
+ // The union is by position, not by value: index 0 and 1 keep their first values.
1747
+ union([1, 2], [3, 4, 5]);
1748
+ // => [1, 2, 5]
1749
+ ```
1750
+
1751
+ #### unshift
1752
+
1753
+ Prepend one or more items to the start of an array or object. It changes the data you pass in, unlike most functions on this page. See [Return values instead of references](https://tolki.abe.dev/arrays-objects/differences.html#return-values-instead-of-references).
1754
+
1755
+ Exported as `unshift` from `@tolki/arr` and `@tolki/obj`, and as `dataUnshift` from `@tolki/data`.
1756
+
1757
+ ```js [@tolki/arr]
1758
+ import { unshift } from "@tolki/arr";
1759
+
1760
+ const queue = [3, 4];
1761
+
1762
+ unshift(queue, 1, 2);
1763
+ // => [1, 2, 3, 4]
1764
+
1765
+ queue;
1766
+ // => [1, 2, 3, 4]
1767
+ ```
1768
+
1769
+ #### values
1770
+
1771
+ Get all the values of an array or object as a list.
1772
+
1773
+ Exported as `values` from `@tolki/arr` and `@tolki/obj`, and as `dataValues` from `@tolki/data`.
1774
+
1775
+ ```js [@tolki/arr]
1776
+ import { values } from "@tolki/arr";
1777
+
1778
+ values(["name", "age", "city"]);
1779
+ // => ["name", "age", "city"]
1780
+
1781
+ values([]);
1782
+ // => []
1783
+ ```
1784
+
1785
+ #### where
1786
+
1787
+ Filter an array or object using the given callback. A list comes back renumbered from 0; see [Behaviour differences](https://tolki.abe.dev/arrays-objects/differences.html#behaviour-differences).
1788
+
1789
+ Exported as `where` from `@tolki/arr` and `@tolki/obj`, and as `dataWhere` from `@tolki/data`.
1790
+
1791
+ Laravel: [`Arr::where()`](https://laravel.com/docs/helpers#method-array-where)
1792
+
1793
+ ```js [@tolki/arr]
1794
+ import { where } from "@tolki/arr";
1795
+
1796
+ where([1, 2, 3, 4], (value) => value > 2);
1797
+ // => [3, 4]
1798
+
1799
+ where([1, 2, 3, 4], (value, index) => index % 2 === 1);
1800
+ // => [2, 4]
1801
+ ```
1802
+
1803
+ #### whereNotNull
1804
+
1805
+ Filter an array or object, removing the items whose value is `null`. Only `null` is removed; an `undefined` item stays. A list comes back renumbered from 0; see [Behaviour differences](https://tolki.abe.dev/arrays-objects/differences.html#behaviour-differences).
1806
+
1807
+ Exported as `whereNotNull` from `@tolki/arr` and `@tolki/obj`, and as `dataWhereNotNull` from `@tolki/data`.
1808
+
1809
+ Laravel: [`Arr::whereNotNull()`](https://laravel.com/docs/helpers#method-array-where-not-null)
1810
+
1811
+ ```js [@tolki/arr]
1812
+ import { whereNotNull } from "@tolki/arr";
1813
+
1814
+ whereNotNull(["a", null, "b", null]);
1815
+ // => ["a", "b"]
1816
+
1817
+ whereNotNull([1, null, 2, undefined, 3]);
1818
+ // => [1, 2, undefined, 3]
1819
+ ```
1820
+
1821
+ #### wrap
1822
+
1823
+ If the given value is not already an array (an object, with `@tolki/obj`), wrap it in one. `null` gives an empty result.
1824
+
1825
+ Exported as `wrap` from `@tolki/arr` and `@tolki/obj`. `@tolki/data` has no `wrap`.
1826
+
1827
+ Laravel: [`Arr::wrap()`](https://laravel.com/docs/helpers#method-array-wrap)
1828
+
1829
+ ```js [@tolki/arr]
1830
+ import { wrap } from "@tolki/arr";
1831
+
1832
+ wrap("hello");
1833
+ // => ["hello"]
1834
+
1835
+ wrap(["hello"]);
1836
+ // => ["hello"]
1837
+
1838
+ wrap(null);
1839
+ // => []
1840
+ ```
1841
+
1842
+ ## Dot Notation
1843
+
1844
+ A dot path is a string such as `"user.address.city"` that reaches into nested arrays and objects in one call. `@tolki/arr`, `@tolki/obj`, `@tolki/data` and the `Collection` methods all read the same paths.
1845
+
1846
+ ### Path syntax
1847
+
1848
+ Each segment between the dots is an object key or an array index. A number segment picks that position of an array. A path that leads nowhere gives the default, which is `null` when you leave it out.
1849
+
1850
+ ```js [@tolki/arr]
1851
+ import { get } from "@tolki/arr";
1852
+
1853
+ get([{ items: ["a", "b"] }], "0.items.1");
1854
+ // => "b"
1855
+
1856
+ get([{ items: ["a", "b"] }], "0.items.5", "none");
1857
+ // => "none"
1858
+ ```
1859
+
1860
+ ### Keys that contain a dot
1861
+
1862
+ An object key can hold a dot of its own. `get` from `@tolki/obj` tries the whole string as a key first, and only splits it into segments when no such key exists.
1863
+
1864
+ <!-- docs/php-parity/Arr/get.json, "X26 get/has/exists resolve a literal dotted key first" -->
1865
+
1866
+ Laravel's `Arr::get()` does the same, so the port gives the same answer.
1867
+
1868
+ ```js
1869
+ import { get } from "@tolki/obj";
1870
+
1871
+ get({ "a.b": 1, a: { b: 2 } }, "a.b");
1872
+ // => 1
1873
+
1874
+ get({ a: { b: 2 } }, "a.b");
1875
+ // => 2
1876
+ ```
1877
+
1878
+ `dataGet` from `@tolki/data` also tries the literal key first, because it forwards to `get`. Laravel's `data_get()` does not: it always splits the path, so `data_get(['a.b' => 1, 'a' => ['b' => 2]], 'a.b')` is `2`. <!-- docs/php-parity/Helpers/data_get.json, "C32-D-data-get-literal-dotted-key" -->
1879
+
1880
+ ```js
1881
+ import { dataGet } from "@tolki/data";
1882
+
1883
+ dataGet({ "a.b": 1, a: { b: 2 } }, "a.b");
1884
+ // => 1
1885
+ ```
1886
+
1887
+ ### Functions that take a path
1888
+
1889
+ - **Read:** `get`, `has`, `hasAll`, `hasAny`, and the typed getters `string`, `integer`, `float`, `boolean` and `arrayItem` (`objectItem`, `dataItem`).
1890
+ - **Write:** `set`, `add`, `forget`, `pull` and `push`. `set`, `add` and `push` create the missing steps of a path as they go. `forget` leaves the data as it is when the path is missing, and `pull` returns the default, `null` unless you pass one, with the data unchanged.
1891
+ - **Pick out of rows:** `pluck` takes a path for the value and for the key, and `except` takes paths among its keys.
1892
+ - **Walk:** a path goes into lists, plain objects and `Map`s. A write keeps a `Map` a `Map`. <!-- docs/php-parity/Arr/get.json, "get-nested-out-of-order-keys"; docs/php-parity/Arr/set.json, "set-nested-append-keeps-key-order" -->
1893
+ - **Convert:** `dot` turns a nested structure into a flat object of paths, and `undot` turns it back.
1894
+
1895
+ ```js [@tolki/arr]
1896
+ import {
1897
+ add,
1898
+ dot,
1899
+ except,
1900
+ forget,
1901
+ has,
1902
+ pull,
1903
+ push,
1904
+ set,
1905
+ string,
1906
+ undot,
1907
+ } from "@tolki/arr";
1908
+
1909
+ has([["a", "b"]], "0.1");
1910
+ // => true
1911
+
1912
+ set([], "0.0", "x");
1913
+ // => [["x"]]
1914
+
1915
+ except([["a", "b"]], ["0.1"]);
1916
+ // => [["a"]]
1917
+
1918
+ string([{ name: "x" }], "0.name");
1919
+ // => "x"
1920
+
1921
+ dot([{ a: 1 }, { a: 2 }]);
1922
+ // => { "0.a": 1, "1.a": 2 }
1923
+
1924
+ undot({ "0.0": "x", 0.1: "y" });
1925
+ // => [["x", "y"]]
1926
+
1927
+ add([], "0.0", "x");
1928
+ // => [["x"]]
1929
+
1930
+ push([], "0", "x");
1931
+ // => [["x"]]
1932
+
1933
+ forget([["x"]], "5.0");
1934
+ // => [["x"]]
1935
+
1936
+ pull([["x"]], "5.0");
1937
+ // => { value: null, data: [["x"]] }
1938
+ ```
1939
+
1940
+ ### Wildcards
1941
+
1942
+ A `*` segment stands for every item at that step, and `pluck` collects what lies behind it. The result has one entry per item in front of the `*`.
1943
+
1944
+ <!-- docs/php-parity/Arr/pluck.json, "Arr::pluck wildcard path", "Arr::pluck wildcard + key" -->
1945
+
1946
+ Laravel's `Arr::pluck()` reads a `*` the same way, and the port returns the same shape.
1947
+
1948
+ ```js [@tolki/arr]
1949
+ import { pluck } from "@tolki/arr";
1950
+
1951
+ const posts = [
1952
+ { title: "A", tags: [{ id: 1 }, { id: 2 }] },
1953
+ { title: "B", tags: [{ id: 3 }] },
1954
+ ];
1955
+
1956
+ pluck(posts, "tags.*.id");
1957
+ // => [[1, 2], [3]]
1958
+
1959
+ pluck(posts, "tags.*.id", "title");
1960
+ // => { A: [1, 2], B: [3] }
1961
+ ```
1962
+
1963
+ ### Where wildcards do not work
1964
+
1965
+ Of the functions above, only `pluck` reads a `*`. `get`, `has` and their `data` versions take it as the name of a key, so a path with `*` finds nothing. The same goes for the `{first}` and `{last}` segments.
1966
+
1967
+ <!-- docs/php-parity/Helpers/data_get.json, "data-get-wildcard-and-first-last-segments" -->
1968
+
1969
+ Laravel's `data_get()` expands both of them. See [Differences from Laravel](https://tolki.abe.dev/arrays-objects/differences.html#behaviour-differences) for the full list.
1970
+
1971
+ ```js [@tolki/arr]
1972
+ import { get, has } from "@tolki/arr";
1973
+
1974
+ get([{ n: 1 }, { n: 2 }], "*.n");
1975
+ // => null
1976
+
1977
+ has([{ n: 1 }], "*.n");
1978
+ // => false
1979
+
1980
+ get([{ s: [{ x: 1 }] }], "0.s.{first}.x");
1981
+ // => null
1982
+ ```
1983
+
1984
+ ### Collections read the same paths
1985
+
1986
+ `@tolki/collection` methods that look inside items, such as `pluck`, `where`, `sortBy`, `keyBy` and `sum`, take the same paths, and `pluck` takes the same `*`. `get` is the exception: it takes one key, not a path. The method pages are in the Collections section, under [Methods List](https://tolki.abe.dev/collections/methods-list.html#pluck).
1987
+
1988
+ ```js
1989
+ import { collect } from "@tolki/collection";
1990
+
1991
+ const posts = collect([
1992
+ { title: "A", author: { id: 2 }, tags: [{ id: 1 }, { id: 2 }] },
1993
+ { title: "B", author: { id: 1 }, tags: [{ id: 3 }] },
1994
+ ]);
1995
+
1996
+ posts.pluck("tags.*.id").all();
1997
+ // => [[1, 2], [3]]
1998
+
1999
+ posts.where("author.id", 1).pluck("title").all();
2000
+ // => ["B"]
2001
+
2002
+ posts.sortBy("author.id").pluck("title").all();
2003
+ // => ["B", "A"]
2004
+
2005
+ posts.sum("author.id");
2006
+ // => 3
2007
+
2008
+ collect({ a: { b: 1 } }).get("a.b");
2009
+ // => null
2010
+ ```
2011
+
2012
+ ## PHP Semantics
2013
+
2014
+ These packages port Laravel, so where JavaScript and PHP disagree about what is equal, what is true or what a key is, the port follows PHP. That keeps a result the same as the one Laravel gives. This page lists the four places it matters. The helpers behind them live in `@tolki/utils`.
2015
+
2016
+ ### Loose and strict comparison
2017
+
2018
+ PHP's `==` is loose: it converts a value before it compares. A string that reads as a number equals that number, and `null` equals `0`, `""` and `[]`. The functions that compare values (`contains`, `search`, `unique`, `where` and others) use it, and the ones with a `Strict` twin (`containsStrict`, `whereStrict`) use PHP's `===` instead.
2019
+
2020
+ <!-- docs/php-parity/Php/loose-equal.json, "one and its numeric string", "null and zero", "null and empty string", "null and an empty array", "null and undefined-as-null", "zero and a letter", "one and zero-padded one" -->
2021
+
2022
+ `looseEqual` from `@tolki/utils` is the `==` operator. `"1" == 1` and `null == 0` are both true in PHP. PHP has no `undefined`, so the port reads it as `null`. `"abc"` does not equal `0`, but `"1"` equals `"01"`. JavaScript's own `==` gives a different answer for `null == 0` (false) and for `"1" == "01"` (false).
2023
+
2024
+ ```js
2025
+ import { looseEqual } from "@tolki/utils";
2026
+
2027
+ looseEqual("1", 1);
2028
+ // => true
2029
+
2030
+ looseEqual(null, 0);
2031
+ // => true
2032
+
2033
+ looseEqual(null, undefined);
2034
+ // => true
2035
+
2036
+ looseEqual("abc", 0);
2037
+ // => false
2038
+
2039
+ looseEqual("1", "01");
2040
+ // => true
2041
+ ```
2042
+
2043
+ The `contains` and `containsStrict` pair shows the difference on a list.
2044
+
2045
+ ```js [@tolki/arr]
2046
+ import { contains, containsStrict } from "@tolki/arr";
2047
+
2048
+ contains([1, 2, 3], "1");
2049
+ // => true
2050
+
2051
+ containsStrict([1, 2, 3], "1");
2052
+ // => false
2053
+ ```
2054
+
2055
+ On a `Collection`, `where` is loose and `whereStrict` is strict, and `where` with a `==` or `===` operator picks the same two.
2056
+
2057
+ ```js
2058
+ import { collect } from "@tolki/collection";
2059
+
2060
+ const rows = collect([{ a: 1 }, { a: "1" }, { a: 2 }]);
2061
+
2062
+ rows.where("a", 1).all();
2063
+ // => [{ a: 1 }, { a: "1" }]
2064
+
2065
+ rows.whereStrict("a", 1).all();
2066
+ // => [{ a: 1 }]
2067
+
2068
+ rows.where("a", "===", 1).all();
2069
+ // => [{ a: 1 }]
2070
+ ```
2071
+
2072
+ ### Truthiness
2073
+
2074
+ Wherever a callback or a value is tested for being true, PHP's rule applies, not JavaScript's. The tests are `filter()` with no callback, `Collection`'s `reject()` with no argument, `Collection`'s `where` without an operator, and what a callback to `first`, `where`, `reject`, `every`, `some` or `sole` answers.
2075
+
2076
+ <!-- docs/php-parity/Php/truthiness.json, "truthiness-the-string-zero", "truthiness-the-string-0.0", "truthiness-an-empty-string", "truthiness-integer-zero", "truthiness-null", "truthiness-an-empty-array", "truthiness-an-array-holding-zero", "truthiness-NAN", "truthiness-an-empty-object" -->
2077
+
2078
+ In PHP the string `"0"`, the empty string, `0`, `null`, `false` and the empty array are false. The string `"0.0"`, `NaN` and a non-empty array are true. The port keeps the same list and adds the JavaScript containers that have no PHP twin: an empty plain object, an empty `Map` and an empty `Set` are false. Any other object is true, however empty.
2079
+
2080
+ ```js
2081
+ import { collect } from "@tolki/collection";
2082
+ import { isFalsy, isPhpFalsy } from "@tolki/utils";
2083
+
2084
+ collect(["0", "0.0", "", 0, [], {}, NaN, "a", false, null]).filter().all();
2085
+ // => ["0.0", NaN, "a"]
2086
+
2087
+ collect([1, null, 0, 2]).reject().all();
2088
+ // => [null, 0]
2089
+
2090
+ collect([{ a: 1 }, { a: 0 }])
2091
+ .where("a")
2092
+ .all();
2093
+ // => [{ a: 1 }]
2094
+
2095
+ isPhpFalsy("0");
2096
+ // => true
2097
+
2098
+ isFalsy("0");
2099
+ // => false
2100
+
2101
+ isPhpFalsy(new Map());
2102
+ // => true
2103
+
2104
+ isPhpFalsy(NaN);
2105
+ // => false
2106
+ ```
2107
+
2108
+ `isFalsy` is the JavaScript-minded check, where `"0"` is true. Use `isPhpFalsy` when you want the rule the packages use.
2109
+
2110
+ A callback that answers `"0"` counts as false, and one that answers `"0.0"` counts as true.
2111
+
2112
+ <!-- docs/php-parity/Arr/callback-truthiness.json, "C32-C-arr-callback-php-truthiness" -->
2113
+
2114
+ ```js [@tolki/arr]
2115
+ import { where } from "@tolki/arr";
2116
+
2117
+ where(["a", "b"], () => "0");
2118
+ // => []
2119
+
2120
+ where(["a", "b"], () => "0.0");
2121
+ // => ["a", "b"]
2122
+ ```
2123
+
2124
+ ### Key casting
2125
+
2126
+ PHP stores a string key that reads as a plain integer, such as `"10"`, as the number `10`. A string such as `"01"` stays a string. The port does the same, so a callback gets a number for `"10"` and a string for `"01"`, and `keys` lists them that way.
2127
+
2128
+ <!-- docs/php-parity/Collection/callback-key-types.json, "C32-E-callback-key-types-sweep" -->
2129
+ <!-- docs/php-parity/Arr/set.json, "set-record-key-cast" -->
2130
+
2131
+ A callback sees the number `1` for the key `"1"` and a string for `"x"`, and `set` writes the key `"1"` as an integer but `"01"` as a string.
2132
+
2133
+ ```js [@tolki/arr]
2134
+ import { keys } from "@tolki/arr";
2135
+
2136
+ keys(["a", "b"]);
2137
+ // => [0, 1]
2138
+ ```
2139
+
2140
+ ```js
2141
+ import { map, set } from "@tolki/obj";
2142
+
2143
+ map({ 10: 1, "01": 2, x: 3 }, (value, key) => typeof key);
2144
+ // => { 10: "number", "01": "string", x: "string" }
2145
+
2146
+ set({ x: 1 }, "1", "V");
2147
+ // => { 1: "V", x: 1 }
2148
+
2149
+ set({ x: 1 }, "01", "V");
2150
+ // => { x: 1, "01": "V" }
2151
+ ```
2152
+
2153
+ `phpArrayKey` from `@tolki/utils` applies the rule to one key. Two keys that cast to the same key are one key, so the later value wins, as in a PHP array literal.
2154
+
2155
+ <!-- docs/php-parity/Arr/first.json, "first-collision" -->
2156
+
2157
+ ```js
2158
+ import { values } from "@tolki/obj";
2159
+ import { phpArrayKey } from "@tolki/utils";
2160
+
2161
+ phpArrayKey("10");
2162
+ // => 10
2163
+
2164
+ phpArrayKey("-5");
2165
+ // => -5
2166
+
2167
+ phpArrayKey("01");
2168
+ // => "01"
2169
+
2170
+ values(
2171
+ new Map([
2172
+ [1, "a"],
2173
+ ["1", "b"],
2174
+ ]),
2175
+ );
2176
+ // => ["b"]
2177
+ ```
2178
+
2179
+ ### Exceptions
2180
+
2181
+ The errors Laravel throws exist here under the same names. `@tolki/utils` exports them, and each one extends `Error`, so `instanceof` tells them apart.
2182
+
2183
+ <!-- docs/php-parity/Arr/integer.json, "integer-string-value" -->
2184
+ <!-- docs/php-parity/Arr/sole.json, "sole-none", "sole-multi-list" -->
2185
+ <!-- docs/php-parity/Collection/ensure.json, "C32-A-ensure-scalar-message" -->
2186
+ <!-- docs/php-parity/Collection/callback-truthiness.json, "C32-C-collection-callback-php-truthiness" -->
2187
+
2188
+ | Class | Thrown when |
2189
+ | ----------------------------- | ---------------------------------------------------------------------------- |
2190
+ | `InvalidArgumentException` | A typed getter (`string`, `integer`, `float`, `boolean`) finds another type. |
2191
+ | `ItemNotFoundException` | `sole` or `firstOrFail` finds no match. |
2192
+ | `MultipleItemsFoundException` | `sole` finds more than one match. It carries the number found as `count`. |
2193
+ | `UnexpectedValueException` | `Collection.ensure` meets an item of a type you did not allow. |
2194
+
2195
+ ```js
2196
+ import { sole } from "@tolki/arr";
2197
+ import { integer } from "@tolki/obj";
2198
+ import {
2199
+ InvalidArgumentException,
2200
+ ItemNotFoundException,
2201
+ MultipleItemsFoundException,
2202
+ } from "@tolki/utils";
2203
+
2204
+ sole([1, 2], () => true);
2205
+ // => throws MultipleItemsFoundException
2206
+
2207
+ integer({ a: "x" }, "a");
2208
+ // => throws InvalidArgumentException
2209
+
2210
+ let caught;
2211
+
2212
+ try {
2213
+ sole([]);
2214
+ } catch (error) {
2215
+ caught = error instanceof ItemNotFoundException;
2216
+ }
2217
+
2218
+ caught;
2219
+ // => true
2220
+
2221
+ try {
2222
+ sole([1, 2], () => true);
2223
+ } catch (error) {
2224
+ caught = error instanceof MultipleItemsFoundException && error.count;
2225
+ }
2226
+
2227
+ caught;
2228
+ // => 2
2229
+
2230
+ try {
2231
+ integer({ a: "x" }, "a");
2232
+ } catch (error) {
2233
+ caught = error instanceof InvalidArgumentException;
2234
+ }
2235
+
2236
+ caught;
2237
+ // => true
2238
+ ```
2239
+
2240
+ ## Differences from Laravel
2241
+
2242
+ `@tolki/arr`, `@tolki/obj` and `@tolki/data` follow Laravel's `Arr` class and `data_*` helpers wherever JavaScript can hold the same data. This page lists the places where they cannot, or choose not to. For the rules they keep on purpose, see [PHP Semantics](https://tolki.abe.dev/arrays-objects/php-semantics.html).
2243
+
2244
+ ### Return values instead of references
2245
+
2246
+ <!-- docs/php-parity/Arr/set.json, "set-dot-path-under-a-list-index" -->
2247
+ <!-- docs/php-parity/Arr/forget.json, "forget-products.desk.price" -->
2248
+ <!-- docs/php-parity/Arr/push.json, "push-mutates-the-caller-by-reference" -->
2249
+ <!-- docs/php-parity/Arr/pull.json, "Arr::pull — first-level key containing dots" -->
2250
+
2251
+ Laravel's `Arr::set()`, `forget()`, `push()` and `pull()` take the array by reference and change it. JavaScript cannot pass a variable that way, so these functions return the result and leave what you passed in unchanged. Assign the result to keep it.
2252
+
2253
+ - `set` returns the array or object with the value written. <!-- docs/php-parity/Arr/set.json, "set-dot-path-under-a-list-index" -->
2254
+ - `forget` returns it with the path removed. <!-- docs/php-parity/Arr/forget.json, "forget-products.desk.price" -->
2255
+ - `push` returns it with the values appended to the array at the path. <!-- docs/php-parity/Arr/push.json, "push-mutates-the-caller-by-reference" -->
2256
+ - `pull` returns `{ value, data }`: the item taken out, and the data without it. <!-- docs/php-parity/Arr/pull.json, "Arr::pull — first-level key containing dots" -->
2257
+
2258
+ ```js [@tolki/arr]
2259
+ import { forget, pull, push, set } from "@tolki/arr";
2260
+
2261
+ const grid = [["a", "b"], ["c"]];
2262
+
2263
+ set(grid, "0.1", "z");
2264
+ // => [["a", "z"], ["c"]]
2265
+
2266
+ forget(grid, "0.1");
2267
+ // => [["a"], ["c"]]
2268
+
2269
+ push(grid, "1", "d");
2270
+ // => [["a", "b"], ["c", "d"]]
2271
+
2272
+ pull(grid, "0.0");
2273
+ // => { value: "a", data: [["b"], ["c"]] }
2274
+
2275
+ grid;
2276
+ // => [["a", "b"], ["c"]]
2277
+ ```
2278
+
2279
+ `shift`, `pop`, `splice` and `unshift` are different. Laravel gets them from `Collection`, which changes itself, and the functions keep that: they change the array or object you pass and return what came out (or the changed value, for `unshift`).
2280
+
2281
+ <!-- docs/php-parity/Collection/shift.json, "X2 shift mutates and returns the first value" -->
2282
+ <!-- docs/php-parity/Collection/pop.json, "X1 pop mutates and returns the last value" -->
2283
+ <!-- docs/php-parity/Collection/splice.json, "X6 splice mutates and returns the removed items" -->
2284
+ <!-- docs/php-parity/Collection/unshift.json, "X5 unshift mutates and renumbers integer keys" -->
2285
+
2286
+ ```js [@tolki/arr]
2287
+ import { pop, shift, splice, unshift } from "@tolki/arr";
2288
+
2289
+ const queue = [1, 2, 3, 4, 5];
2290
+
2291
+ shift(queue);
2292
+ // => 1
2293
+
2294
+ pop(queue);
2295
+ // => 5
2296
+
2297
+ splice(queue, 1, 1);
2298
+ // => [3]
2299
+
2300
+ unshift(queue, 0);
2301
+ // => [0, 2, 4]
2302
+
2303
+ queue;
2304
+ // => [0, 2, 4]
2305
+ ```
2306
+
2307
+ ### Behaviour differences
2308
+
2309
+ - `float` accepts a whole number such as `1`, where Laravel's `Arr::float()` throws, because PHP's `is_float()` rejects an integer. JavaScript has one number type. <!-- docs/php-parity/Arr/float.json, "Arr::float rejects a whole-number int" -->
2310
+ - `get`, `has` and the other path readers take `*`, `{first}` and `{last}` as plain key names, where Laravel's `data_get()` expands them. `pluck` does read a `*`. <!-- docs/php-parity/Helpers/data_get.json, "data-get-wildcard-and-first-last-segments" -->
2311
+ - On a list, `where`, `reject` and `whereNotNull` return the kept values as a new array numbered from 0, where Laravel keeps each value's original key. An object keeps its keys. <!-- docs/php-parity/Arr/where.json, "where-preserves-int-keys"; docs/php-parity/Arr/reject.json, "reject-preserves-int-keys"; docs/php-parity/Arr/whereNotNull.json, "whereNotNull-list-preserves-keys" -->
2312
+ - The functions Laravel takes from `Collection` give the same list result: `filter`, `intersectAssoc`, `intersectByKeys`, `diffKeys`, `reverse`, `skipUntil` and `skipWhile` return a list numbered from 0 where Laravel keeps each value's original key. `chunk`, `chunkBy` and `chunkWhile` renumber the values inside each chunk, where Laravel keeps their keys. `diff`, `diffAssoc`, `intersect` and `slice` return a fresh list in the same way. `@tolki/obj` keeps the keys of an object. <!-- docs/php-parity/Collection/filter.json, "filter-list-gap-keys"; docs/php-parity/Collection/intersectAssoc.json, "intersectAssoc-list-keeps-gap-keys"; docs/php-parity/Collection/intersectByKeys.json, "intersectByKeys-list-keeps-gap-keys"; docs/php-parity/Collection/diffKeys.json, "C32-F-diffKeys-signature-examples"; docs/php-parity/Collection/reverse.json, "reverse preserves keys and reverses entry order"; docs/php-parity/Collection/skipUntil.json, "C32-D-skipUntil-list-keys"; docs/php-parity/Collection/skipWhile.json, "C32-D-skipWhile-list-keys"; docs/php-parity/Collection/chunk.json, "collection-chunk-last-chunk-keys"; docs/php-parity/Collection/chunkBy.json, "chunkBy with a callback"; docs/php-parity/Collection/chunkWhile.json, "chunkWhile on equal adjacent elements"; docs/php-parity/Collection/diff.json, "diff on array-backed collection"; docs/php-parity/Collection/diffAssoc.json, "diffAssoc on array-backed collection"; docs/php-parity/Collection/intersect.json, "intersect on array-backed collection"; docs/php-parity/Collection/slice.json, "X15 slice with a negative offset and a length" -->
2313
+ - `dataGet` reads a literal key that contains a dot before it splits the path, so `dataGet({ "a.b": 1, a: { b: 2 } }, "a.b")` is `1`, where Laravel's `data_get()` is `2`. When the literal key is the only one, `dataGet` still finds it and `data_get()` returns the default. `get` in `@tolki/obj` does the same. <!-- docs/php-parity/Helpers/data_get.json, "C32-D-data-get-literal-dotted-key" -->
2314
+ - A plain object lists integer-like keys first, in ascending order, whatever order you wrote them. Laravel's arrays keep insertion order. A `Map` keeps insertion order, so pass one when the order matters. <!-- docs/php-parity/Arr/divide.json, "divide-mixed" -->
2315
+ - With a final glue and a single item, `join` returns that item cast to a string, where Laravel returns the item as it is: `1` stays the number `1`. <!-- docs/php-parity/Arr/join.json, "C32-H-arr-join-pieces" -->
2316
+
2317
+ ```js
2318
+ import { chunk, filter, float, intersectAssoc, join, where } from "@tolki/arr";
2319
+ import { dataGet } from "@tolki/data";
2320
+ import { divide, filter as filterObject, get, reject } from "@tolki/obj";
2321
+
2322
+ float([{ price: 1 }], "0.price");
2323
+ // => 1
2324
+
2325
+ get({ a: { n: 1 }, b: { n: 2 } }, "*.n");
2326
+ // => null
2327
+
2328
+ where(
2329
+ ["100", "200", "300", "400"],
2330
+ (value) => value === "200" || value === "400",
2331
+ );
2332
+ // => ["200", "400"]
2333
+
2334
+ reject({ a: 1, b: 2, c: 3 }, (value) => value > 1);
2335
+ // => { a: 1 }
2336
+
2337
+ filter([1, 2, 3], (value) => value !== 2);
2338
+ // => [1, 3]
2339
+
2340
+ filterObject({ 0: 1, 1: 2, 2: 3 }, (value) => value !== 2);
2341
+ // => { 0: 1, 2: 3 }
2342
+
2343
+ intersectAssoc(["a", "b", "c"], ["a", "x", "c"]);
2344
+ // => ["a", "c"]
2345
+
2346
+ chunk([1, 2, 3, 4, 5], 2);
2347
+ // => [[1, 2], [3, 4], [5]]
2348
+
2349
+ dataGet({ "a.b": 1, a: { b: 2 } }, "a.b");
2350
+ // => 1
2351
+
2352
+ dataGet({ "a.b": 1 }, "a.b", "miss");
2353
+ // => 1
2354
+
2355
+ get({ "a.b": 1, a: { b: 2 } }, "a.b");
2356
+ // => 1
2357
+
2358
+ divide({ x: 1, 0: 2 });
2359
+ // => [[0, "x"], [2, 1]]
2360
+
2361
+ divide(
2362
+ new Map([
2363
+ ["x", 1],
2364
+ [0, 2],
2365
+ ]),
2366
+ );
2367
+ // => [["x", 0], [1, 2]]
2368
+
2369
+ join([1], ", ", " and ");
2370
+ // => "1"
2371
+ ```
2372
+
2373
+ ### Not ported
2374
+
2375
+ These Laravel helpers have no function in the packages.
2376
+
2377
+ - `Arr::isAssoc()` and `Arr::isList()`: in none of the three. `Array.isArray()` tells a list from an object.
2378
+ - `data_fill()`, and the global `head()` and `last()` helpers. `first` and `last` from the packages are `Arr::first()` and `Arr::last()`.
2379
+ - `accessible`, `arrayable` and `wrap` in `@tolki/data`. `@tolki/arr` and `@tolki/obj` have them.
2380
+
2381
+ ```js
2382
+ import * as arr from "@tolki/arr";
2383
+ import * as data from "@tolki/data";
2384
+ import * as obj from "@tolki/obj";
2385
+
2386
+ "isList" in arr || "isAssoc" in obj || "dataFill" in data;
2387
+ // => false
2388
+
2389
+ "dataAccessible" in data || "dataArrayable" in data || "dataWrap" in data;
2390
+ // => false
2391
+ ```
2392
+
2393
+ ### Renamed
2394
+
2395
+ - `Arr::array()` is `arrayItem` in `@tolki/arr`, `objectItem` in `@tolki/obj` and `dataItem` in `@tolki/data`. `array` is too generic a name to export.
2396
+ - `Arr::arrayable()` is `arrayable` in `@tolki/arr` and `objectifiable` in `@tolki/obj`.
2397
+ - `data_get()`, `data_set()` and `data_forget()` are `dataGet`, `dataSet` and `dataForget`. Every other function in `@tolki/data` takes the same `data` prefix, so `Arr::pluck()` is `dataPluck`.
2398
+
2399
+ ```js [@tolki/arr]
2400
+ import { arrayItem } from "@tolki/arr";
2401
+
2402
+ arrayItem([{ tags: ["a"] }], "0.tags");
2403
+ // => ["a"]
2404
+ ```
2405
+
2406
+ ### JavaScript-only additions
2407
+
2408
+ - Functions Laravel has on `Collection` rather than on `Arr`. All three packages export `chunk`, `chunkBy`, `chunkWhile`, `combine`, `contains`, `containsStrict`, the `diff` and `intersect` families, `filter`, `flip`, `keys`, `pad`, `pop`, `replace`, `replaceRecursive`, `reverse`, `shift`, `skipUntil`, `skipWhile`, `slice`, `splice`, `takeUntil`, `takeWhile`, `union`, `unshift` and `values`. `@tolki/data` also has `dataCount`, `dataSearch`, `dataBefore` and `dataAfter`. <!-- docs/php-parity/Collection/search.json, "search-callback"; docs/php-parity/Collection/before.json, "before-array-needle"; docs/php-parity/Collection/after.json, "after-array-needle"; docs/php-parity/Collection/count.json, "count-empty" -->
2409
+ - `flattenDot` in `@tolki/obj` flattens like `dot` but drops an empty nested object or array, where `dot` keeps it as a value, as Laravel's `Arr::dot()` does. <!-- docs/php-parity/Arr/dot.json, "dot-empty-leaf", "dot-nested-empty-leaf" -->
2410
+ - `dataUndot(data, true)` builds a list from paths made of whole numbers such as `"0.1"`, as `undot` from `@tolki/arr` always does. It throws a `TypeError` for any other path.
2411
+ - Every function that takes a dot path reads and writes a `Map` at any depth as Laravel reads a nested array, and `dot` and `flattenDot` walk one the same way. A write keeps the `Map` a `Map` and keeps its key order. Helpers that recurse through values, such as `flatten`, `sortRecursive` and `replaceRecursive`, do not walk into a nested `Map`. When the whole data is a `Map`, a path function that writes, such as `set`, `forget` or `push`, returns a plain object. <!-- docs/php-parity/Arr/set.json, "set-nested-append-keeps-key-order" -->
2412
+ - `@tolki/data` reads a `Set` and a generator as a list.
2413
+
2414
+ ```js
2415
+ import {
2416
+ dataAfter,
2417
+ dataBefore,
2418
+ dataCount,
2419
+ dataFrom,
2420
+ dataGet,
2421
+ dataSearch,
2422
+ dataUndot,
2423
+ } from "@tolki/data";
2424
+ import { dot, flatten, flattenDot, set } from "@tolki/obj";
2425
+
2426
+ dataCount({ a: 1, b: 2 });
2427
+ // => 2
2428
+
2429
+ dataSearch([1, 2, 3], 2);
2430
+ // => 1
2431
+
2432
+ dataBefore([1, 2, 3], 3);
2433
+ // => 2
2434
+
2435
+ dataAfter([1, 2, 3], 1);
2436
+ // => 2
2437
+
2438
+ dot({ a: {}, b: { c: 1 } });
2439
+ // => { a: {}, "b.c": 1 }
2440
+
2441
+ flattenDot({ a: {}, b: { c: 1 } });
2442
+ // => { "b.c": 1 }
2443
+
2444
+ dataUndot({ "0.0": "x", 0.1: "y" }, true);
2445
+ // => [["x", "y"]]
2446
+
2447
+ dataFrom(new Set([1, 2]));
2448
+ // => [1, 2]
2449
+
2450
+ dataGet(new Map([["a", { b: 1 }]]), "a.b");
2451
+ // => 1
2452
+
2453
+ set(
2454
+ {
2455
+ m: new Map([
2456
+ [2, "c"],
2457
+ [0, "a"],
2458
+ ]),
2459
+ },
2460
+ "m.1",
2461
+ "b",
2462
+ );
2463
+ // => { m: new Map([[2, "c"], [0, "a"], [1, "b"]]) }
2464
+
2465
+ flatten({ m: new Map([["a", 1]]) });
2466
+ // => [new Map([["a", 1]])]
2467
+ ```
2468
+
2469
+ <!-- AUTO-GENERATED-DOCS:END -->