@zairakai/js-utils 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 +270 -0
- package/dist/arrays.cjs +210 -0
- package/dist/arrays.d.cts +119 -0
- package/dist/arrays.d.ts +119 -0
- package/dist/arrays.js +32 -0
- package/dist/chunk-27YHP2CK.js +407 -0
- package/dist/chunk-3WNRYKPG.js +37 -0
- package/dist/chunk-42CHLXT7.js +214 -0
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-7SXRFZBB.js +173 -0
- package/dist/chunk-F6RSTW65.js +156 -0
- package/dist/chunk-G7ZJ23DW.js +253 -0
- package/dist/chunk-IPP7PA6H.js +136 -0
- package/dist/chunk-LDSWHSRX.js +96 -0
- package/dist/chunk-TY75OOIQ.js +700 -0
- package/dist/chunk-W6JEMFAF.js +54 -0
- package/dist/chunk-XEJLBAXE.js +164 -0
- package/dist/chunk-Z7G3SIQH.js +270 -0
- package/dist/chunk-ZJPKS2MQ.js +101 -0
- package/dist/collections.cjs +797 -0
- package/dist/collections.d.cts +353 -0
- package/dist/collections.d.ts +353 -0
- package/dist/collections.js +17 -0
- package/dist/datetime.cjs +80 -0
- package/dist/datetime.d.cts +75 -0
- package/dist/datetime.d.ts +75 -0
- package/dist/datetime.js +24 -0
- package/dist/equals.cjs +121 -0
- package/dist/equals.d.cts +24 -0
- package/dist/equals.d.ts +24 -0
- package/dist/equals.js +8 -0
- package/dist/formatters.cjs +201 -0
- package/dist/formatters.d.cts +180 -0
- package/dist/formatters.d.ts +180 -0
- package/dist/formatters.js +48 -0
- package/dist/index.cjs +2906 -0
- package/dist/index.d.cts +120 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +348 -0
- package/dist/number.cjs +279 -0
- package/dist/number.d.cts +177 -0
- package/dist/number.d.ts +177 -0
- package/dist/number.js +10 -0
- package/dist/obj.cjs +427 -0
- package/dist/obj.d.cts +177 -0
- package/dist/obj.d.ts +177 -0
- package/dist/obj.js +12 -0
- package/dist/php-arrays.cjs +954 -0
- package/dist/php-arrays.d.cts +256 -0
- package/dist/php-arrays.d.ts +256 -0
- package/dist/php-arrays.js +70 -0
- package/dist/runtime.cjs +134 -0
- package/dist/runtime.d.cts +90 -0
- package/dist/runtime.d.ts +90 -0
- package/dist/runtime.js +24 -0
- package/dist/schemas.cjs +86 -0
- package/dist/schemas.d.cts +108 -0
- package/dist/schemas.d.ts +108 -0
- package/dist/schemas.js +22 -0
- package/dist/str.cjs +499 -0
- package/dist/str.d.cts +282 -0
- package/dist/str.d.ts +282 -0
- package/dist/str.js +11 -0
- package/dist/types.cjs +18 -0
- package/dist/types.d.cts +13 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/validator.cjs +251 -0
- package/dist/validator.d.cts +99 -0
- package/dist/validator.d.ts +99 -0
- package/dist/validator.js +11 -0
- package/dist/validators.cjs +217 -0
- package/dist/validators.d.cts +216 -0
- package/dist/validators.d.ts +216 -0
- package/dist/validators.js +64 -0
- package/package.json +180 -0
- package/src/arrays.ts +316 -0
- package/src/collections.ts +866 -0
- package/src/datetime.ts +103 -0
- package/src/equals.ts +134 -0
- package/src/formatters.ts +342 -0
- package/src/index.ts +36 -0
- package/src/number.ts +281 -0
- package/src/obj.ts +303 -0
- package/src/php-arrays.ts +445 -0
- package/src/pipe.ts +29 -0
- package/src/runtime.ts +194 -0
- package/src/schemas.ts +136 -0
- package/src/str.ts +438 -0
- package/src/types.ts +13 -0
- package/src/validator.ts +157 -0
- package/src/validators.ts +359 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collection utilities for JavaScript arrays and objects
|
|
3
|
+
* Inspired by Laravel Collections with modern JavaScript features
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Generic callback type for collection operations.
|
|
7
|
+
*/
|
|
8
|
+
type Callback<T, R> = (item: T, index: number) => R;
|
|
9
|
+
/**
|
|
10
|
+
* Predicate type for collection filtering and validation.
|
|
11
|
+
*/
|
|
12
|
+
type Predicate<T> = (item: T, index: number) => boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Enhanced Array class with Laravel Collection-like methods.
|
|
15
|
+
* Extends the native Array class with additional utility methods for manipulation,
|
|
16
|
+
* validation, and statistical analysis.
|
|
17
|
+
*/
|
|
18
|
+
declare class EnhancedArray<T> extends Array<T> {
|
|
19
|
+
protected originalItems: T[];
|
|
20
|
+
/**
|
|
21
|
+
* Create a new EnhancedArray instance.
|
|
22
|
+
*
|
|
23
|
+
* @param {...T[]} items The items to initialize the collection with
|
|
24
|
+
*/
|
|
25
|
+
constructor(...items: T[]);
|
|
26
|
+
/**
|
|
27
|
+
* Determine if the collection has been modified since initialization or last sync.
|
|
28
|
+
*
|
|
29
|
+
* @returns {boolean} True if modified, false otherwise
|
|
30
|
+
*/
|
|
31
|
+
isDirty(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Determine if the collection is clean (not modified).
|
|
34
|
+
*
|
|
35
|
+
* @returns {boolean} True if not modified, false otherwise
|
|
36
|
+
*/
|
|
37
|
+
isClean(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Sync the original items with current items to mark the collection as clean.
|
|
40
|
+
*
|
|
41
|
+
* @returns {this} The collection instance
|
|
42
|
+
*/
|
|
43
|
+
syncOriginal(): this;
|
|
44
|
+
/**
|
|
45
|
+
* Compare with another array or collection for equality.
|
|
46
|
+
*
|
|
47
|
+
* @param {T[] | EnhancedArray<T>} other The array or collection to compare with
|
|
48
|
+
* @returns {boolean} True if equivalent, false otherwise
|
|
49
|
+
*/
|
|
50
|
+
isEquivalent(other: T[] | EnhancedArray<T>): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Create an EnhancedArray from an iterable or array-like object.
|
|
53
|
+
*
|
|
54
|
+
* @param {Iterable<T> | ArrayLike<T>} items The items to convert
|
|
55
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
56
|
+
*/
|
|
57
|
+
static from<T>(items: Iterable<T> | ArrayLike<T>): EnhancedArray<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Create an EnhancedArray from a variable number of arguments.
|
|
60
|
+
*
|
|
61
|
+
* @param {...T[]} items The items to include
|
|
62
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
63
|
+
*/
|
|
64
|
+
static of<T>(...items: T[]): EnhancedArray<T>;
|
|
65
|
+
/**
|
|
66
|
+
* Create a new collection instance from the current items.
|
|
67
|
+
*
|
|
68
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
69
|
+
*/
|
|
70
|
+
collect(): EnhancedArray<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Pluck an array of values from an array of objects.
|
|
73
|
+
*
|
|
74
|
+
* @param {K} key The key to pluck
|
|
75
|
+
* @returns {EnhancedArray<T[K]>} A new collection of plucked values
|
|
76
|
+
*/
|
|
77
|
+
pluck<K extends keyof T>(key: K): EnhancedArray<T[K]>;
|
|
78
|
+
/**
|
|
79
|
+
* Group the collection's items by a given key or callback.
|
|
80
|
+
*
|
|
81
|
+
* @param {K | Callback<T, string | number>} key The key or callback to group by
|
|
82
|
+
* @returns {Record<string, EnhancedArray<T>>} A record of grouped collections
|
|
83
|
+
*/
|
|
84
|
+
groupBy<K extends keyof T>(key: K | Callback<T, string | number>): Record<string, EnhancedArray<T>>;
|
|
85
|
+
/**
|
|
86
|
+
* Return only unique items from the collection.
|
|
87
|
+
*
|
|
88
|
+
* @param {K} [key] Optional key to determine uniqueness for objects
|
|
89
|
+
* @returns {EnhancedArray<T>} A new collection of unique items
|
|
90
|
+
*/
|
|
91
|
+
unique<K extends keyof T>(key?: K): EnhancedArray<T>;
|
|
92
|
+
/**
|
|
93
|
+
* Chunk the collection into smaller collections of a given size.
|
|
94
|
+
*
|
|
95
|
+
* @param {number} size The size of each chunk
|
|
96
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
97
|
+
*/
|
|
98
|
+
chunk(size: number): EnhancedArray<EnhancedArray<T>>;
|
|
99
|
+
/**
|
|
100
|
+
* Flatten a multi-dimensional collection into a single level.
|
|
101
|
+
*
|
|
102
|
+
* @param {number} [depth=Infinity] The depth to flatten to
|
|
103
|
+
* @returns {EnhancedArray<unknown>} A new flattened collection
|
|
104
|
+
*/
|
|
105
|
+
deepFlatten(depth?: number): EnhancedArray<unknown>;
|
|
106
|
+
/**
|
|
107
|
+
* Rotate the collection by a given number of positions.
|
|
108
|
+
*
|
|
109
|
+
* @param {number} [positions=1] The number of positions to rotate
|
|
110
|
+
* @returns {EnhancedArray<T>} A new rotated collection
|
|
111
|
+
*/
|
|
112
|
+
rotate(positions?: number): EnhancedArray<T>;
|
|
113
|
+
/**
|
|
114
|
+
* Chunk the collection as long as the given predicate returns true.
|
|
115
|
+
*
|
|
116
|
+
* @param {Predicate<T>} predicate The predicate to check
|
|
117
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
118
|
+
*/
|
|
119
|
+
chunkWhile(predicate: Predicate<T>): EnhancedArray<EnhancedArray<T>>;
|
|
120
|
+
/**
|
|
121
|
+
* Transpose the collection (swap rows and columns).
|
|
122
|
+
*
|
|
123
|
+
* @returns {EnhancedArray<EnhancedArray<unknown>>} A new transposed collection
|
|
124
|
+
*/
|
|
125
|
+
transpose(): EnhancedArray<EnhancedArray<unknown>>;
|
|
126
|
+
/**
|
|
127
|
+
* Check if the collection is sorted.
|
|
128
|
+
*
|
|
129
|
+
* @param {Function} [compareFn] Optional comparison function
|
|
130
|
+
* @returns {boolean} True if sorted, false otherwise
|
|
131
|
+
*/
|
|
132
|
+
isSorted(compareFn?: (a: T, b: T) => number): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Check if the collection contains duplicate items.
|
|
135
|
+
*
|
|
136
|
+
* @param {K} [key] Optional key to check for uniqueness in objects
|
|
137
|
+
* @returns {boolean} True if duplicates found, false otherwise
|
|
138
|
+
*/
|
|
139
|
+
hasDuplicates<K extends keyof T>(key?: K): boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Calculate the median value of the collection.
|
|
142
|
+
*
|
|
143
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
144
|
+
* @returns {number | null} The median value or null if collection is empty
|
|
145
|
+
*/
|
|
146
|
+
median(accessor?: Callback<T, number>): number | null;
|
|
147
|
+
/**
|
|
148
|
+
* Calculate the mode (most frequent values) of the collection.
|
|
149
|
+
*
|
|
150
|
+
* @param {Callback<T, unknown>} [accessor] Optional accessor for the values
|
|
151
|
+
* @returns {EnhancedArray<unknown>} A collection of mode values
|
|
152
|
+
*/
|
|
153
|
+
mode(accessor?: Callback<T, unknown>): EnhancedArray<unknown>;
|
|
154
|
+
/**
|
|
155
|
+
* Calculate the standard deviation of the collection.
|
|
156
|
+
*
|
|
157
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
158
|
+
* @returns {number} The standard deviation
|
|
159
|
+
*/
|
|
160
|
+
standardDeviation(accessor?: Callback<T, number>): number;
|
|
161
|
+
/**
|
|
162
|
+
* Calculate a specific percentile of the collection.
|
|
163
|
+
*
|
|
164
|
+
* @param {number} percentile The percentile to calculate (0-100)
|
|
165
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
166
|
+
* @returns {number | null} The percentile value or null if collection is empty
|
|
167
|
+
*/
|
|
168
|
+
percentile(percentile: number, accessor?: Callback<T, number>): number | null;
|
|
169
|
+
/**
|
|
170
|
+
* Calculate the frequencies of items in the collection.
|
|
171
|
+
*
|
|
172
|
+
* @param {Callback<T, unknown>} [accessor] Optional accessor for the values
|
|
173
|
+
* @returns {Record<string, number>} A record of values and their frequencies
|
|
174
|
+
*/
|
|
175
|
+
frequencies(accessor?: Callback<T, unknown>): Record<string, number>;
|
|
176
|
+
/**
|
|
177
|
+
* Calculate the Cartesian product of the collection and other arrays.
|
|
178
|
+
*
|
|
179
|
+
* @param {...U[][]} arrays The arrays to calculate the product with
|
|
180
|
+
* @returns {EnhancedArray<[T, ...U[]]>} A collection of product combinations
|
|
181
|
+
*/
|
|
182
|
+
cartesian<U>(...arrays: U[][]): EnhancedArray<[T, ...U[]]>;
|
|
183
|
+
/**
|
|
184
|
+
* Interleave the collection with other arrays.
|
|
185
|
+
*
|
|
186
|
+
* @param {...U[][]} arrays The arrays to interleave with
|
|
187
|
+
* @returns {EnhancedArray<T | U>} A new interleaved collection
|
|
188
|
+
*/
|
|
189
|
+
interleave<U>(...arrays: U[][]): EnhancedArray<T | U>;
|
|
190
|
+
/**
|
|
191
|
+
* Get a sliding window of items from the collection.
|
|
192
|
+
*
|
|
193
|
+
* @param {number} [size=2] The size of the window
|
|
194
|
+
* @param {number} [step=1] The step between windows
|
|
195
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of window collections
|
|
196
|
+
*/
|
|
197
|
+
sliding(size?: number, step?: number): EnhancedArray<EnhancedArray<T>>;
|
|
198
|
+
/**
|
|
199
|
+
* Get the item at the given index.
|
|
200
|
+
*
|
|
201
|
+
* @param {number} index The index to retrieve
|
|
202
|
+
* @returns {T | undefined} The item at the index or undefined
|
|
203
|
+
*/
|
|
204
|
+
at(index: number): T | undefined;
|
|
205
|
+
/**
|
|
206
|
+
* Get the item before the given item.
|
|
207
|
+
*
|
|
208
|
+
* @param {T} item The item to search for
|
|
209
|
+
* @returns {T | undefined} The item before or undefined
|
|
210
|
+
*/
|
|
211
|
+
before(item: T): T | undefined;
|
|
212
|
+
/**
|
|
213
|
+
* Get the item after the given item.
|
|
214
|
+
*
|
|
215
|
+
* @param {T} item The item to search for
|
|
216
|
+
* @returns {T | undefined} The item after or undefined
|
|
217
|
+
*/
|
|
218
|
+
after(item: T): T | undefined;
|
|
219
|
+
/**
|
|
220
|
+
* Chunk the collection by a given key or callback when its value changes.
|
|
221
|
+
*
|
|
222
|
+
* @param {K | Callback<T, unknown>} key The key or callback to chunk by
|
|
223
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
224
|
+
*/
|
|
225
|
+
chunkBy<K extends keyof T>(key: K | Callback<T, unknown>): EnhancedArray<EnhancedArray<T>>;
|
|
226
|
+
/**
|
|
227
|
+
* Map and filter the collection simultaneously.
|
|
228
|
+
* Items for which the callback returns null or undefined are removed.
|
|
229
|
+
*
|
|
230
|
+
* @param {Function} callback The callback to map items
|
|
231
|
+
* @returns {EnhancedArray<U>} A new mapped and filtered collection
|
|
232
|
+
*/
|
|
233
|
+
filterMap<U>(callback: (item: T, index: number) => U | null | undefined): EnhancedArray<U>;
|
|
234
|
+
/**
|
|
235
|
+
* Extract a subset of properties from each item in the collection.
|
|
236
|
+
*
|
|
237
|
+
* @param {K[]} keys The keys to extract
|
|
238
|
+
* @returns {EnhancedArray<Partial<T>>} A new collection of partial items
|
|
239
|
+
*/
|
|
240
|
+
extract<K extends keyof T>(keys: K[]): EnhancedArray<Partial<T>>;
|
|
241
|
+
/**
|
|
242
|
+
* Get the first item or push a default item if the collection is empty.
|
|
243
|
+
*
|
|
244
|
+
* @param {T} item The default item to push if empty
|
|
245
|
+
* @returns {T} The first item or the pushed item
|
|
246
|
+
*/
|
|
247
|
+
firstOrPush(item: T): T;
|
|
248
|
+
/**
|
|
249
|
+
* Get every n-th item from the collection.
|
|
250
|
+
*
|
|
251
|
+
* @param {number} n The step size
|
|
252
|
+
* @returns {EnhancedArray<T>} A new collection of every n-th item
|
|
253
|
+
*/
|
|
254
|
+
getNth(n: number): EnhancedArray<T>;
|
|
255
|
+
/**
|
|
256
|
+
* Get a random item from the collection based on weights.
|
|
257
|
+
*
|
|
258
|
+
* @param {number[]} [weights] Optional weights for each item
|
|
259
|
+
* @returns {T | undefined} A random item or undefined if collection is empty
|
|
260
|
+
*/
|
|
261
|
+
weightedRandom(weights?: number[]): T | undefined;
|
|
262
|
+
/**
|
|
263
|
+
* Paginate the collection items.
|
|
264
|
+
*
|
|
265
|
+
* @param {number} perPage Items per page
|
|
266
|
+
* @param {number} [page=1] Current page number
|
|
267
|
+
* @returns {Object} Pagination results
|
|
268
|
+
*/
|
|
269
|
+
paginate(perPage: number, page?: number): {
|
|
270
|
+
data: EnhancedArray<T>;
|
|
271
|
+
total: number;
|
|
272
|
+
perPage: number;
|
|
273
|
+
currentPage: number;
|
|
274
|
+
lastPage: number;
|
|
275
|
+
from: number;
|
|
276
|
+
to: number;
|
|
277
|
+
};
|
|
278
|
+
/**
|
|
279
|
+
* Recursively convert all nested arrays to EnhancedArray instances.
|
|
280
|
+
*
|
|
281
|
+
* @returns {EnhancedArray<unknown>} A new collection with recursive EnhancedArrays
|
|
282
|
+
*/
|
|
283
|
+
recursive(): EnhancedArray<unknown>;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Convert a plain object to a Map.
|
|
287
|
+
*
|
|
288
|
+
* @param {Record<string, T>} obj The object to convert
|
|
289
|
+
* @returns {Map<string, T>} A new Map instance
|
|
290
|
+
*/
|
|
291
|
+
declare const objectToMap: <T>(obj: Record<string, T>) => Map<string, T>;
|
|
292
|
+
/**
|
|
293
|
+
* Convert a Map to a plain object.
|
|
294
|
+
*
|
|
295
|
+
* @param {Map<string, T>} map The Map to convert
|
|
296
|
+
* @returns {Record<string, T>} A new plain object
|
|
297
|
+
*/
|
|
298
|
+
declare const mapToObject: <T>(map: Map<string, T>) => Record<string, T>;
|
|
299
|
+
/**
|
|
300
|
+
* Enhanced Map class with additional collection-like methods.
|
|
301
|
+
* Extends the native Map class.
|
|
302
|
+
*/
|
|
303
|
+
declare class EnhancedMap<K, V> extends Map<K, V> {
|
|
304
|
+
/**
|
|
305
|
+
* Create an EnhancedMap from a plain object.
|
|
306
|
+
*
|
|
307
|
+
* @param {Record<string, V>} obj The object to convert
|
|
308
|
+
* @returns {EnhancedMap<string, V>} A new EnhancedMap instance
|
|
309
|
+
*/
|
|
310
|
+
static fromObject<V>(obj: Record<string, V>): EnhancedMap<string, V>;
|
|
311
|
+
/**
|
|
312
|
+
* Pluck a property from each value in the map.
|
|
313
|
+
*
|
|
314
|
+
* @param {P} key The key to pluck
|
|
315
|
+
* @returns {EnhancedArray<V[P]>} A new collection of plucked values
|
|
316
|
+
*/
|
|
317
|
+
pluck<P extends keyof V>(key: P): EnhancedArray<V[P]>;
|
|
318
|
+
/**
|
|
319
|
+
* Group the map's entries by a given accessor.
|
|
320
|
+
*
|
|
321
|
+
* @param {Function} accessor The accessor to group by
|
|
322
|
+
* @returns {Record<string, EnhancedArray<[K, V]>>} A record of grouped entries
|
|
323
|
+
*/
|
|
324
|
+
groupBy(accessor: (value: V, key: K) => string | number): Record<string, EnhancedArray<[K, V]>>;
|
|
325
|
+
/**
|
|
326
|
+
* Convert the map to an array of entries.
|
|
327
|
+
*
|
|
328
|
+
* @returns {EnhancedArray<[K, V]>} A collection of entries
|
|
329
|
+
*/
|
|
330
|
+
toArray(): EnhancedArray<[K, V]>;
|
|
331
|
+
/**
|
|
332
|
+
* Convert the map to a plain object.
|
|
333
|
+
*
|
|
334
|
+
* @returns {Record<string, V>} A plain object representation of the map
|
|
335
|
+
*/
|
|
336
|
+
toObject(): Record<string, V>;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Create a new EnhancedArray from an array of items.
|
|
340
|
+
*
|
|
341
|
+
* @param {T[]} items The items to collect
|
|
342
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
343
|
+
*/
|
|
344
|
+
declare const collect: <T>(items: T[]) => EnhancedArray<T>;
|
|
345
|
+
/**
|
|
346
|
+
* Create a new EnhancedMap from an iterable of entries.
|
|
347
|
+
*
|
|
348
|
+
* @param {Iterable<[K, V]>} [entries] Optional entries to initialize the map
|
|
349
|
+
* @returns {EnhancedMap<K, V>} A new EnhancedMap instance
|
|
350
|
+
*/
|
|
351
|
+
declare const collectMap: <K, V>(entries?: Iterable<[K, V]>) => EnhancedMap<K, V>;
|
|
352
|
+
|
|
353
|
+
export { type Callback, EnhancedArray, EnhancedMap, type Predicate, collect, collectMap, mapToObject, objectToMap };
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collection utilities for JavaScript arrays and objects
|
|
3
|
+
* Inspired by Laravel Collections with modern JavaScript features
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Generic callback type for collection operations.
|
|
7
|
+
*/
|
|
8
|
+
type Callback<T, R> = (item: T, index: number) => R;
|
|
9
|
+
/**
|
|
10
|
+
* Predicate type for collection filtering and validation.
|
|
11
|
+
*/
|
|
12
|
+
type Predicate<T> = (item: T, index: number) => boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Enhanced Array class with Laravel Collection-like methods.
|
|
15
|
+
* Extends the native Array class with additional utility methods for manipulation,
|
|
16
|
+
* validation, and statistical analysis.
|
|
17
|
+
*/
|
|
18
|
+
declare class EnhancedArray<T> extends Array<T> {
|
|
19
|
+
protected originalItems: T[];
|
|
20
|
+
/**
|
|
21
|
+
* Create a new EnhancedArray instance.
|
|
22
|
+
*
|
|
23
|
+
* @param {...T[]} items The items to initialize the collection with
|
|
24
|
+
*/
|
|
25
|
+
constructor(...items: T[]);
|
|
26
|
+
/**
|
|
27
|
+
* Determine if the collection has been modified since initialization or last sync.
|
|
28
|
+
*
|
|
29
|
+
* @returns {boolean} True if modified, false otherwise
|
|
30
|
+
*/
|
|
31
|
+
isDirty(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Determine if the collection is clean (not modified).
|
|
34
|
+
*
|
|
35
|
+
* @returns {boolean} True if not modified, false otherwise
|
|
36
|
+
*/
|
|
37
|
+
isClean(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Sync the original items with current items to mark the collection as clean.
|
|
40
|
+
*
|
|
41
|
+
* @returns {this} The collection instance
|
|
42
|
+
*/
|
|
43
|
+
syncOriginal(): this;
|
|
44
|
+
/**
|
|
45
|
+
* Compare with another array or collection for equality.
|
|
46
|
+
*
|
|
47
|
+
* @param {T[] | EnhancedArray<T>} other The array or collection to compare with
|
|
48
|
+
* @returns {boolean} True if equivalent, false otherwise
|
|
49
|
+
*/
|
|
50
|
+
isEquivalent(other: T[] | EnhancedArray<T>): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Create an EnhancedArray from an iterable or array-like object.
|
|
53
|
+
*
|
|
54
|
+
* @param {Iterable<T> | ArrayLike<T>} items The items to convert
|
|
55
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
56
|
+
*/
|
|
57
|
+
static from<T>(items: Iterable<T> | ArrayLike<T>): EnhancedArray<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Create an EnhancedArray from a variable number of arguments.
|
|
60
|
+
*
|
|
61
|
+
* @param {...T[]} items The items to include
|
|
62
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
63
|
+
*/
|
|
64
|
+
static of<T>(...items: T[]): EnhancedArray<T>;
|
|
65
|
+
/**
|
|
66
|
+
* Create a new collection instance from the current items.
|
|
67
|
+
*
|
|
68
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
69
|
+
*/
|
|
70
|
+
collect(): EnhancedArray<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Pluck an array of values from an array of objects.
|
|
73
|
+
*
|
|
74
|
+
* @param {K} key The key to pluck
|
|
75
|
+
* @returns {EnhancedArray<T[K]>} A new collection of plucked values
|
|
76
|
+
*/
|
|
77
|
+
pluck<K extends keyof T>(key: K): EnhancedArray<T[K]>;
|
|
78
|
+
/**
|
|
79
|
+
* Group the collection's items by a given key or callback.
|
|
80
|
+
*
|
|
81
|
+
* @param {K | Callback<T, string | number>} key The key or callback to group by
|
|
82
|
+
* @returns {Record<string, EnhancedArray<T>>} A record of grouped collections
|
|
83
|
+
*/
|
|
84
|
+
groupBy<K extends keyof T>(key: K | Callback<T, string | number>): Record<string, EnhancedArray<T>>;
|
|
85
|
+
/**
|
|
86
|
+
* Return only unique items from the collection.
|
|
87
|
+
*
|
|
88
|
+
* @param {K} [key] Optional key to determine uniqueness for objects
|
|
89
|
+
* @returns {EnhancedArray<T>} A new collection of unique items
|
|
90
|
+
*/
|
|
91
|
+
unique<K extends keyof T>(key?: K): EnhancedArray<T>;
|
|
92
|
+
/**
|
|
93
|
+
* Chunk the collection into smaller collections of a given size.
|
|
94
|
+
*
|
|
95
|
+
* @param {number} size The size of each chunk
|
|
96
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
97
|
+
*/
|
|
98
|
+
chunk(size: number): EnhancedArray<EnhancedArray<T>>;
|
|
99
|
+
/**
|
|
100
|
+
* Flatten a multi-dimensional collection into a single level.
|
|
101
|
+
*
|
|
102
|
+
* @param {number} [depth=Infinity] The depth to flatten to
|
|
103
|
+
* @returns {EnhancedArray<unknown>} A new flattened collection
|
|
104
|
+
*/
|
|
105
|
+
deepFlatten(depth?: number): EnhancedArray<unknown>;
|
|
106
|
+
/**
|
|
107
|
+
* Rotate the collection by a given number of positions.
|
|
108
|
+
*
|
|
109
|
+
* @param {number} [positions=1] The number of positions to rotate
|
|
110
|
+
* @returns {EnhancedArray<T>} A new rotated collection
|
|
111
|
+
*/
|
|
112
|
+
rotate(positions?: number): EnhancedArray<T>;
|
|
113
|
+
/**
|
|
114
|
+
* Chunk the collection as long as the given predicate returns true.
|
|
115
|
+
*
|
|
116
|
+
* @param {Predicate<T>} predicate The predicate to check
|
|
117
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
118
|
+
*/
|
|
119
|
+
chunkWhile(predicate: Predicate<T>): EnhancedArray<EnhancedArray<T>>;
|
|
120
|
+
/**
|
|
121
|
+
* Transpose the collection (swap rows and columns).
|
|
122
|
+
*
|
|
123
|
+
* @returns {EnhancedArray<EnhancedArray<unknown>>} A new transposed collection
|
|
124
|
+
*/
|
|
125
|
+
transpose(): EnhancedArray<EnhancedArray<unknown>>;
|
|
126
|
+
/**
|
|
127
|
+
* Check if the collection is sorted.
|
|
128
|
+
*
|
|
129
|
+
* @param {Function} [compareFn] Optional comparison function
|
|
130
|
+
* @returns {boolean} True if sorted, false otherwise
|
|
131
|
+
*/
|
|
132
|
+
isSorted(compareFn?: (a: T, b: T) => number): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Check if the collection contains duplicate items.
|
|
135
|
+
*
|
|
136
|
+
* @param {K} [key] Optional key to check for uniqueness in objects
|
|
137
|
+
* @returns {boolean} True if duplicates found, false otherwise
|
|
138
|
+
*/
|
|
139
|
+
hasDuplicates<K extends keyof T>(key?: K): boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Calculate the median value of the collection.
|
|
142
|
+
*
|
|
143
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
144
|
+
* @returns {number | null} The median value or null if collection is empty
|
|
145
|
+
*/
|
|
146
|
+
median(accessor?: Callback<T, number>): number | null;
|
|
147
|
+
/**
|
|
148
|
+
* Calculate the mode (most frequent values) of the collection.
|
|
149
|
+
*
|
|
150
|
+
* @param {Callback<T, unknown>} [accessor] Optional accessor for the values
|
|
151
|
+
* @returns {EnhancedArray<unknown>} A collection of mode values
|
|
152
|
+
*/
|
|
153
|
+
mode(accessor?: Callback<T, unknown>): EnhancedArray<unknown>;
|
|
154
|
+
/**
|
|
155
|
+
* Calculate the standard deviation of the collection.
|
|
156
|
+
*
|
|
157
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
158
|
+
* @returns {number} The standard deviation
|
|
159
|
+
*/
|
|
160
|
+
standardDeviation(accessor?: Callback<T, number>): number;
|
|
161
|
+
/**
|
|
162
|
+
* Calculate a specific percentile of the collection.
|
|
163
|
+
*
|
|
164
|
+
* @param {number} percentile The percentile to calculate (0-100)
|
|
165
|
+
* @param {Callback<T, number>} [accessor] Optional accessor for non-numeric items
|
|
166
|
+
* @returns {number | null} The percentile value or null if collection is empty
|
|
167
|
+
*/
|
|
168
|
+
percentile(percentile: number, accessor?: Callback<T, number>): number | null;
|
|
169
|
+
/**
|
|
170
|
+
* Calculate the frequencies of items in the collection.
|
|
171
|
+
*
|
|
172
|
+
* @param {Callback<T, unknown>} [accessor] Optional accessor for the values
|
|
173
|
+
* @returns {Record<string, number>} A record of values and their frequencies
|
|
174
|
+
*/
|
|
175
|
+
frequencies(accessor?: Callback<T, unknown>): Record<string, number>;
|
|
176
|
+
/**
|
|
177
|
+
* Calculate the Cartesian product of the collection and other arrays.
|
|
178
|
+
*
|
|
179
|
+
* @param {...U[][]} arrays The arrays to calculate the product with
|
|
180
|
+
* @returns {EnhancedArray<[T, ...U[]]>} A collection of product combinations
|
|
181
|
+
*/
|
|
182
|
+
cartesian<U>(...arrays: U[][]): EnhancedArray<[T, ...U[]]>;
|
|
183
|
+
/**
|
|
184
|
+
* Interleave the collection with other arrays.
|
|
185
|
+
*
|
|
186
|
+
* @param {...U[][]} arrays The arrays to interleave with
|
|
187
|
+
* @returns {EnhancedArray<T | U>} A new interleaved collection
|
|
188
|
+
*/
|
|
189
|
+
interleave<U>(...arrays: U[][]): EnhancedArray<T | U>;
|
|
190
|
+
/**
|
|
191
|
+
* Get a sliding window of items from the collection.
|
|
192
|
+
*
|
|
193
|
+
* @param {number} [size=2] The size of the window
|
|
194
|
+
* @param {number} [step=1] The step between windows
|
|
195
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of window collections
|
|
196
|
+
*/
|
|
197
|
+
sliding(size?: number, step?: number): EnhancedArray<EnhancedArray<T>>;
|
|
198
|
+
/**
|
|
199
|
+
* Get the item at the given index.
|
|
200
|
+
*
|
|
201
|
+
* @param {number} index The index to retrieve
|
|
202
|
+
* @returns {T | undefined} The item at the index or undefined
|
|
203
|
+
*/
|
|
204
|
+
at(index: number): T | undefined;
|
|
205
|
+
/**
|
|
206
|
+
* Get the item before the given item.
|
|
207
|
+
*
|
|
208
|
+
* @param {T} item The item to search for
|
|
209
|
+
* @returns {T | undefined} The item before or undefined
|
|
210
|
+
*/
|
|
211
|
+
before(item: T): T | undefined;
|
|
212
|
+
/**
|
|
213
|
+
* Get the item after the given item.
|
|
214
|
+
*
|
|
215
|
+
* @param {T} item The item to search for
|
|
216
|
+
* @returns {T | undefined} The item after or undefined
|
|
217
|
+
*/
|
|
218
|
+
after(item: T): T | undefined;
|
|
219
|
+
/**
|
|
220
|
+
* Chunk the collection by a given key or callback when its value changes.
|
|
221
|
+
*
|
|
222
|
+
* @param {K | Callback<T, unknown>} key The key or callback to chunk by
|
|
223
|
+
* @returns {EnhancedArray<EnhancedArray<T>>} A collection of chunked collections
|
|
224
|
+
*/
|
|
225
|
+
chunkBy<K extends keyof T>(key: K | Callback<T, unknown>): EnhancedArray<EnhancedArray<T>>;
|
|
226
|
+
/**
|
|
227
|
+
* Map and filter the collection simultaneously.
|
|
228
|
+
* Items for which the callback returns null or undefined are removed.
|
|
229
|
+
*
|
|
230
|
+
* @param {Function} callback The callback to map items
|
|
231
|
+
* @returns {EnhancedArray<U>} A new mapped and filtered collection
|
|
232
|
+
*/
|
|
233
|
+
filterMap<U>(callback: (item: T, index: number) => U | null | undefined): EnhancedArray<U>;
|
|
234
|
+
/**
|
|
235
|
+
* Extract a subset of properties from each item in the collection.
|
|
236
|
+
*
|
|
237
|
+
* @param {K[]} keys The keys to extract
|
|
238
|
+
* @returns {EnhancedArray<Partial<T>>} A new collection of partial items
|
|
239
|
+
*/
|
|
240
|
+
extract<K extends keyof T>(keys: K[]): EnhancedArray<Partial<T>>;
|
|
241
|
+
/**
|
|
242
|
+
* Get the first item or push a default item if the collection is empty.
|
|
243
|
+
*
|
|
244
|
+
* @param {T} item The default item to push if empty
|
|
245
|
+
* @returns {T} The first item or the pushed item
|
|
246
|
+
*/
|
|
247
|
+
firstOrPush(item: T): T;
|
|
248
|
+
/**
|
|
249
|
+
* Get every n-th item from the collection.
|
|
250
|
+
*
|
|
251
|
+
* @param {number} n The step size
|
|
252
|
+
* @returns {EnhancedArray<T>} A new collection of every n-th item
|
|
253
|
+
*/
|
|
254
|
+
getNth(n: number): EnhancedArray<T>;
|
|
255
|
+
/**
|
|
256
|
+
* Get a random item from the collection based on weights.
|
|
257
|
+
*
|
|
258
|
+
* @param {number[]} [weights] Optional weights for each item
|
|
259
|
+
* @returns {T | undefined} A random item or undefined if collection is empty
|
|
260
|
+
*/
|
|
261
|
+
weightedRandom(weights?: number[]): T | undefined;
|
|
262
|
+
/**
|
|
263
|
+
* Paginate the collection items.
|
|
264
|
+
*
|
|
265
|
+
* @param {number} perPage Items per page
|
|
266
|
+
* @param {number} [page=1] Current page number
|
|
267
|
+
* @returns {Object} Pagination results
|
|
268
|
+
*/
|
|
269
|
+
paginate(perPage: number, page?: number): {
|
|
270
|
+
data: EnhancedArray<T>;
|
|
271
|
+
total: number;
|
|
272
|
+
perPage: number;
|
|
273
|
+
currentPage: number;
|
|
274
|
+
lastPage: number;
|
|
275
|
+
from: number;
|
|
276
|
+
to: number;
|
|
277
|
+
};
|
|
278
|
+
/**
|
|
279
|
+
* Recursively convert all nested arrays to EnhancedArray instances.
|
|
280
|
+
*
|
|
281
|
+
* @returns {EnhancedArray<unknown>} A new collection with recursive EnhancedArrays
|
|
282
|
+
*/
|
|
283
|
+
recursive(): EnhancedArray<unknown>;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Convert a plain object to a Map.
|
|
287
|
+
*
|
|
288
|
+
* @param {Record<string, T>} obj The object to convert
|
|
289
|
+
* @returns {Map<string, T>} A new Map instance
|
|
290
|
+
*/
|
|
291
|
+
declare const objectToMap: <T>(obj: Record<string, T>) => Map<string, T>;
|
|
292
|
+
/**
|
|
293
|
+
* Convert a Map to a plain object.
|
|
294
|
+
*
|
|
295
|
+
* @param {Map<string, T>} map The Map to convert
|
|
296
|
+
* @returns {Record<string, T>} A new plain object
|
|
297
|
+
*/
|
|
298
|
+
declare const mapToObject: <T>(map: Map<string, T>) => Record<string, T>;
|
|
299
|
+
/**
|
|
300
|
+
* Enhanced Map class with additional collection-like methods.
|
|
301
|
+
* Extends the native Map class.
|
|
302
|
+
*/
|
|
303
|
+
declare class EnhancedMap<K, V> extends Map<K, V> {
|
|
304
|
+
/**
|
|
305
|
+
* Create an EnhancedMap from a plain object.
|
|
306
|
+
*
|
|
307
|
+
* @param {Record<string, V>} obj The object to convert
|
|
308
|
+
* @returns {EnhancedMap<string, V>} A new EnhancedMap instance
|
|
309
|
+
*/
|
|
310
|
+
static fromObject<V>(obj: Record<string, V>): EnhancedMap<string, V>;
|
|
311
|
+
/**
|
|
312
|
+
* Pluck a property from each value in the map.
|
|
313
|
+
*
|
|
314
|
+
* @param {P} key The key to pluck
|
|
315
|
+
* @returns {EnhancedArray<V[P]>} A new collection of plucked values
|
|
316
|
+
*/
|
|
317
|
+
pluck<P extends keyof V>(key: P): EnhancedArray<V[P]>;
|
|
318
|
+
/**
|
|
319
|
+
* Group the map's entries by a given accessor.
|
|
320
|
+
*
|
|
321
|
+
* @param {Function} accessor The accessor to group by
|
|
322
|
+
* @returns {Record<string, EnhancedArray<[K, V]>>} A record of grouped entries
|
|
323
|
+
*/
|
|
324
|
+
groupBy(accessor: (value: V, key: K) => string | number): Record<string, EnhancedArray<[K, V]>>;
|
|
325
|
+
/**
|
|
326
|
+
* Convert the map to an array of entries.
|
|
327
|
+
*
|
|
328
|
+
* @returns {EnhancedArray<[K, V]>} A collection of entries
|
|
329
|
+
*/
|
|
330
|
+
toArray(): EnhancedArray<[K, V]>;
|
|
331
|
+
/**
|
|
332
|
+
* Convert the map to a plain object.
|
|
333
|
+
*
|
|
334
|
+
* @returns {Record<string, V>} A plain object representation of the map
|
|
335
|
+
*/
|
|
336
|
+
toObject(): Record<string, V>;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Create a new EnhancedArray from an array of items.
|
|
340
|
+
*
|
|
341
|
+
* @param {T[]} items The items to collect
|
|
342
|
+
* @returns {EnhancedArray<T>} A new EnhancedArray instance
|
|
343
|
+
*/
|
|
344
|
+
declare const collect: <T>(items: T[]) => EnhancedArray<T>;
|
|
345
|
+
/**
|
|
346
|
+
* Create a new EnhancedMap from an iterable of entries.
|
|
347
|
+
*
|
|
348
|
+
* @param {Iterable<[K, V]>} [entries] Optional entries to initialize the map
|
|
349
|
+
* @returns {EnhancedMap<K, V>} A new EnhancedMap instance
|
|
350
|
+
*/
|
|
351
|
+
declare const collectMap: <K, V>(entries?: Iterable<[K, V]>) => EnhancedMap<K, V>;
|
|
352
|
+
|
|
353
|
+
export { type Callback, EnhancedArray, EnhancedMap, type Predicate, collect, collectMap, mapToObject, objectToMap };
|