@r01al/array-polyfills 1.0.5 → 1.0.8

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