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