@r01al/array-polyfills 1.0.5 → 1.0.7

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 (48) hide show
  1. package/README.md +474 -2
  2. package/dist/auto.cjs +262 -0
  3. package/dist/auto.cjs.map +1 -1
  4. package/dist/auto.mjs +262 -0
  5. package/dist/auto.mjs.map +1 -1
  6. package/dist/auto.umd.js +530 -0
  7. package/dist/auto.umd.js.map +1 -0
  8. package/dist/functions/compact.d.ts +7 -0
  9. package/dist/functions/compact.js +7 -0
  10. package/dist/functions/compactMap.d.ts +6 -0
  11. package/dist/functions/compactMap.js +15 -0
  12. package/dist/functions/countBy.d.ts +6 -0
  13. package/dist/functions/countBy.js +17 -0
  14. package/dist/functions/difference.d.ts +6 -0
  15. package/dist/functions/difference.js +15 -0
  16. package/dist/functions/flatten.d.ts +5 -0
  17. package/dist/functions/flatten.js +14 -0
  18. package/dist/functions/groupBy.d.ts +6 -0
  19. package/dist/functions/groupBy.js +19 -0
  20. package/dist/functions/intersection.d.ts +6 -0
  21. package/dist/functions/intersection.js +11 -0
  22. package/dist/functions/pad.d.ts +7 -0
  23. package/dist/functions/pad.js +15 -0
  24. package/dist/functions/partition.d.ts +6 -0
  25. package/dist/functions/partition.js +17 -0
  26. package/dist/functions/pluck.d.ts +6 -0
  27. package/dist/functions/pluck.js +8 -0
  28. package/dist/functions/sample.d.ts +6 -0
  29. package/dist/functions/sample.js +16 -0
  30. package/dist/functions/sortBy.d.ts +6 -0
  31. package/dist/functions/sortBy.js +20 -0
  32. package/dist/functions/union.d.ts +6 -0
  33. package/dist/functions/union.js +22 -0
  34. package/dist/functions/uniqBy.d.ts +6 -0
  35. package/dist/functions/uniqBy.js +21 -0
  36. package/dist/functions/zip.d.ts +6 -0
  37. package/dist/functions/zip.js +15 -0
  38. package/dist/index.cjs +289 -11
  39. package/dist/index.cjs.map +1 -1
  40. package/dist/index.d.ts +30 -0
  41. package/dist/index.js +42 -11
  42. package/dist/index.mjs +275 -12
  43. package/dist/index.mjs.map +1 -1
  44. package/dist/index.umd.js +580 -0
  45. package/dist/index.umd.js.map +1 -0
  46. package/dist/polyfills/array.d.ts +15 -0
  47. package/dist/polyfills/array.js +30 -0
  48. package/package.json +2 -1
package/README.md CHANGED
@@ -1,2 +1,474 @@
1
- # Cool Array Polyfills
2
- Side-effect polyfills for `Array.prototype` (+ matching polyfills) written in TypeScript.
1
+ # Cool Array Polyfills
2
+
3
+ Side-effect polyfills for `Array.prototype` plus matching standalone helpers, written in TypeScript.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i @r01al/array-polyfills
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ ### Standalone helpers (no prototype changes)
14
+
15
+ ```ts
16
+ import {
17
+ first,
18
+ last,
19
+ unique,
20
+ chunk,
21
+ compact,
22
+ compactMap,
23
+ random,
24
+ keyValueMap,
25
+ sum,
26
+ avg,
27
+ max,
28
+ min,
29
+ shuffle,
30
+ groupBy,
31
+ flatten,
32
+ zip,
33
+ partition,
34
+ pluck,
35
+ countBy,
36
+ difference,
37
+ intersection,
38
+ union,
39
+ uniqBy,
40
+ sortBy,
41
+ sample,
42
+ pad
43
+ } from "@r01al/array-polyfills";
44
+
45
+ first([1, 2, 3]);
46
+ unique([1, 1, 2]);
47
+ chunk([1, 2, 3, 4], 2);
48
+ compact([0, 1, "", 2]);
49
+ compactMap([1, 2, 3], n => (n > 1 ? n * 2 : null));
50
+ groupBy([{ type: "a" }, { type: "b" }], "type");
51
+ flatten([1, [2, 3], 4]);
52
+ zip([1, 2], ["a", "b"]);
53
+ partition([1, 2, 3, 4], n => n % 2 === 0);
54
+ pluck([{ id: 1 }, { id: 2 }], "id");
55
+ countBy(["a", "b", "a"], value => value);
56
+ difference([1, 2, 3], [2, 4]);
57
+ intersection([1, 2, 3], [2, 3, 4]);
58
+ union([1, 2], [2, 3], [3, 4]);
59
+ uniqBy([{ id: 1 }, { id: 1 }, { id: 2 }], "id");
60
+ sortBy([{ n: 2 }, { n: 1 }], "n");
61
+ sample([1, 2, 3, 4], 2);
62
+ pad([1, 2], 5, 0);
63
+ ```
64
+
65
+ ### Polyfill `Array.prototype`
66
+
67
+ ```ts
68
+ import "@r01al/array-polyfills/auto";
69
+
70
+ [1, 2, 3].first();
71
+ [1, 1, 2].unique();
72
+ [1, 2, 3, 4].chunk(2);
73
+ [0, 1, "", 2].compact();
74
+ ([1, 2, 3] as number[]).compactMap(n => (n > 1 ? n * 2 : null));
75
+ [{ type: "a" }, { type: "b" }].groupBy("type");
76
+ ([1, [2, 3], 4] as (number | number[])[]).flatten();
77
+ [1, 2].zip(["a", "b"]);
78
+ [1, 2, 3, 4].partition(n => n % 2 === 0);
79
+ ([{ id: 1 }, { id: 2 }] as { id: number }[]).pluck("id");
80
+ ["a", "b", "a"].countBy(value => value);
81
+ [1, 2, 3].difference([2, 4]);
82
+ [1, 2, 3].intersection([2, 3, 4]);
83
+ [1, 2].union([2, 3], [3, 4]);
84
+ ([{ id: 1 }, { id: 1 }, { id: 2 }] as { id: number }[]).uniqBy("id");
85
+ ([{ n: 2 }, { n: 1 }] as { n: number }[]).sortBy("n");
86
+ [1, 2, 3, 4].sample(2);
87
+ [1, 2].pad(5, 0);
88
+ ```
89
+
90
+ ### Browser (UMD via unpkg)
91
+
92
+ Auto polyfills (no module loader required):
93
+
94
+ ```html
95
+ <script src="https://unpkg.com/@r01al/array-polyfills@latest/dist/auto.umd.js"></script>
96
+ <script>
97
+ [1, 2, 3].first();
98
+ [1, 1, 2].unique();
99
+ </script>
100
+ ```
101
+
102
+ Standalone helpers (global `ArrayPolyfills`):
103
+
104
+ ```html
105
+ <script src="https://unpkg.com/@r01al/array-polyfills@latest/dist/index.umd.js"></script>
106
+ <script>
107
+ ArrayPolyfills.first([1, 2, 3]);
108
+ ArrayPolyfills.shuffle([1, 2, 3]);
109
+ </script>
110
+ ```
111
+
112
+ Note: UMD bundles are ES5-targeted, but older browsers may still require `Map`/`Set` polyfills.
113
+
114
+ ## API
115
+
116
+ All functions below can be used either as standalone helpers or as prototype methods when using the `auto` entry.
117
+
118
+ ### first
119
+
120
+ ```ts
121
+ first<T>(arr: T[]): T | undefined
122
+ ```
123
+
124
+ Returns the first element, or `undefined` if the array is empty.
125
+
126
+ ### last
127
+
128
+ ```ts
129
+ last<T>(arr: T[]): T | undefined
130
+ ```
131
+
132
+ Returns the last element, or `undefined` if the array is empty.
133
+
134
+ ### unique
135
+
136
+ ```ts
137
+ unique<T>(arr: T[]): T[]
138
+ ```
139
+
140
+ Returns a new array with unique elements. Primitives are de-duplicated by value; objects use a deep equality check.
141
+
142
+ ### chunk
143
+
144
+ ```ts
145
+ chunk<T>(arr: T[], size: number): T[][]
146
+ ```
147
+
148
+ Splits the array into chunks of `size`. Throws if `size` is not a positive integer.
149
+
150
+ ### compact
151
+
152
+ ```ts
153
+ compact<T>(arr: T[]): T[]
154
+ ```
155
+
156
+ Returns a new array with all falsy values removed (`false`, `0`, `""`, `null`, `undefined`).
157
+
158
+ Example:
159
+
160
+ ```ts
161
+ compact([0, 1, "", 2, null, undefined, false, 3]);
162
+ // [1, 2, 3]
163
+ ```
164
+
165
+ ### compactMap
166
+
167
+ ```ts
168
+ compactMap<T, R>(
169
+ arr: T[],
170
+ mapper: (value: T, index: number, arr: T[]) => R
171
+ ): Exclude<R, null | undefined>[]
172
+ ```
173
+
174
+ Maps items and removes `null`/`undefined` results.
175
+
176
+ Example:
177
+
178
+ ```ts
179
+ compactMap([1, 2, 3], n => (n > 1 ? n * 2 : null));
180
+ // [4, 6]
181
+ ```
182
+
183
+ ### random
184
+
185
+ ```ts
186
+ random<T>(arr: T[]): T | undefined
187
+ ```
188
+
189
+ Returns a random element, or `undefined` if the array is empty.
190
+
191
+ ### keyValueMap
192
+
193
+ ```ts
194
+ keyValueMap<T extends Record<string, any>>(arr: T[], key: string, value: string): Record<string, any>
195
+ ```
196
+
197
+ Builds an object mapping `item[key]` to `item[value]`. Throws if `key` or `value` is missing, any element is falsy, non-object, or missing `key`.
198
+
199
+ Example:
200
+
201
+ ```ts
202
+ keyValueMap(
203
+ [
204
+ { id: "a", name: "Alpha" },
205
+ { id: "b", name: "Beta" }
206
+ ],
207
+ "id",
208
+ "name"
209
+ );
210
+ // { a: "Alpha", b: "Beta" }
211
+ ```
212
+
213
+ ### groupBy
214
+
215
+ ```ts
216
+ groupBy<T, K extends PropertyKey>(
217
+ arr: T[],
218
+ key: ((item: T, index: number, arr: T[]) => K) | keyof T
219
+ ): Record<K, T[]>
220
+ ```
221
+
222
+ Groups items by a property name or mapper function.
223
+
224
+ Example:
225
+
226
+ ```ts
227
+ groupBy(
228
+ [
229
+ { type: "a", value: 1 },
230
+ { type: "b", value: 2 },
231
+ { type: "a", value: 3 }
232
+ ],
233
+ "type"
234
+ );
235
+ // { a: [{...}, {...}], b: [{...}] }
236
+ ```
237
+
238
+ ### pluck
239
+
240
+ ```ts
241
+ pluck<T, K extends keyof T>(arr: T[], key: K): T[K][]
242
+ ```
243
+
244
+ Plucks a property value from each item.
245
+
246
+ Example:
247
+
248
+ ```ts
249
+ pluck([{ id: 1 }, { id: 2 }], "id");
250
+ // [1, 2]
251
+ ```
252
+
253
+ ### countBy
254
+
255
+ ```ts
256
+ countBy<T, K extends PropertyKey>(
257
+ arr: T[],
258
+ key: ((item: T, index: number, arr: T[]) => K) | keyof T
259
+ ): Record<K, number>
260
+ ```
261
+
262
+ Counts items by a property name or mapper function.
263
+
264
+ Example:
265
+
266
+ ```ts
267
+ countBy(["a", "b", "a"], value => value);
268
+ // { a: 2, b: 1 }
269
+ ```
270
+
271
+ ### sum
272
+
273
+ ```ts
274
+ sum(arr: number[]): number
275
+ ```
276
+
277
+ Returns the sum of all numbers. Throws if any element is not a number. Returns `0` for an empty array.
278
+
279
+ ### avg
280
+
281
+ ```ts
282
+ avg(arr: number[]): number | undefined
283
+ ```
284
+
285
+ Returns the average. Throws if any element is not a number. Returns `undefined` for an empty array.
286
+
287
+ ### max
288
+
289
+ ```ts
290
+ max(arr: number[]): number | undefined
291
+ ```
292
+
293
+ Returns the maximum value. Throws if any element is not a number. Returns `undefined` for an empty array.
294
+
295
+ ### min
296
+
297
+ ```ts
298
+ min(arr: number[]): number | undefined
299
+ ```
300
+
301
+ Returns the minimum value. Throws if any element is not a number. Returns `undefined` for an empty array.
302
+
303
+ ### shuffle
304
+
305
+ ```ts
306
+ shuffle<T>(arr: T[]): T[]
307
+ ```
308
+
309
+ Shuffles the array in place and returns the same array reference.
310
+
311
+ ### flatten
312
+
313
+ ```ts
314
+ flatten<T>(arr: (T | T[])[]): T[]
315
+ ```
316
+
317
+ Flattens the array by one level.
318
+
319
+ Example:
320
+
321
+ ```ts
322
+ flatten([1, [2, 3], 4]);
323
+ // [1, 2, 3, 4]
324
+ ```
325
+
326
+ ### zip
327
+
328
+ ```ts
329
+ zip<T>(arr: T[], ...arrays: any[][]): any[][]
330
+ ```
331
+
332
+ Zips arrays into tuples, truncated to the shortest array length. If no extra arrays are provided, returns single-item tuples.
333
+
334
+ Example:
335
+
336
+ ```ts
337
+ zip([1, 2, 3], ["a", "b"]);
338
+ // [[1, "a"], [2, "b"]]
339
+ ```
340
+
341
+ ### partition
342
+
343
+ ```ts
344
+ partition<T>(
345
+ arr: T[],
346
+ predicate: (value: T, index: number, arr: T[]) => boolean
347
+ ): [T[], T[]]
348
+ ```
349
+
350
+ Splits items into two arrays: those that pass the predicate and those that fail.
351
+
352
+ Example:
353
+
354
+ ```ts
355
+ partition([1, 2, 3, 4], n => n % 2 === 0);
356
+ // [[2, 4], [1, 3]]
357
+ ```
358
+
359
+ ### difference
360
+
361
+ ```ts
362
+ difference<T>(arr: T[], ...arrays: T[][]): T[]
363
+ ```
364
+
365
+ Returns items that are not present in the other arrays.
366
+
367
+ Example:
368
+
369
+ ```ts
370
+ difference([1, 2, 3], [2, 4]);
371
+ // [1, 3]
372
+ ```
373
+
374
+ ### intersection
375
+
376
+ ```ts
377
+ intersection<T>(arr: T[], ...arrays: T[][]): T[]
378
+ ```
379
+
380
+ Returns items present in all arrays.
381
+
382
+ Example:
383
+
384
+ ```ts
385
+ intersection([1, 2, 3], [2, 3, 4]);
386
+ // [2, 3]
387
+ ```
388
+
389
+ ### union
390
+
391
+ ```ts
392
+ union<T>(arr: T[], ...arrays: T[][]): T[]
393
+ ```
394
+
395
+ Returns a unique merge of the array and the other arrays.
396
+
397
+ Example:
398
+
399
+ ```ts
400
+ union([1, 2], [2, 3], [3, 4]);
401
+ // [1, 2, 3, 4]
402
+ ```
403
+
404
+ ### uniqBy
405
+
406
+ ```ts
407
+ uniqBy<T, K>(
408
+ arr: T[],
409
+ key: ((item: T, index: number, arr: T[]) => K) | keyof T
410
+ ): T[]
411
+ ```
412
+
413
+ Returns the first item for each unique key.
414
+
415
+ Example:
416
+
417
+ ```ts
418
+ uniqBy([{ id: 1 }, { id: 1 }, { id: 2 }], "id");
419
+ // [{ id: 1 }, { id: 2 }]
420
+ ```
421
+
422
+ ### sortBy
423
+
424
+ ```ts
425
+ sortBy<T, K extends string | number | bigint>(
426
+ arr: T[],
427
+ key: ((item: T, index: number, arr: T[]) => K) | keyof T
428
+ ): T[]
429
+ ```
430
+
431
+ Returns a new array sorted by the key (stable). For dates, map to `getTime()` in the mapper.
432
+
433
+ Example:
434
+
435
+ ```ts
436
+ sortBy([{ n: 2 }, { n: 1 }], "n");
437
+ // [{ n: 1 }, { n: 2 }]
438
+ ```
439
+
440
+ ### sample
441
+
442
+ ```ts
443
+ sample<T>(arr: T[], count: number): T[]
444
+ ```
445
+
446
+ Returns a random sample without replacement.
447
+
448
+ Example:
449
+
450
+ ```ts
451
+ sample([1, 2, 3, 4], 2);
452
+ // length 2
453
+ ```
454
+
455
+ ### pad
456
+
457
+ ```ts
458
+ pad<T>(arr: T[], length: number, value: T): T[]
459
+ ```
460
+
461
+ Pads the array to the given length with the provided value.
462
+
463
+ Example:
464
+
465
+ ```ts
466
+ pad([1, 2], 5, 0);
467
+ // [1, 2, 0, 0, 0]
468
+ ```
469
+
470
+ ## Notes
471
+
472
+ - The `auto` entry defines methods only if they do not already exist.
473
+ - The `auto` entry is side-effectful by design and is listed under `sideEffects` for bundlers.
474
+ - Build outputs live under `dist/` for ESM, CJS, and UMD.