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