@luminix/core 0.0.1-beta.9 → 0.2.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.
Files changed (103) hide show
  1. package/README.md +0 -28
  2. package/dist/core.js +1042 -1925
  3. package/package.json +5 -9
  4. package/types/contracts/Builder.d.ts +46 -0
  5. package/types/contracts/ModelCollection.d.ts +7 -0
  6. package/{dist → types}/contracts/Plugin.d.ts +3 -3
  7. package/{dist → types}/contracts/Relation/BelongsTo.d.ts +3 -5
  8. package/{dist → types}/contracts/Relation/BelongsToMany.d.ts +7 -9
  9. package/{dist → types}/contracts/Relation/HasMany.d.ts +6 -7
  10. package/{dist → types}/contracts/Relation/HasOne.d.ts +3 -4
  11. package/{dist → types}/contracts/Relation/HasOneOrMany.d.ts +0 -1
  12. package/{dist → types}/contracts/Relation/MorphMany.d.ts +6 -7
  13. package/{dist → types}/contracts/Relation/MorphOne.d.ts +3 -4
  14. package/{dist → types}/contracts/Relation/MorphOneOrMany.d.ts +0 -1
  15. package/{dist → types}/contracts/Relation/MorphTo.d.ts +0 -1
  16. package/types/contracts/Relation/MorphToMany.d.ts +8 -0
  17. package/{dist → types}/contracts/Relation.d.ts +12 -9
  18. package/types/extensions.d.ts +10 -0
  19. package/types/facades/App.d.ts +8 -0
  20. package/types/facades/Auth.d.ts +7 -0
  21. package/types/facades/Config.d.ts +7 -0
  22. package/types/facades/Error.d.ts +10 -0
  23. package/types/facades/Http.d.ts +8 -0
  24. package/types/facades/Log.d.ts +7 -0
  25. package/types/facades/Model.d.ts +8 -0
  26. package/types/facades/Route.d.ts +8 -0
  27. package/types/helpers/app.d.ts +4 -0
  28. package/{dist → types}/helpers/auth.d.ts +0 -1
  29. package/types/helpers/collect.d.ts +2 -0
  30. package/{dist → types}/helpers/config.d.ts +0 -1
  31. package/{dist → types}/helpers/error.d.ts +0 -1
  32. package/types/helpers/log.d.ts +5 -0
  33. package/{dist → types}/helpers/model.d.ts +2 -3
  34. package/{dist → types}/helpers/route.d.ts +2 -2
  35. package/types/index.d.ts +26 -0
  36. package/types/mixins/BaseModel.d.ts +7 -0
  37. package/types/providers/LuminixServiceProvider.d.ts +8 -0
  38. package/types/services/AuthService.d.ts +18 -0
  39. package/{dist/facades/Error.d.ts → types/services/ErrorService.d.ts} +2 -6
  40. package/types/services/HttpService.d.ts +21 -0
  41. package/{dist/facades/Log.d.ts → types/services/LogService.d.ts} +2 -4
  42. package/types/services/ModelService.d.ts +21 -0
  43. package/types/services/RouteService.d.ts +21 -0
  44. package/{dist → types}/support/model.d.ts +1 -1
  45. package/types/types/App.d.ts +88 -0
  46. package/{dist → types}/types/Auth.d.ts +0 -1
  47. package/types/types/Builder.d.ts +46 -0
  48. package/{dist → types}/types/Config.d.ts +4 -2
  49. package/types/types/Error.d.ts +17 -0
  50. package/types/types/Http.d.ts +20 -0
  51. package/{dist → types}/types/Model.d.ts +61 -43
  52. package/{dist → types}/types/Relation.d.ts +1 -3
  53. package/types/types/Route.d.ts +28 -0
  54. package/{dist → types}/types/Support.d.ts +3 -2
  55. package/.eslintignore +0 -3
  56. package/.eslintrc.cjs +0 -39
  57. package/dist/contracts/Builder.d.ts +0 -53
  58. package/dist/contracts/Collection.d.ts +0 -190
  59. package/dist/contracts/ModelCollection.d.ts +0 -9
  60. package/dist/contracts/PropertyBag.d.ts +0 -21
  61. package/dist/contracts/Relation/MorphToMany.d.ts +0 -16
  62. package/dist/facades/App.d.ts +0 -37
  63. package/dist/facades/Auth.d.ts +0 -14
  64. package/dist/facades/Model.d.ts +0 -41
  65. package/dist/facades/Route.d.ts +0 -32
  66. package/dist/helpers/app.d.ts +0 -5
  67. package/dist/helpers/collect.d.ts +0 -1
  68. package/dist/helpers/log.d.ts +0 -5
  69. package/dist/index.d.ts +0 -24
  70. package/dist/mixins/BaseModel.d.ts +0 -5
  71. package/dist/mixins/HasEvents.d.ts +0 -15
  72. package/dist/mixins/Reducible.d.ts +0 -17
  73. package/dist/support/collection.d.ts +0 -4
  74. package/dist/support/reader.d.ts +0 -2
  75. package/dist/support/searchParams.d.ts +0 -1
  76. package/dist/types/App.d.ts +0 -79
  77. package/dist/types/Builder.d.ts +0 -51
  78. package/dist/types/Collection.d.ts +0 -1804
  79. package/dist/types/Error.d.ts +0 -31
  80. package/dist/types/Event.d.ts +0 -14
  81. package/dist/types/PropertyBag.d.ts +0 -25
  82. package/dist/types/Reducer.d.ts +0 -17
  83. package/dist/types/Route.d.ts +0 -22
  84. package/index.html +0 -13
  85. package/jest.config.js +0 -15
  86. package/tsconfig.json +0 -25
  87. package/vite.config.js +0 -20
  88. /package/{dist → types}/exceptions/AttributeNotFillableException.d.ts +0 -0
  89. /package/{dist → types}/exceptions/FacadeNotFoundException.d.ts +0 -0
  90. /package/{dist → types}/exceptions/MethodNotImplementedException.d.ts +0 -0
  91. /package/{dist → types}/exceptions/ModelInvalidRelatedTypeException.d.ts +0 -0
  92. /package/{dist → types}/exceptions/ModelNotFoundException.d.ts +0 -0
  93. /package/{dist → types}/exceptions/ModelNotPersistedException.d.ts +0 -0
  94. /package/{dist → types}/exceptions/ModelWithoutPrimaryKeyException.d.ts +0 -0
  95. /package/{dist → types}/exceptions/NoEmbedException.d.ts +0 -0
  96. /package/{dist → types}/exceptions/NoInverseRelationException.d.ts +0 -0
  97. /package/{dist → types}/exceptions/NotModelException.d.ts +0 -0
  98. /package/{dist → types}/exceptions/NotReducibleException.d.ts +0 -0
  99. /package/{dist → types}/exceptions/ReducerOverrideException.d.ts +0 -0
  100. /package/{dist → types}/exceptions/RouteNotFoundException.d.ts +0 -0
  101. /package/{dist → types}/exceptions/UnsupportedRelationException.d.ts +0 -0
  102. /package/{dist → types}/types/Log.d.ts +0 -0
  103. /package/{dist → types}/types/Plugin.d.ts +0 -0
@@ -1,1804 +0,0 @@
1
- import { Event, EventSource } from './Event';
2
- import { Constructor, TypeOf, JsonValue } from './Support';
3
-
4
- export type CollectionEvents<T = unknown> = {
5
- 'change': (e: CollectionChangeEvent<T>) => void;
6
- };
7
- export type CollectionChangeEvent<T = unknown> = Event<Collection<T>> & {
8
- items: T[];
9
- };
10
- export type Operator = '=' | '!=' | '>' | '>=' | '<' | '<=';
11
- export type CollectionIteratorCallback<T = unknown, R = void> = (value: T, index: number, collection: Collection<T>) => R;
12
- export type CollectionPipeCallback<T = unknown, R = unknown> = (collection: Collection<T>) => R;
13
- export type CollectionSortCallback<T = unknown> = (a: T, b: T) => number;
14
- export type Collection<T = unknown> = EventSource<CollectionEvents<T>> & {
15
- [Symbol.iterator](): Iterator<T>;
16
- get items(): T[];
17
- /**
18
- *
19
- * The `all` method returns the underlying array represented by the collection.
20
- *
21
- *
22
- */
23
- all(): T[];
24
- /**
25
- *
26
- * Alias for the `avg` method.
27
- *
28
- */
29
- average(): number;
30
- average<K extends keyof T>(key: K): number;
31
- /**
32
- * The `avg` method returns the average value of a given key.
33
- *
34
- * ```js
35
- * collect([1, 2, 3, 4, 5]).avg();
36
- * // 3
37
- * collect([{ value: 1 }, { value: 2 }, { value: 3 }]).avg('value');
38
- * // 2
39
- * ```
40
- *
41
- */
42
- avg(): number;
43
- avg<K extends keyof T>(key: K): number;
44
- /**
45
- *
46
- * The `chunk` method breaks the collection into multiple, smaller collections of a given size:
47
- *
48
- * ```js
49
- * collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).chunk(3);
50
- * // [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
51
- * ```
52
- *
53
- * This method is especially useful in views when working with a grid system such as Bootstrap.
54
- * For example, imagine you have a collection of Luminix models that you want to display in a grid, using React:
55
- *
56
- * ```jsx
57
- * <div className="container">
58
- * {products.chunk(3).map((chunk, rowIndex) => (
59
- * <div key={rowIndex} className="row">
60
- * {chunk.map((product, colIndex) => (
61
- * <div key={colIndex} className="col">
62
- * {product.name}
63
- * </div>
64
- * ))}
65
- * </div>
66
- * ))}
67
- * </div>
68
- * ```
69
- *
70
- */
71
- chunk(size: number): Collection<Collection<T>>;
72
- /**
73
- *
74
- * The `chunkWhile` method breaks the collection into multiple, smaller collections based
75
- * on the evaluation of the given callback. The `chunk`variable passed to the closure may
76
- * be used to inspect the previous elements in the current chunk:
77
- *
78
- * ```js
79
- * const collection = collect('AABBCCCCD'.split(''));
80
- * const chunks = collection.chunkWhile((value, index, chunk) => {
81
- * return value === chunk.last();
82
- * });
83
- *
84
- * chunks.all();
85
- * // [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]
86
- * ```
87
- *
88
- */
89
- chunkWhile(callback: CollectionIteratorCallback<T, boolean>): Collection<Collection<T>>;
90
- /**
91
- *
92
- * The `collapse` method collapses a collection of arrays into a single, flat collection:
93
- *
94
- * ```js
95
- * collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).collapse();
96
- * // [1, 2, 3, 4, 5, 6, 7, 8, 9]
97
- * ```
98
- *
99
- */
100
- collapse(): Collection<unknown>;
101
- /**
102
- *
103
- * The `collect` method creates a new `Collection` instance with the items
104
- * currently in the collection:
105
- *
106
- */
107
- collect(): Collection<T>;
108
- /**
109
- * The `combine` method combines the values of the collection, as keys,
110
- * with the values of another array or collection:
111
- *
112
- * ```js
113
- * collect(['name', 'age']).combine(['George', 29]);
114
- * // { name: 'George', age: 29 }
115
- * ```
116
- *
117
- */
118
- combine(values: Collection<JsonValue> | JsonValue[]): Record<string, JsonValue>;
119
- /**
120
- * The `concat` method appends the given array or collection's values
121
- * onto the end of another collection:
122
- *
123
- * ```js
124
- * collect([1, 2, 3]).concat([4, 5, 6]);
125
- * // [1, 2, 3, 4, 5, 6]
126
- * ```
127
- *
128
- * The `concat` method will not modify the original collection:
129
- *
130
- */
131
- concat(collection: Collection<unknown> | unknown[]): Collection<unknown>;
132
- /**
133
- * The `contains` method determines whether the collection contains a given item.
134
- * You may pass a closure to the `contains` method to determine if an element
135
- * exists in the collection matching a given truth test:
136
- *
137
- * ```js
138
- * collect([1, 2, 3, 4, 5]).contains(3);
139
- * // true
140
- *
141
- * collect([1, 2, 3, 4, 5]).contains(value => value > 5);
142
- * // false
143
- * ```
144
- *
145
- * You may also pass a key / value pair to the `contains` method, which will determine
146
- * if the given pair exists in the collection:
147
- *
148
- * ```js
149
- * const collection = collect([
150
- * { name: 'Desk', price: 200 },
151
- * { name: 'Chair', price: 100 },
152
- * ]);
153
- *
154
- * collection.contains('name', 'Desk');
155
- * // true
156
- * ```
157
- *
158
- * The `contains` method uses "loose" comparisons when checking values, meaning a string
159
- * with an integer value will be considered equal to an integer with the same numeric value.
160
- * Use the `containsStrict` method to filter using strict comparisons.
161
- *
162
- * For the inverse of the `contains` method, see the `doesntContain` method.
163
- *
164
- */
165
- contains(value: T): boolean;
166
- contains(key: keyof T, value: T): boolean;
167
- contains(callback: CollectionIteratorCallback<T, boolean>): boolean;
168
- /**
169
- *
170
- * The `containsOneItem` method determnines whether the collection contains
171
- * a single item:
172
- *
173
- */
174
- containsOneItem(): boolean;
175
- /**
176
- *
177
- * This method has the same signature as the `contains` method; however
178
- * all values are compared using strict comparisons.
179
- *
180
- */
181
- containsStrict(value: T): boolean;
182
- containsStrict(key: keyof T, value: T): boolean;
183
- containsStrict(callback: CollectionIteratorCallback<T, boolean>): boolean;
184
- /**
185
- *
186
- * The `count` method returns the total number of items in the collection:
187
- *
188
- * ```js
189
- * collect([1, 2, 3, 4]).count();
190
- * // 4
191
- * ```
192
- *
193
- */
194
- count(): number;
195
- /**
196
- *
197
- * The `countBy` method counts the occurrences of values in the collection.
198
- * By default, the method counts the occurrences of every element, allowing
199
- * you to count certain "types" of elements in the collection:
200
- *
201
- * ```js
202
- * const collection = collect([1, 2, 2, 2, 3]);
203
- *
204
- * const counted = collection.countBy();
205
- *
206
- * // { 1: 1, 2: 3, 3: 1 }
207
- * ```
208
- *
209
- * You may also pass a callback to the `countBy` method to count all items by
210
- * a custom value:
211
- *
212
- * ```js
213
- * const collection = collect(['alice@gmail.com', 'bob@yahoo.com', 'carlos@gmail.com']);
214
- *
215
- * const counted = collection.countBy((value) => value.split('@')[1]);
216
- *
217
- * // { 'gmail.com': 2, 'yahoo.com': 1 }
218
- * ```
219
- *
220
- */
221
- countBy(callback?: CollectionIteratorCallback<T, string | number>): Record<string | number, number>;
222
- /**
223
- *
224
- * The `crossJoin` method cross joins the collection's values among the given arrays or collections,
225
- * returning a Cartesian product with all possible permutations:
226
- *
227
- * ```js
228
- * collect([1, 2]).crossJoin(['a', 'b']);
229
- *
230
- * // [
231
- * // [1, 'a'],
232
- * // [1, 'b'],
233
- * // [2, 'a'],
234
- * // [2, 'b'],
235
- * // ]
236
- *
237
- * collect([1, 2]).crossJoin(['a', 'b'], ['I', 'II']);
238
- *
239
- * // [
240
- * // [1, 'a', 'I'],
241
- * // [1, 'a', 'II'],
242
- * // [1, 'b', 'I'],
243
- * // [1, 'b', 'II'],
244
- * // [2, 'a', 'I'],
245
- * // [2, 'a', 'II'],
246
- * // [2, 'b', 'I'],
247
- * // [2, 'b', 'II'],
248
- * // ]
249
- *
250
- * ```
251
- *
252
- */
253
- crossJoin<V>(...collections: (Collection<V> | V[])[]): Collection<(T | V)[]>;
254
- /**
255
- *
256
- * The `diff` method compares the collection against another collection or a plain array
257
- * based on its values. This method will return the values in the original collection that
258
- * are not present in the given collection:
259
- *
260
- * ```js
261
- * collect([1, 2, 3, 4, 5]).diff([2, 4, 6, 8]);
262
- * // [1, 3, 5]
263
- * ```
264
- *
265
- */
266
- diff(values: Collection<T> | T[]): Collection<T>;
267
- /**
268
- *
269
- * The `doesntContain` method determines whether the collection does not contain a given item.
270
- * You may pass a closure to the `doesntContain` method to determine if an element
271
- * does not exist in the collection matching a given truth test:
272
- *
273
- * ```js
274
- * collect([1, 2, 3, 4, 5]).doesntContain(3);
275
- * // false
276
- *
277
- * collect([1, 2, 3, 4, 5]).doesntContain(value => value > 5);
278
- * // true
279
- *
280
- * ```
281
- *
282
- * You may also pass a key / value pair to the `doesntContain` method, which will determine
283
- * if the given pair does not exist in the collection:
284
- *
285
- * ```js
286
- * const collection = collect([
287
- * { name: 'Desk', price: 200 },
288
- * { name: 'Chair', price: 100 },
289
- * ]);
290
- *
291
- * collection.doesntContain('name', 'Desk');
292
- * // false
293
- *
294
- * collection.doesntContain('name', 'Bookcase');
295
- * // true
296
- * ```
297
- *
298
- * The `doesntContain` method uses "loose" comparisons when checking values, meaning a string
299
- * with an integer value will be considered equal to an integer with the same numeric value.
300
- *
301
- */
302
- doesntContain(value: T): boolean;
303
- doesntContain(key: keyof T, value: T): boolean;
304
- doesntContain(callback: CollectionIteratorCallback<T, boolean>): boolean;
305
- /**
306
- *
307
- * The `dump` method dumps the collection's items:
308
- *
309
- * ```js
310
- * collect([1, 2, 3, 4, 5]).dump();
311
- * // Collection [1, 2, 3, 4, 5]
312
- *
313
- * collect([{ name: 'John Doe' }, { name: 'Jane Doe' }]).dump();
314
- *
315
- * // Collection [
316
- * // { name: 'John Doe' },
317
- * // { name: 'Jane Doe' },
318
- * // ]
319
- * ```
320
- *
321
- */
322
- dump(): void;
323
- /**
324
- *
325
- * The `duplicates` method retrieves and returns duplicate values from the collection:
326
- *
327
- * ```js
328
- * collect([1, 2, 2, 3, 4, 4, 5]).duplicates();
329
- * // [2, 4]
330
- * ```
331
- *
332
- * If the collection contains objects, you may pass a key of the attributes
333
- * that you wish to check for duplicate values:
334
- *
335
- * ```js
336
- * const employees = collect([
337
- * { id: 1, name: 'John Doe', position: 'Developer' },
338
- * { id: 2, name: 'Jane Doe', position: 'Designer' },
339
- * { id: 3, name: 'Johnny Doe', position: 'Developer' },
340
- * ]);
341
- *
342
- * employees.duplicates('position');
343
- * // ['Developer']
344
- *
345
- * ```
346
- *
347
- * The `duplicates` method uses "loose" comparisons when checking values, meaning a string
348
- * with an integer value will be considered equal to an integer with the same numeric value.
349
- *
350
- */
351
- duplicates(): Collection<T>;
352
- duplicates<K extends keyof T>(key: K): Collection<T[K]>;
353
- /**
354
- *
355
- * The `duplicatesStrict` method has the same signature as the `duplicates` method;
356
- * however, all values are compared using strict comparisons.
357
- *
358
- */
359
- duplicatesStrict(): Collection<T>;
360
- duplicatesStrict<K extends keyof T>(key: K): Collection<T[K]>;
361
- /**
362
- *
363
- * The `each` method iterates over the items in the collection and passes each item to a callback:
364
- *
365
- * ```js
366
- * collect([1, 2, 3]).each((value, index) => {
367
- * console.log(value, index);
368
- * });
369
- * ```
370
- *
371
- * If you would like to stop iterating through the items, you may return `false` from the callback:
372
- *
373
- * ```js
374
- * collect([1, 2, 3]).each((value, index) => {
375
- * console.log(value, index);
376
- * if (value === 2) {
377
- * return false;
378
- * }
379
- * });
380
- * // 1, 0, 2, 1
381
- * ```
382
- *
383
- */
384
- each(callback: CollectionIteratorCallback<T, void | false>): Collection<T>;
385
- /**
386
- *
387
- * The `eachSpread` method iterates over the items in the collection,
388
- * passing each nested item to a callback:
389
- *
390
- * ```js
391
- * collect([
392
- * ['John Doe', 36],
393
- * ['Jane Doe', 34],
394
- * ]).eachSpread((name, age) => {
395
- * // ...
396
- * });
397
- * ```
398
- *
399
- * You may stop iterating through the items by returning `false` from the callback:
400
- *
401
- * ```js
402
- * collection.eachSpread((name, age) => {
403
- * return false;
404
- * });
405
- * ```
406
- *
407
- */
408
- eachSpread(callback: (...args: unknown[]) => void | false): Collection<T>;
409
- /**
410
- *
411
- * The `ensure` method may be used to verify that all elements of
412
- * a collection are of a given type or list of types. Otherwise
413
- * an error will be thrown:
414
- *
415
- * ```js
416
- * return collection.ensure(User);
417
- *
418
- * return collection.ensure([User, Customer]);
419
- * ```
420
- *
421
- * Primitive types may also be passed to the `ensure` method:
422
- *
423
- * ```js
424
- * return collection.ensure('string');
425
- * ```
426
- *
427
- */
428
- ensure(type: TypeOf | Constructor | (TypeOf | Constructor)[]): Collection<T>;
429
- /**
430
- *
431
- * The `every` method may be used to verify that all elements of
432
- * a collection pass a given truth test:
433
- *
434
- * ```js
435
- * collect([1, 2, 3, 4, 5]).every((value, index) => value > 2);
436
- * // false
437
- * ```
438
- *
439
- * If the collection is empty, the `every` method will return `true`.
440
- *
441
- */
442
- every(callback: CollectionIteratorCallback<T, boolean>): boolean;
443
- /**
444
- *
445
- * The `except` method returns all items in the collection except for those
446
- * with the specified indexes:
447
- *
448
- * ```js
449
- * collect([1, 2, 3, 4, 5]).except([0, 4]);
450
- * // [2, 3, 4]
451
- * ```
452
- *
453
- * For the inverse of `except`, see the `only` method.
454
- *
455
- */
456
- except(indexes: number[]): Collection<T>;
457
- /**
458
- *
459
- * The `filter` method filters the collection using the given callback, keeping only those items that pass a given truth test:
460
- *
461
- * ```js
462
- * collect([1, 2, 3, 4, 5]).filter(value => value > 2);
463
- *
464
- * // [3, 4, 5]
465
- * ```
466
- *
467
- * If no callback is provided, all values that are equivalent to `false` will be removed:
468
- *
469
- * ```js
470
- * collect([1, 2, 3, null, false, '', 0, 4, 5]).filter();
471
- * // [1, 2, 3, 4, 5]
472
- * ```
473
- *
474
- * For the inverse of `filter`, see the `reject` method.
475
- *
476
- */
477
- filter(callback?: CollectionIteratorCallback<T, boolean>): Collection<T>;
478
- /**
479
- *
480
- * The `first` method returns the first item in the collection that passes a given truth test:
481
- *
482
- * ```js
483
- * collect([1, 2, 3, 4, 5]).first(value => value > 2);
484
- * // 3
485
- * ```
486
- *
487
- * If no callback is provided, the first item in the collection will be returned:
488
- *
489
- * ```js
490
- * collect([1, 2, 3, 4, 5]).first();
491
- * // 1
492
- * ```
493
- *
494
- * If the collection is empty, or if no items pass the truth test, `null` will be returned.
495
- *
496
- */
497
- first(callback?: CollectionIteratorCallback<T, boolean>): T | null;
498
- /**
499
- *
500
- * The `firstOrFail` is identical to the `first` method, except that it will throw
501
- * an error if no matching item is found:
502
- *
503
- * ```js
504
- * collect([1, 2, 3, 4]).firstOrFail(value => value > 5);
505
- * // Error: No matching item found
506
- * ```
507
- *
508
- * If no callback is provided, the `firstOrFail` method will return the first item
509
- * in the collection. If the collection is empty, an error will be thrown:
510
- *
511
- * ```js
512
- * collect().firstOrFail();
513
- * // Error: No matching item found
514
- * ```
515
- *
516
- */
517
- firstOrFail(callback?: CollectionIteratorCallback<T, boolean>): T;
518
- /**
519
- *
520
- * The `firstWhere` method returns the first item in the collection with the given key / value pair:
521
- *
522
- * ```js
523
- * const collection = collect([
524
- * { name: 'Janie Doe', age: null },
525
- * { name: 'John Doe', age: 36 },
526
- * { name: 'Jane Doe', age: 34 },
527
- * { name: 'Johnny Doe', age: 86 },
528
- * ]);
529
- *
530
- * collection.firstWhere('age', 36);
531
- * // { name: 'John Doe', age: 36 }
532
- * ```
533
- *
534
- * You may also call the `firstWhere` method with a comparison operator:
535
- *
536
- * ```js
537
- * collection.firstWhere('age', '>', 35);
538
- * // { name: 'John Doe', age: 36 }
539
- * ```
540
- *
541
- * Like the `where` method, you may pass one argument to the `firstWhere` method.
542
- * In this scenario, the `firstWhere` method will return the first item where the
543
- * given item key's value is "truthy":
544
- *
545
- * ```js
546
- * collection.firstWhere('age');
547
- * // { name: 'John Doe', age: 36 }
548
- * ```
549
- *
550
- */
551
- firstWhere(key: keyof T): T | null;
552
- firstWhere(key: keyof T, value: T): T | null;
553
- firstWhere(key: keyof T, operator: Operator, value: T): T | null;
554
- /**
555
- *
556
- * Calls a defined callback function on each element of the collection. Then,
557
- * flattens the result into a new collection. This is identical to a map followed
558
- * by flat with depth 1:
559
- *
560
- * ```js
561
- * const collection = collect([1, 2, 5]);
562
- *
563
- * collection.flatMap((num) => (num === 2 ? [num, num * 2] : 1));
564
- * // [1, 2, 4, 1]
565
- * ```
566
- *
567
- */
568
- flatMap<R>(callback: CollectionIteratorCallback<T, R | R[]>): Collection<R>;
569
- /**
570
- *
571
- * The `forget` method removes an item from the collection by its key:
572
- *
573
- * ```js
574
- * const collection = collect([1, 2, 3, 4]);
575
- *
576
- * collection.forget(1);
577
- *
578
- * collection.all();
579
- * // [1, 3, 4]
580
- *
581
- * ```
582
- *
583
- * Unlike most other collection methods, `forget` does not return a new collection;
584
- * it modifies the collection it is called on.
585
- *
586
- */
587
- forget(key: number): Collection<T>;
588
- /**
589
- *
590
- * The `forPage` method returns a new collection containing the items that would be present
591
- * on a given page number. The method accepts the page number as its first argument and the
592
- * number of items to show per page as its second argument:
593
- *
594
- * ```js
595
- * collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
596
- * .forPage(2, 3)
597
- * .all();
598
- * // [4, 5, 6]
599
- * ```
600
- *
601
- */
602
- forPage(page: number, perPage: number): Collection<T>;
603
- /**
604
- *
605
- * The `get` method returns the item at a given key. If the key does not exist,
606
- * `null` will be returned:
607
- *
608
- * ```js
609
- * collect([1, 2, 3, 4]).get(2);
610
- * // 3
611
- * ```
612
- *
613
- * You may also pass a default value as the second argument:
614
- *
615
- * ```js
616
- * collect([1, 2, 3, 4]).get(5, 'default');
617
- * // 'default'
618
- * ```
619
- *
620
- * You may even pass a callback as the default value. The result of the callback
621
- * will be returned if the key does not exist:
622
- *
623
- * ```js
624
- * collect([1, 2, 3, 4]).get(5, () => 'default');
625
- * // 'default'
626
- * ```
627
- *
628
- */
629
- get(key: number): T | null;
630
- get<R>(key: number, defaultValue: R): T | R;
631
- get<R>(key: number, defaultValue: () => R): T | R;
632
- /**
633
- *
634
- * The `groupBy` method groups the collection's items by a given key:
635
- *
636
- * ```js
637
- * const collection = collect([
638
- * { account_id: 'account-x10', product: 'Chair' },
639
- * { account_id: 'account-x10', product: 'Bookcase' },
640
- * { account_id: 'account-x11', product: 'Desk' },
641
- * ]);
642
- *
643
- * collection.groupBy('account_id');
644
- * // {
645
- * // 'account-x10': [
646
- * // { account_id: 'account-x10', product: 'Chair' },
647
- * // { account_id: 'account-x10', product: 'Bookcase' },
648
- * // ],
649
- * // 'account-x11': [
650
- * // { account_id: 'account-x11', product: 'Desk' },
651
- * // ],
652
- * // }
653
- * ```
654
- *
655
- * The `groupBy` method also accepts a callback. The callback should return the value
656
- * to group the collection by:
657
- *
658
- * ```js
659
- * collection.groupBy((item) => item.account_id.slice(-3));
660
- * // {
661
- * // 'x10': [
662
- * // { account_id: 'account-x10', product: 'Chair' },
663
- * // { account_id: 'account-x10', product: 'Bookcase' },
664
- * // ],
665
- * // 'x11': [
666
- * // { account_id: 'account-x11', product: 'Desk' },
667
- * // ],
668
- * // }
669
- * ```
670
- *
671
- * Multiple grouping criteria may be passed as an array:
672
- *
673
- * ```js
674
- * const data = collect([
675
- * { user: 1, skill: 1, roles: ['Member', 'Author'] },
676
- * { user: 2, skill: 1, roles: ['Member', 'Manager'] },
677
- * { user: 3, skill: 2, roles: ['Member'] },
678
- * { user: 4, skill: 2, roles: ['Manager'] },
679
- * ]);
680
- *
681
- * data.groupBy(['skill', (item) => item.roles]);
682
- * // {
683
- * // '1': {
684
- * // 'Member': [
685
- * // { user: 1, skill: 1, roles: ['Member', 'Author'] },
686
- * // { user: 2, skill: 1, roles: ['Member', 'Manager'] },
687
- * // ],
688
- * // 'Author': [
689
- * // { user: 1, skill: 1, roles: ['Member', 'Author'] },
690
- * // ],
691
- * // 'Manager': [
692
- * // { user: 2, skill: 1, roles: ['Member', 'Manager'] },
693
- * // ],
694
- * // },
695
- * // '2': {
696
- * // 'Member': [
697
- * // { user: 3, skill: 2, roles: ['Member'] },
698
- * // ],
699
- * // 'Manager': [
700
- * // { user: 4, skill: 2, roles: ['Manager'] },
701
- * // ],
702
- * // },
703
- * // }
704
- * ```
705
- *
706
- */
707
- groupBy(key: keyof T): Record<string, T[]>;
708
- groupBy(callback: CollectionIteratorCallback<T, string | string[]>): Record<string, T[]>;
709
- groupBy(keys: (keyof T | CollectionIteratorCallback<T, string | string[]>)[]): Record<string, unknown>;
710
- /**
711
- *
712
- * The `has` method determines if a index exists in the collection:
713
- *
714
- * ```js
715
- * collect([1, 2, 3, 4]).has(2);
716
- * // true
717
- * ```
718
- *
719
- */
720
- has(index: number): boolean;
721
- /**
722
- *
723
- * The `hasAny` method determines if any of the given indexes exist in the collection:
724
- *
725
- * ```js
726
- * collect([1, 2, 3, 4]).hasAny([2, 4, 6]);
727
- * // true
728
- * ```
729
- *
730
- */
731
- hasAny(indexes: number[]): boolean;
732
- /**
733
- *
734
- * The `implode` method joins the items in a collection. Its arguments depend on the type
735
- * of items in the collection. If the collection contains objects, you should pass the key
736
- * of the attributes you wish to join, and the "glue" string you wish to place between the
737
- * values:
738
- *
739
- * ```js
740
- * const collection = collect([
741
- * { account_id: 1, product: 'Desk' },
742
- * { account_id: 2, product: 'Chair' },
743
- * ]).implode('product', ', ');
744
- * // 'Desk, Chair'
745
- * ```
746
- *
747
- * If the collection contains simple strings or numeric values, simply pass the "glue" as
748
- * the only argument:
749
- *
750
- * ```js
751
- * collect([1, 2, 3, 4, 5]).implode('-');
752
- * // '1-2-3-4-5'
753
- * ```
754
- *
755
- * You may pass a callback to the `implode` method if you would like to format the values
756
- * being imploded:
757
- *
758
- * ```js
759
- * collection.implode((item) => item.product.toUpperCase(), ', ');
760
- * // 'DESK, CHAIR'
761
- * ```
762
- *
763
- */
764
- implode(glue: string): string;
765
- implode(key: keyof T, glue: string): string;
766
- implode(callback: CollectionIteratorCallback<T, string>, glue: string): string;
767
- /**
768
- *
769
- * The `intersect` method compares the collection against another collection or a plain array
770
- * based on its values, keeping only the values that are present in both collections:
771
- *
772
- * ```js
773
- * collect([1, 2, 3, 4, 5]).intersect([2, 4, 6, 8]).all();
774
- * // [2, 4]
775
- * ```
776
- *
777
- * > This method behavior is modified when using a ModelCollection.
778
- *
779
- */
780
- intersect(values: Collection<T> | T[]): Collection<T>;
781
- /**
782
- *
783
- * The `isEmpty` method returns `true` if the collection is empty; otherwise, `false` is returned:
784
- *
785
- * ```js
786
- * collect().isEmpty();
787
- * // true
788
- * ```
789
- *
790
- */
791
- isEmpty(): boolean;
792
- /**
793
- *
794
- * The `isNotEmpty` method returns `true` if the collection is not empty; otherwise, `false` is returned:
795
- *
796
- * ```js
797
- * collect().isNotEmpty();
798
- * // false
799
- * ```
800
- *
801
- */
802
- isNotEmpty(): boolean;
803
- /**
804
- *
805
- * The `join` method joins the collection's values with a string. Using this
806
- * method's second argument, you may also specify how the final element should
807
- * be appended to the string:
808
- *
809
- * ```js
810
- * collect(['a', 'b', 'c']).join(', '); // 'a, b, c'
811
- * collect(['a', 'b', 'c']).join(', ', ' and '); // 'a, b and c'
812
- * ```
813
- *
814
- */
815
- join(glue: string): string;
816
- join(glue: string, finalGlue: string): string;
817
- /**
818
- *
819
- * The `keyBy` method keys the collection by the given key. If multiple items have the same key,
820
- * only the last one will appear in the new collection:
821
- *
822
- * ```js
823
- * const collection = collect([
824
- * { product_id: 'prod-100', name: 'Desk' },
825
- * { product_id: 'prod-200', name: 'Chair' },
826
- * ]);
827
- *
828
- * collection.keyBy('product_id');
829
- * // {
830
- * // 'prod-100': { product_id: 'prod-100', name: 'Desk' },
831
- * // 'prod-200': { product_id: 'prod-200', name: 'Chair' },
832
- * // }
833
- * ```
834
- *
835
- * You may also pass a callback to the method. The callback should return the value to key
836
- * the collection by:
837
- *
838
- * ```js
839
- * collection.keyBy((item) => item.product_id.toUpperCase());
840
- * // {
841
- * // 'PROD-100': { product_id: 'prod-100', name: 'Desk' },
842
- * // 'PROD-200': { product_id: 'prod-200', name: 'Chair' },
843
- * // }
844
- * ```
845
- *
846
- */
847
- keyBy(key: keyof T): Record<string, T>;
848
- keyBy(callback: CollectionIteratorCallback<T, string>): Record<string, T>;
849
- /**
850
- *
851
- * The `last` method returns the last item in the collection that passes a given truth test:
852
- *
853
- * ```js
854
- * collect([1, 2, 3, 4, 5]).last(value => value < 3);
855
- * // 2
856
- * ```
857
- *
858
- * If no callback is provided, the last item in the collection will be returned:
859
- *
860
- * ```js
861
- * collect([1, 2, 3, 4, 5]).last();
862
- * // 5
863
- * ```
864
- *
865
- */
866
- last(callback?: CollectionIteratorCallback<T, boolean>): T | null;
867
- /**
868
- *
869
- * The `map` method iterates through the collection and passes each value to
870
- * the given callback. The callback is free to modify the item and return it,
871
- * thus forming a new collection of modified items:
872
- *
873
- * ```js
874
- * collect([1, 2, 3, 4, 5]).map(value => value * 2).all();
875
- * // [2, 4, 6, 8, 10]
876
- *
877
- */
878
- map<R>(callback: CollectionIteratorCallback<T, R>): Collection<R>;
879
- /**
880
- *
881
- * The `mapInto` method creates a new collection by passing each item in the collection
882
- * to a given constructor:
883
- *
884
- * ```js
885
- * class User {
886
- *
887
- * constructor(attributes) {
888
- * this.attributes = attributes;
889
- * }
890
- * }
891
- *
892
- * const collection = collect([{ name: 'John' }, { name: 'Jane' }]);
893
- *
894
- * collection.mapInto(User).all();
895
- * // [User, User]
896
- * ```
897
- *
898
- */
899
- mapInto<R extends Constructor<InstanceType<R>>>(constructor: R): Collection<InstanceType<R>>;
900
- /**
901
- *
902
- * The `mapSpread` method iterates through the collection and passes each nested item to
903
- * the given callback. The callback is free to modify the item and return it, thus forming
904
- * a new collection of modified items:
905
- *
906
- * ```js
907
- * const collection = collect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
908
- *
909
- * collection.chunk(2).mapSpread((even, odd) => even + odd).all();
910
- * // [1, 5, 9, 13, 17]
911
- * ```
912
- *
913
- */
914
- mapSpread<R>(callback: (...args: unknown[]) => R): Collection<R>;
915
- /**
916
- *
917
- * The `mapToGroups` method groups the collection's items by a given callback.
918
- * The callback should return an object containing the a single key / value pair,
919
- * thus forming a new object of grouped values:
920
- *
921
- * ```js
922
- * const collection = collect([
923
- * { name: 'John Doe', department: 'Sales' },
924
- * { name: 'Jane Doe', department: 'Sales' },
925
- * { name: 'Johnny Doe', department: 'Marketing' },
926
- * ]);
927
- *
928
- * collection.mapToGroups((item) => ({ [item.department]: item.name }));
929
- * // {
930
- * // 'Sales': ['John Doe', 'Jane Doe'],
931
- * // 'Marketing': ['Johnny Doe'],
932
- * // }
933
- * ```
934
- *
935
- */
936
- mapToGroups<R>(callback: CollectionIteratorCallback<T, Record<string, R>>): Record<string, R[]>;
937
- /**
938
- *
939
- * The `mapWithKeys` method iterates through the collection and passes each value to the given callback.
940
- * The callback should return an object containing a single key / value pair:
941
- *
942
- * ```js
943
- * const collection = collect([
944
- * { product_id: 'prod-100', name: 'Desk' },
945
- * { product_id: 'prod-200', name: 'Chair' },
946
- * ]);
947
- *
948
- * collection.mapWithKeys((item) => ({ [item.product_id]: item.name }));
949
- * // {
950
- * // 'prod-100': 'Desk',
951
- * // 'prod-200': 'Chair',
952
- * // }
953
- * ```
954
- *
955
- */
956
- mapWithKeys<R>(callback: CollectionIteratorCallback<T, Record<string, R>>): Record<string, R>;
957
- /**
958
- *
959
- * The `max` method returns the maximum value of a given key:
960
- *
961
- * ```js
962
- * const collection = collect([{ foo: 10 }, { foo: 20 }]);
963
- *
964
- * collection.max('foo');
965
- * // 20
966
- *
967
- * collect([1, 2, 3, 4, 5]).max();
968
- * // 5
969
- *
970
- * ```
971
- *
972
- * If the collection is empty, `null` will be returned.
973
- *
974
- */
975
- max(): T | null;
976
- max<K extends keyof T>(key: K): T[K] | null;
977
- /**
978
- *
979
- * The `median` method returns the median value of a given key:
980
- *
981
- * ```js
982
- * const collection = collect([1, 2, 2, 4]);
983
- *
984
- * collection.median();
985
- * // 2
986
- * ```
987
- *
988
- * If the collection is empty, `null` will be returned.
989
- *
990
- */
991
- median(): T | null;
992
- median<K extends keyof T>(key: K): T[K] | null;
993
- /**
994
- *
995
- * The `merge` method creates a new collection by merging the items
996
- * of the collection with the items of another array or collection:
997
- *
998
- * ```js
999
- * collect([1, 2, 3]).merge([4, 5, 6]);
1000
- * // [1, 2, 3, 4, 5, 6]
1001
- * ```
1002
- *
1003
- */
1004
- merge(values: Collection<T> | T[]): Collection<T>;
1005
- merge<R>(values: Collection<R> | R[]): Collection<T | R>;
1006
- /**
1007
- *
1008
- * The `min` method returns the minimum value of a given key:
1009
- *
1010
- * ```js
1011
- * let min = collect([{ foo: 10 }, { foo: 20 }]).min('foo');
1012
- * // 10
1013
- *
1014
- * min = collect([1, 2, 3, 4, 5]).min();
1015
- * // 1
1016
- *
1017
- */
1018
- min(): T | null;
1019
- min<K extends keyof T>(key: K): T[K] | null;
1020
- /**
1021
- *
1022
- * The `mode` method returns the mode value of a given key:
1023
- *
1024
- * ```js
1025
- * let mode = collect([
1026
- * { foo: 10 },
1027
- * { foo: 10 },
1028
- * { foo: 20 },
1029
- * { foo: 40 },
1030
- * ]).mode('foo');
1031
- * // [10]
1032
- *
1033
- * mode = collect([1, 2, 2, 4]).mode();
1034
- * // [2]
1035
- *
1036
- * mode = collect([1, 1, 2, 2]).mode();
1037
- * // [1, 2]
1038
- *
1039
- * ```
1040
- *
1041
- * @link https://en.wikipedia.org/wiki/Mode_(statistics)
1042
- *
1043
- */
1044
- mode(): T[];
1045
- mode<K extends keyof T>(key: K): T[K][];
1046
- /**
1047
- *
1048
- * The `nth` method creates a new collection consisting of every `n-th` element:
1049
- *
1050
- * ```js
1051
- * const collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1052
- *
1053
- * collection.nth(4).all();
1054
- * // [1, 5, 9]
1055
- * ```
1056
- *
1057
- * You may also pass an offset as the second argument:
1058
- *
1059
- * ```js
1060
- * collection.nth(4, 1).all();
1061
- * // [2, 6, 10]
1062
- * ```
1063
- *
1064
- */
1065
- nth(n: number, offset?: number): Collection<T>;
1066
- /**
1067
- *
1068
- * The `only` method returns the items in the collection with the specified indexes:
1069
- *
1070
- * ```js
1071
- * collect([1, 2, 3, 4, 5]).only([0, 2, 4]);
1072
- * // [1, 3, 5]
1073
- * ```
1074
- *
1075
- * For the inverse of `only`, see the `except` method.
1076
- *
1077
- */
1078
- only(indexes: number[]): Collection<T>;
1079
- /**
1080
- *
1081
- * The `pad` method will fill the array with the given value until the array reaches the specified size:
1082
- * To pad to the left, you should specify a negative size. No padding will take place if the absolute
1083
- * value of the given size is less than or equal to the length of the collection:
1084
- *
1085
- * ```js
1086
- * collect([1, 2, 3]).pad(5, 0).all();
1087
- * // [1, 2, 3, 0, 0]
1088
- *
1089
- * collect([1, 2, 3]).pad(-5, 0).all();
1090
- *
1091
- * // [0, 0, 1, 2, 3]
1092
- * ```
1093
- *
1094
- */
1095
- pad<R>(size: number, value?: T | R | null): Collection<T | R | null>;
1096
- /**
1097
- *
1098
- * The `partition` method may be combined with array destructuring to separate elements that pass a given
1099
- * truth test from those that do not:
1100
- *
1101
- * ```js
1102
- * const collection = collect([1, 2, 3, 4, 5, 6]);
1103
- *
1104
- * const [even, odd] = collection.partition((value) => value % 2 === 0);
1105
- *
1106
- * even.all();
1107
- * // [2, 4, 6]
1108
- *
1109
- * odd.all();
1110
- * // [1, 3, 5]
1111
- *
1112
- * ```
1113
- *
1114
- */
1115
- partition(callback: CollectionIteratorCallback<T, boolean>): [Collection<T>, Collection<T>];
1116
- /**
1117
- *
1118
- * The `percentage` method calculates the percentage of items in the collection
1119
- * that pass a given truth test:
1120
- *
1121
- * ```js
1122
- * collect([1, 1, 2, 2, 2, 3]).percentage((value) => value === 1);
1123
- * // 33.33
1124
- *
1125
- * ```
1126
- *
1127
- * By default, the `percentage` method will return a floating point number with two decimal places.
1128
- * You may pass an optional second argument to specify the number of decimal places:
1129
- *
1130
- * ```js
1131
- * collect([1, 1, 2, 2, 2, 3]).percentage((value) => value === 1, 3);
1132
- * // 33.333
1133
- * ```
1134
- *
1135
- */
1136
- percentage(callback: CollectionIteratorCallback<T, boolean>, precision?: number): number;
1137
- /**
1138
- *
1139
- * The `pipe` method passes the collection to the given callback and returns the result:
1140
- *
1141
- * ```js
1142
- * collect([1, 2, 3]).pipe((collection) => {
1143
- * return collection.sum();
1144
- * });
1145
- * // 6
1146
- * ```
1147
- *
1148
- */
1149
- pipe<R>(callback: CollectionPipeCallback<T, R>): R;
1150
- /**
1151
- *
1152
- * The `pipeInto` method passes the collection to the given constructor and returns the result:
1153
- *
1154
- * ```js
1155
- * class MyClass {
1156
- * constructor(collection) {
1157
- * this.collection = collection;
1158
- * }
1159
- * }
1160
- *
1161
- * collect([1, 2, 3]).pipeInto(MyClass).collection.all();
1162
- * // [1, 2, 3]
1163
- *
1164
- */
1165
- pipeInto<R extends Constructor<InstanceType<R>>>(constructor: R): InstanceType<R>;
1166
- /**
1167
- *
1168
- * The `pipeThrough` method passes the collection to the given array of callbacks and returns the result
1169
- * of the last callback in the array:
1170
- *
1171
- * ```js
1172
- * const collection = collect([1, 2, 3]);
1173
- *
1174
- * collection.pipeThrough([
1175
- * (collection) => collection.merge([4, 5]),
1176
- * (collection) => collection.sum(),
1177
- * ]);
1178
- * // 15
1179
- * ```
1180
- *
1181
- */
1182
- pipeThrough<R>(pipeline: CollectionPipeCallback<unknown, Collection<unknown> | R>[]): R;
1183
- /**
1184
- *
1185
- * The `pluck` method retrieves all of the values for a given key:
1186
- *
1187
- * ```js
1188
- * const collection = collect([
1189
- * { product_id: 'prod-100', name: 'Desk' },
1190
- * { product_id: 'prod-200', name: 'Chair' },
1191
- * ]);
1192
- *
1193
- * collection.pluck('name');
1194
- * // ['Desk', 'Chair']
1195
- * ```
1196
- *
1197
- */
1198
- pluck<K extends keyof T>(key: K): Collection<T[K]>;
1199
- /**
1200
- *
1201
- * The `pop` method removes and returns the last item from the collection:
1202
- *
1203
- * ```js
1204
- * const collection = collect([1, 2, 3, 4, 5]);
1205
- *
1206
- * collection.pop();
1207
- * // 5
1208
- *
1209
- * collection.all();
1210
- * // [1, 2, 3, 4]
1211
- *
1212
- * ```
1213
- *
1214
- * You may pass an integer to the `pop` method to remove and return a specific
1215
- * number of items from the end of the collection:
1216
- *
1217
- * ```js
1218
- * collection.pop(2);
1219
- * // collect([3, 4])
1220
- *
1221
- * ```
1222
- *
1223
- */
1224
- pop(): T | null;
1225
- pop(count: number): Collection<T>;
1226
- /**
1227
- *
1228
- * The `prepend` method adds an item to the beginning of the collection:
1229
- *
1230
- * ```js
1231
- * collect([1, 2, 3, 4, 5]).prepend(0).all();
1232
- * // [0, 1, 2, 3, 4, 5]
1233
- * ```
1234
- *
1235
- */
1236
- prepend(value: T): number;
1237
- /**
1238
- *
1239
- * The `pull` method removes and returns an item from the collection by its key:
1240
- *
1241
- * ```js
1242
- * const collection = collect([1, 2, 3, 4]);
1243
- *
1244
- * collection.pull(1);
1245
- * // 2
1246
- *
1247
- * collection.all();
1248
- * // [1, 3, 4]
1249
- * ```
1250
- *
1251
- */
1252
- pull(key: number): T | null;
1253
- /**
1254
- *
1255
- * The `push` method appends an item to the end of the collection:
1256
- *
1257
- * ```js
1258
- * collect([1, 2, 3, 4, 5]).push(6).all();
1259
- * // [1, 2, 3, 4, 5, 6]
1260
- * ```
1261
- *
1262
- * You may pass many items to the `push` method:
1263
- *
1264
- * ```js
1265
- * collect([1, 2, 3, 4, 5]).push(6, 7, 8).all();
1266
- * // [1, 2, 3, 4, 5, 6, 7, 8]
1267
- * ```
1268
- *
1269
- *
1270
- */
1271
- push(...values: T[]): number;
1272
- /**
1273
- *
1274
- * The `put` method sets the given key and value in the collection:
1275
- *
1276
- * ```js
1277
- * const collection = collect([1, 2, 3, 4]);
1278
- *
1279
- * collection.put(2, 5);
1280
- *
1281
- * collection.all();
1282
- * // [1, 2, 5, 4]
1283
- * ```
1284
- *
1285
- */
1286
- put(key: number, value: T): Collection<T>;
1287
- /**
1288
- *
1289
- * The `random` method returns a random item from the collection:
1290
- *
1291
- * ```js
1292
- * collect([1, 2, 3, 4, 5]).random();
1293
- * // 3
1294
- * ```
1295
- *
1296
- * You may also specify the number of items to randomly return:
1297
- *
1298
- * ```js
1299
- * collect([1, 2, 3, 4, 5]).random(3);
1300
- * // [2, 4, 5]
1301
- * ```
1302
- *
1303
- */
1304
- random(): T | null;
1305
- random(count: number): Collection<T>;
1306
- /**
1307
- *
1308
- * The `reduce` method reduces the collection to a single value, passing
1309
- * the result of each iteration into the subsequent iteration:
1310
- *
1311
- * ```js
1312
- * collect([1, 2, 3, 4, 5]).reduce((carry, item) => carry + item, 0);
1313
- * // 15
1314
- * ```
1315
- *
1316
- */
1317
- reduce<R>(callback: (carry: R | null, item: T, index: number, collection: Collection<T>) => R, initialValue?: R | null): R | null;
1318
- /**
1319
- *
1320
- * The `reject` method filters the collection using the given callback. The callback should return `true`
1321
- * if the item should be removed from the resulting collection:
1322
- *
1323
- * ```js
1324
- * collect([1, 2, 3, 4, 5]).reject(value => value > 2);
1325
- * // [1, 2]
1326
- * ```
1327
- *
1328
- * For the inverse of `reject`, see the `filter` method.
1329
- *
1330
- */
1331
- reject(callback: CollectionIteratorCallback<T, boolean>): Collection<T>;
1332
- /**
1333
- *
1334
- * The `replace` method replaces the given key in the collection with the given value:
1335
- *
1336
- * ```js
1337
- * const collection = collect([1, 2, 3, 4]);
1338
- *
1339
- * const replaced = collection.replace({
1340
- * 1: 5,
1341
- * 3: 6,
1342
- * });
1343
- *
1344
- * replaced.all();
1345
- * // [1, 5, 3, 6]
1346
- *
1347
- * ```
1348
- *
1349
- */
1350
- replace(replacements: Record<number, T>): Collection<T>;
1351
- /**
1352
- *
1353
- * The `reverse` method reverses the order of the collection's items.
1354
- * Unlike `Array.reverse()` method, this method returns a new collection,
1355
- * leaving the original collection unchanged:
1356
- *
1357
- * ```js
1358
- * collect([1, 2, 3, 4, 5]).reverse().all();
1359
- * // [5, 4, 3, 2, 1]
1360
- * ```
1361
- *
1362
- */
1363
- reverse(): Collection<T>;
1364
- /**
1365
- *
1366
- * The `search` method searches the collection for the given value and returns its key if found.
1367
- * If the item is not found, `false` will be returned:
1368
- *
1369
- * ```js
1370
- * collect([1, 2, 3, 4, 5]).search(3);
1371
- * // 2
1372
- * ```
1373
- *
1374
- * The search is done using a "loose" comparison. In this case, the string `3` is considered equal
1375
- * to the number `3`. To use "strict" comparison, pass `true` as the second argument:
1376
- *
1377
- * ```js
1378
- * collect([1, 2, 3, 4, 5]).search('3', true);
1379
- * // false
1380
- * ```
1381
- *
1382
- * Alternatively, you may provide your own callback to search for the first item
1383
- * that passes your truth test:
1384
- *
1385
- * ```js
1386
- * collect([1, 2, 3, 4, 5]).search(value => value > 2);
1387
- * // 2
1388
- * ```
1389
- *
1390
- */
1391
- search(value: T): number | false;
1392
- search(value: T, strict: boolean): number | false;
1393
- search(callback: CollectionIteratorCallback<T, boolean>): number | false;
1394
- /**
1395
- *
1396
- * The `select` method selects the given keys from the collection:
1397
- *
1398
- * ```js
1399
- * const collection = collect([
1400
- * { product_id: 'prod-100', name: 'Desk', price: 200 },
1401
- * { product_id: 'prod-200', name: 'Chair', price: 100 },
1402
- * ]);
1403
- *
1404
- * collection.select(['name', 'price']).all();
1405
- * // [{ name: 'Desk', price: 200 }, { name: 'Chair', price: 100 }]
1406
- *
1407
- * ```
1408
- *
1409
- */
1410
- select<K extends Array<keyof T>>(keys: K): Collection<Pick<T, K[number]>>;
1411
- /**
1412
- *
1413
- * The `shift` method removes and returns the first item from the collection:
1414
- *
1415
- * ```js
1416
- * const collection = collect([1, 2, 3, 4, 5]);
1417
- *
1418
- * collection.shift();
1419
- * // 1
1420
- *
1421
- * collection.all();
1422
- * // [2, 3, 4, 5]
1423
- * ```
1424
- *
1425
- * You may pass an integer to the `shift` method to remove and return a specific
1426
- * number of items from the beginning of the collection:
1427
- *
1428
- * ```js
1429
- * collection.shift(2);
1430
- * // collect([2, 3])
1431
- *
1432
- * ```
1433
- *
1434
- */
1435
- shift(): T | null;
1436
- shift(count: number): Collection<T>;
1437
- /**
1438
- *
1439
- * The `shuffle` method randomly shuffles the items in the collection:
1440
- *
1441
- * ```js
1442
- * collect([1, 2, 3, 4, 5]).shuffle().all();
1443
- * // [3, 2, 5, 1, 4] - (generated randomly)
1444
- * ```
1445
- *
1446
- */
1447
- shuffle(): Collection<T>;
1448
- /**
1449
- *
1450
- * The `skip` method returns a new collection, with the given number of elements
1451
- * removed from the beginning:
1452
- *
1453
- * ```js
1454
- * collect([1, 2, 3, 4, 5]).skip(2).all();
1455
- * // [3, 4, 5]
1456
- *
1457
- * ```
1458
- *
1459
- */
1460
- skip(count: number): Collection<T>;
1461
- /**
1462
- *
1463
- * The `skipUntil` method continues to skip items in the collection until the given callback returns `true`:
1464
- *
1465
- * ```js
1466
- * collect([1, 2, 3, 4, 5]).skipUntil(value => value > 2).all();
1467
- * // [3, 4, 5]
1468
- * ```
1469
- *
1470
- * You may also pass a simple value to the skipUntil method. In this case, the collection will skip
1471
- * all items until the given value is found:
1472
- *
1473
- * ```js
1474
- * collect([1, 2, 3, 4, 5]).skipUntil(3).all();
1475
- * // [3, 4, 5]
1476
- * ```
1477
- *
1478
- * > If the given value is not found or the callback never returns `true`, an empty collection will be returned.
1479
- *
1480
- */
1481
- skipUntil(callback: CollectionIteratorCallback<T, boolean>): Collection<T>;
1482
- skipUntil(value: T): Collection<T>;
1483
- /**
1484
- *
1485
- * The `skipWhile` method continues to skip items in the collection until the given callback returns `false`:
1486
- *
1487
- * ```js
1488
- * collect([1, 2, 3, 4, 5]).skipWhile(value => value < 3).all();
1489
- * // [3, 4, 5]
1490
- * ```
1491
- *
1492
- * You may also pass a simple value to the skipWhile method. In this case, the collection will skip
1493
- * all items until the given value is found:
1494
- *
1495
- * ```js
1496
- * collect([1, 2, 3, 4, 5]).skipWhile(3).all();
1497
- * // [3, 4, 5]
1498
- * ```
1499
- *
1500
- * > If the given value is not found or the callback never returns `false`, an empty collection will be returned.
1501
- *
1502
- */
1503
- skipWhile(callback: CollectionIteratorCallback<T, boolean>): Collection<T>;
1504
- skipWhile(value: T): Collection<T>;
1505
- /**
1506
- *
1507
- * The `slice` method returns a slice of the collection starting at the given index:
1508
- *
1509
- * ```js
1510
- * collect([1, 2, 3, 4, 5]).slice(2).all();
1511
- * // [3, 4, 5]
1512
- * ```
1513
- *
1514
- * If you would like to limit the size of the slice, you may pass the desired size as the second argument:
1515
- *
1516
- * ```js
1517
- * collect([1, 2, 3, 4, 5]).slice(2, 2).all();
1518
- * // [3, 4]
1519
- * ```
1520
- *
1521
- */
1522
- slice(start: number, size?: number): Collection<T>;
1523
- /**
1524
- *
1525
- * The `sliding` method returns a new collection of chunks representing a "sliding window" view of the
1526
- * items in the collection:
1527
- *
1528
- * ```js
1529
- * collect([1, 2, 3, 4, 5]).sliding(3).all();
1530
- * // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
1531
- *
1532
- * ```
1533
- *
1534
- * You may optionally pass a second "step" value, which determines the distance between the first item
1535
- * of each chunk:
1536
- *
1537
- * ```js
1538
- * collect([1, 2, 3, 4, 5]).sliding(3, 2).all();
1539
- * // [[1, 2, 3], [3, 4, 5]]
1540
- * ```
1541
- *
1542
- */
1543
- sliding(size: number, step?: number): Collection<Collection<T>>;
1544
- /**
1545
- *
1546
- * The `sole` method returns the first item in the collection that passes a given
1547
- * truth test, but only if the truth test matches exactly one item:
1548
- *
1549
- * ```js
1550
- * collect([1, 2, 3, 4, 5]).sole(value => value === 2);
1551
- * // 2
1552
- * ```
1553
- *
1554
- * You may also pass a key / value pair to the `sole` method, which will return the
1555
- * first item that matches the given pair, but only if exactly one item matches:
1556
- *
1557
- * ```js
1558
- * const collection = collect([
1559
- * { product: 'Desk', price: 200 },
1560
- * { product: 'Chair', price: 100 },
1561
- * ]);
1562
- *
1563
- * collection.sole('product', 'Chair');
1564
- * // { product: 'Chair', price: 100 }
1565
- *
1566
- * ```
1567
- *
1568
- * Alternatively, you may also call the `sole` method without any arguments to return the
1569
- * first item in the collection, but only if the collection contains exactly one item:
1570
- *
1571
- * ```js
1572
- * collect([1]).sole();
1573
- * // 1
1574
- * ```
1575
- *
1576
- * If there are no items in the collection or more than one item matches the given truth test,
1577
- * `null` will be returned.
1578
- *
1579
- */
1580
- sole(): T | null;
1581
- sole<K extends keyof T>(key: K, value: T[K]): T | null;
1582
- sole(callback: CollectionIteratorCallback<T, boolean>): T | null;
1583
- /**
1584
- *
1585
- * Alias for the `contains` method.
1586
- *
1587
- */
1588
- some(value: T): boolean;
1589
- some(key: keyof T, value: T): boolean;
1590
- some(callback: CollectionIteratorCallback<T, boolean>): boolean;
1591
- /**
1592
- *
1593
- * The `sort` method sorts the collection. Unlike tradition `Array.sort()` method, this method
1594
- * returns a new collection, leaving the original collection unchanged:
1595
- *
1596
- * ```js
1597
- * collect([5, 3, 1, 2, 4]).sort().all();
1598
- * // [1, 2, 3, 4, 5]
1599
- *
1600
- * ```
1601
- *
1602
- * You may also pass a callback to the `sort` method, which will determine how the collection should be sorted:
1603
- *
1604
- * ```js
1605
- * collect([
1606
- * { name: 'Desk', price: 200 },
1607
- * { name: 'Chair', price: 100 },
1608
- * ]).sort((a, b) => a.price - b.price).all();
1609
- * // [{ name: 'Chair', price: 100 }, { name: 'Desk', price: 200 }]
1610
- *
1611
- * ```
1612
- *
1613
- */
1614
- sort(compareFn?: CollectionSortCallback<T>): Collection<T>;
1615
- /**
1616
- *
1617
- * The `sortBy` method sorts the collection by the given key. The method sorts the collection
1618
- * in ascending order. You may also pass an optional second argument to specify the order:
1619
- *
1620
- * ```js
1621
- * const collection = collect([
1622
- * { name: 'Desk', price: 200 },
1623
- * { name: 'Chair', price: 100 },
1624
- * ]);
1625
- *
1626
- * collection.sortBy('price').all();
1627
- * // [{ name: 'Chair', price: 100 }, { name: 'Desk', price: 200 }]
1628
- *
1629
- * collection.sortBy('price', 'desc').all();
1630
- * // [{ name: 'Desk', price: 200 }, { name: 'Chair', price: 100 }]
1631
- *
1632
- * ```
1633
- *
1634
- * Alternatively, you may pass a callback to the `sortBy` method, which will determine how the
1635
- * collection should be sorted:
1636
- *
1637
- * ```js
1638
- * collect([
1639
- * { name: 'Desk', colors: ['Black', 'Mahogany'] },
1640
- * { name: 'Chair', colors: ['Black'] },
1641
- * { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] },
1642
- * ]).sortBy((item) => item.colors.length).all();
1643
- *
1644
- * // [
1645
- * // { name: 'Chair', colors: ['Black'] },
1646
- * // { name: 'Desk', colors: ['Black', 'Mahogany'] },
1647
- * // { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] },
1648
- * // ]
1649
- *
1650
- * ```
1651
- *
1652
- * If you would like to sort the collection by multiple attributes, you may pass an array of sort
1653
- * operations to the `sortBy` method. Each sort operation should be an array containing the key
1654
- * that you wish to sort by and the direction of the desired sort:
1655
- *
1656
- * ```js
1657
- * const collection = collect([
1658
- * { name: 'Desk', price: 200 },
1659
- * { name: 'Chair', price: 100 },
1660
- * { name: 'Desk', price: 300 },
1661
- * { name: 'Chair', price: 90 },
1662
- * ])
1663
- *
1664
- * collection.sortBy([
1665
- * ['name', 'asc'],
1666
- * ['price', 'desc'],
1667
- * ]).all();
1668
- *
1669
- * // [
1670
- * // { name: 'Chair', price: 100 },
1671
- * // { name: 'Chair', price: 90 },
1672
- * // { name: 'Desk', price: 300 },
1673
- * // { name: 'Desk', price: 200 },
1674
- * // ]
1675
- * ```
1676
- *
1677
- * When sorting by multiple attributes, you may also provide callbacks that define
1678
- * each sort operation:
1679
- *
1680
- * ```js
1681
- * collection.sortBy([
1682
- * (a, b) => a.name.localeCompare(b.name),
1683
- * (a, b) => a.price - b.price,
1684
- * ]).all();
1685
- *
1686
- * // [
1687
- * // { name: 'Chair', price: 90 },
1688
- * // { name: 'Chair', price: 100 },
1689
- * // { name: 'Desk', price: 200 },
1690
- * // { name: 'Desk', price: 300 },
1691
- * // ]
1692
- * ```
1693
- *
1694
- */
1695
- sortBy<K extends keyof T>(key: K, order?: 'asc' | 'desc'): Collection<T>;
1696
- sortBy<K extends keyof T>(columns: [K, 'asc' | 'desc'][]): Collection<T>;
1697
- sortBy(callback: CollectionIteratorCallback<T, number>): Collection<T>;
1698
- sortBy(stack: ((a: T, b: T) => number)[]): Collection<T>;
1699
- /**
1700
- *
1701
- * The `sortDesc` method sorts the collection in the opposite order as the `sort` method:
1702
- *
1703
- * ```js
1704
- * const collection = collect([5, 3, 1, 2, 4]);
1705
- *
1706
- * collection.sortDesc().all();
1707
- * // [5, 4, 3, 2, 1]
1708
- * ```
1709
- *
1710
- * Unlike `sort`, you may not pass a callback to `sortDesc`. Instead, you should use the `sort` method
1711
- * and invert the comparison.
1712
- *
1713
- */
1714
- sortDesc(): Collection<T>;
1715
- /**
1716
- *
1717
- * The `splice` method removes and returns a slice of items starting at the specified index:
1718
- *
1719
- * ```js
1720
- * const collection = collect([1, 2, 3, 4, 5]);
1721
- *
1722
- * const chunk = collection.splice(2);
1723
- *
1724
- * chunk.all();
1725
- * // [3, 4, 5]
1726
- *
1727
- * collection.all();
1728
- * // [1, 2]
1729
- * ```
1730
- *
1731
- * You may also pass a second argument to the `splice` method to specify the size of the slice:
1732
- *
1733
- * ```js
1734
- * const collection = collect([1, 2, 3, 4, 5]);
1735
- *
1736
- * const chunk = collection.splice(2, 2);
1737
- *
1738
- * chunk.all();
1739
- * // [3, 4]
1740
- *
1741
- * collection.all();
1742
- * // [1, 2, 5]
1743
- * ```
1744
- *
1745
- * In addition, you may pass a additional arguments to splice, containing the new items to replace
1746
- * the removed items:
1747
- *
1748
- * ```js
1749
- *
1750
- * const collection = collect([1, 2, 3, 4, 5]);
1751
- *
1752
- * const chunk = collection.splice(2, 1, 10, 11);
1753
- *
1754
- * chunk.all();
1755
- * // [3]
1756
- *
1757
- * collection.all();
1758
- * // [1, 2, 10, 11, 4, 5]
1759
- *
1760
- * ```
1761
- *
1762
- *
1763
- *
1764
- */
1765
- splice(start: number): Collection<T>;
1766
- splice(start: number, deleteCount: number): Collection<T>;
1767
- splice(start: number, deleteCount: number, ...items: T[]): Collection<T>;
1768
- split(groups: number): Collection<Collection<T>>;
1769
- splitIn(groups: number): Collection<Collection<T>>;
1770
- sum(): number;
1771
- sum<K extends keyof T>(key: K): number;
1772
- take(amount: number): Collection<T>;
1773
- takeUntil(value: T): Collection<T>;
1774
- takeUntil(callback: CollectionIteratorCallback<T, boolean>): Collection<T>;
1775
- takeWhile(value: T): Collection<T>;
1776
- takeWhile(callback: CollectionIteratorCallback<T, boolean>): Collection<T>;
1777
- tap(callback: CollectionPipeCallback<T, void>): Collection<T>;
1778
- toArray(): T[];
1779
- toJson(): string;
1780
- transform<R>(callback: CollectionIteratorCallback<T, R>): Collection<T | R>;
1781
- unique(): Collection<T>;
1782
- unique<K extends keyof T>(key: K): Collection<T>;
1783
- uniqueStrict(): Collection<T>;
1784
- uniqueStrict<K extends keyof T>(key: K): Collection<T>;
1785
- unless(condition: boolean, callback: CollectionPipeCallback<T, void>, otherwise?: CollectionPipeCallback<T, void>): Collection<T>;
1786
- unlessEmpty(callback: CollectionPipeCallback<T, void>, otherwise?: CollectionPipeCallback<T, void>): Collection<T>;
1787
- unlessNotEmpty(callback: CollectionPipeCallback<T, void>, otherwise?: CollectionPipeCallback<T, void>): Collection<T>;
1788
- value<K extends keyof T>(key: K): T[K] | null;
1789
- when(condition: boolean, callback: CollectionPipeCallback<T, void>, otherwise?: CollectionPipeCallback<T, void>): Collection<T>;
1790
- whenEmpty(callback: CollectionPipeCallback<T, void>, otherwise?: CollectionPipeCallback<T, void>): Collection<T>;
1791
- whenNotEmpty(callback: CollectionPipeCallback<T, void>, otherwise?: CollectionPipeCallback<T, void>): Collection<T>;
1792
- where<K extends keyof T>(key: K, value: T[K]): Collection<T>;
1793
- where<K extends keyof T>(key: K, operator: Operator, value: T[K]): Collection<T>;
1794
- whereStrict<K extends keyof T>(key: K, value: T[K]): Collection<T>;
1795
- whereStrict<K extends keyof T>(key: K, operator: Operator, value: T[K]): Collection<T>;
1796
- whereBetween<K extends keyof T>(key: K, [min, max]: [T[K], T[K]]): Collection<T>;
1797
- whereIn<K extends keyof T>(key: K, values: T[K][]): Collection<T>;
1798
- whereInstanceOf<R extends Constructor<T>>(constructor: R): Collection<T>;
1799
- whereNotBetween<K extends keyof T>(key: K, [min, max]: [T[K], T[K]]): Collection<T>;
1800
- whereNotIn<K extends keyof T>(key: K, values: T[K][]): Collection<T>;
1801
- whereNotNull<K extends keyof T>(key: K): Collection<T>;
1802
- whereNull<K extends keyof T>(key: K): Collection<T>;
1803
- zip<R>(items: Collection<R> | R[]): Collection<[T, R | null]>;
1804
- };