@r01al/array-polyfills 1.0.3 → 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.
- package/README.md +474 -2
- package/dist/auto.cjs +351 -41
- package/dist/auto.cjs.map +1 -1
- package/dist/auto.mjs +351 -41
- package/dist/auto.mjs.map +1 -1
- package/dist/auto.umd.js +530 -0
- package/dist/auto.umd.js.map +1 -0
- package/dist/functions/avg.d.ts +6 -0
- package/dist/functions/avg.js +13 -0
- package/dist/functions/compact.d.ts +7 -0
- package/dist/functions/compact.js +7 -0
- package/dist/functions/compactMap.d.ts +6 -0
- package/dist/functions/compactMap.js +15 -0
- package/dist/functions/countBy.d.ts +6 -0
- package/dist/functions/countBy.js +17 -0
- package/dist/functions/difference.d.ts +6 -0
- package/dist/functions/difference.js +15 -0
- package/dist/functions/flatten.d.ts +5 -0
- package/dist/functions/flatten.js +14 -0
- package/dist/functions/groupBy.d.ts +6 -0
- package/dist/functions/groupBy.js +19 -0
- package/dist/functions/intersection.d.ts +6 -0
- package/dist/functions/intersection.js +11 -0
- package/dist/functions/max.d.ts +6 -0
- package/dist/functions/max.js +13 -0
- package/dist/functions/min.d.ts +6 -0
- package/dist/functions/min.js +13 -0
- package/dist/functions/pad.d.ts +7 -0
- package/dist/functions/pad.js +15 -0
- package/dist/functions/partition.d.ts +6 -0
- package/dist/functions/partition.js +17 -0
- package/dist/functions/pluck.d.ts +6 -0
- package/dist/functions/pluck.js +8 -0
- package/dist/functions/sample.d.ts +6 -0
- package/dist/functions/sample.js +16 -0
- package/dist/functions/shuffle.d.ts +1 -0
- package/dist/functions/shuffle.js +7 -0
- package/dist/functions/sortBy.d.ts +6 -0
- package/dist/functions/sortBy.js +20 -0
- package/dist/functions/sum.d.ts +10 -0
- package/dist/functions/sum.js +19 -0
- package/dist/functions/union.d.ts +6 -0
- package/dist/functions/union.js +22 -0
- package/dist/functions/uniqBy.d.ts +6 -0
- package/dist/functions/uniqBy.js +21 -0
- package/dist/functions/zip.d.ts +6 -0
- package/dist/functions/zip.js +15 -0
- package/dist/index.cjs +364 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +40 -0
- package/dist/index.js +47 -6
- package/dist/index.mjs +345 -7
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +580 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/polyfills/array.d.ts +20 -0
- package/dist/polyfills/array.js +54 -41
- package/package.json +2 -1
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps items and removes null/undefined results.
|
|
3
|
+
* @param mapper Function to map items.
|
|
4
|
+
* @returns A new array of mapped values without null/undefined.
|
|
5
|
+
*/
|
|
6
|
+
export default function <T, R>(this: T[], mapper: (value: T, index: number, arr: T[]) => R): Exclude<R, null | undefined>[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps items and removes null/undefined results.
|
|
3
|
+
* @param mapper Function to map items.
|
|
4
|
+
* @returns A new array of mapped values without null/undefined.
|
|
5
|
+
*/
|
|
6
|
+
export default function (mapper) {
|
|
7
|
+
const out = [];
|
|
8
|
+
for (let index = 0; index < this.length; index++) {
|
|
9
|
+
const value = mapper(this[index], index, this);
|
|
10
|
+
if (value !== null && value !== undefined) {
|
|
11
|
+
out.push(value);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Counts items by a key or mapper function.
|
|
3
|
+
* @param key The property name or mapper function to count by.
|
|
4
|
+
* @returns An object where keys are group identifiers and values are counts.
|
|
5
|
+
*/
|
|
6
|
+
export default function <T, K extends PropertyKey>(this: T[], key: ((item: T, index: number, arr: T[]) => K) | keyof T): Record<K, number>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Counts items by a key or mapper function.
|
|
3
|
+
* @param key The property name or mapper function to count by.
|
|
4
|
+
* @returns An object where keys are group identifiers and values are counts.
|
|
5
|
+
*/
|
|
6
|
+
export default function (key) {
|
|
7
|
+
const result = {};
|
|
8
|
+
const getKey = typeof key === "function"
|
|
9
|
+
? key
|
|
10
|
+
: (item) => item[key];
|
|
11
|
+
for (let index = 0; index < this.length; index++) {
|
|
12
|
+
const item = this[index];
|
|
13
|
+
const groupKey = getKey(item, index, this);
|
|
14
|
+
result[groupKey] = (result[groupKey] ?? 0) + 1;
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns items that are not present in the other arrays.
|
|
3
|
+
* @param arrays Arrays to exclude.
|
|
4
|
+
* @returns A new array of values not found in other arrays.
|
|
5
|
+
*/
|
|
6
|
+
export default function (...arrays) {
|
|
7
|
+
if (arrays.length === 0)
|
|
8
|
+
return this.slice();
|
|
9
|
+
const other = new Set();
|
|
10
|
+
for (const arr of arrays) {
|
|
11
|
+
for (const item of arr)
|
|
12
|
+
other.add(item);
|
|
13
|
+
}
|
|
14
|
+
return this.filter(item => !other.has(item));
|
|
15
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flattens the array by one level.
|
|
3
|
+
* @returns A new flattened array.
|
|
4
|
+
*/
|
|
5
|
+
export default function () {
|
|
6
|
+
const out = [];
|
|
7
|
+
for (const item of this) {
|
|
8
|
+
if (Array.isArray(item))
|
|
9
|
+
out.push(...item);
|
|
10
|
+
else
|
|
11
|
+
out.push(item);
|
|
12
|
+
}
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups array items by a key or mapper function.
|
|
3
|
+
* @param key The property name or mapper function to group by.
|
|
4
|
+
* @returns An object where keys are group identifiers and values are arrays of items.
|
|
5
|
+
*/
|
|
6
|
+
export default function <T, K extends PropertyKey>(this: T[], key: ((item: T, index: number, arr: T[]) => K) | keyof T): Record<K, T[]>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups array items by a key or mapper function.
|
|
3
|
+
* @param key The property name or mapper function to group by.
|
|
4
|
+
* @returns An object where keys are group identifiers and values are arrays of items.
|
|
5
|
+
*/
|
|
6
|
+
export default function (key) {
|
|
7
|
+
const result = {};
|
|
8
|
+
const getKey = typeof key === "function"
|
|
9
|
+
? key
|
|
10
|
+
: (item) => item[key];
|
|
11
|
+
for (let index = 0; index < this.length; index++) {
|
|
12
|
+
const item = this[index];
|
|
13
|
+
const groupKey = getKey(item, index, this);
|
|
14
|
+
if (!result[groupKey])
|
|
15
|
+
result[groupKey] = [];
|
|
16
|
+
result[groupKey].push(item);
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns items present in all arrays.
|
|
3
|
+
* @param arrays Arrays to intersect with.
|
|
4
|
+
* @returns A new array of items found in every array.
|
|
5
|
+
*/
|
|
6
|
+
export default function (...arrays) {
|
|
7
|
+
if (arrays.length === 0)
|
|
8
|
+
return this.slice();
|
|
9
|
+
const sets = arrays.map(arr => new Set(arr));
|
|
10
|
+
return this.filter(item => sets.every(set => set.has(item)));
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the maximum number in the array.
|
|
3
|
+
* @returns The maximum value.
|
|
4
|
+
* @throws If any element is not a number.
|
|
5
|
+
*/
|
|
6
|
+
export default function () {
|
|
7
|
+
if (this.length === 0)
|
|
8
|
+
return undefined;
|
|
9
|
+
if (this.some(el => typeof el !== 'number')) {
|
|
10
|
+
throw new Error('All elements must be numbers');
|
|
11
|
+
}
|
|
12
|
+
return Math.max(...this);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the minimum number in the array.
|
|
3
|
+
* @returns The minimum value.
|
|
4
|
+
* @throws If any element is not a number.
|
|
5
|
+
*/
|
|
6
|
+
export default function () {
|
|
7
|
+
if (this.length === 0)
|
|
8
|
+
return undefined;
|
|
9
|
+
if (this.some(el => typeof el !== 'number')) {
|
|
10
|
+
throw new Error('All elements must be numbers');
|
|
11
|
+
}
|
|
12
|
+
return Math.min(...this);
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pads the array to the given length with the provided value.
|
|
3
|
+
* @param length Target length.
|
|
4
|
+
* @param value Value to pad with.
|
|
5
|
+
* @returns A new padded array.
|
|
6
|
+
*/
|
|
7
|
+
export default function (length, value) {
|
|
8
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
9
|
+
throw new Error("length must be a non-negative integer");
|
|
10
|
+
}
|
|
11
|
+
const out = this.slice();
|
|
12
|
+
while (out.length < length)
|
|
13
|
+
out.push(value);
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits the array into two arrays based on a predicate.
|
|
3
|
+
* @param predicate Function to test each element.
|
|
4
|
+
* @returns A tuple: [items that pass, items that fail].
|
|
5
|
+
*/
|
|
6
|
+
export default function <T>(this: T[], predicate: (value: T, index: number, arr: T[]) => boolean): [T[], T[]];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits the array into two arrays based on a predicate.
|
|
3
|
+
* @param predicate Function to test each element.
|
|
4
|
+
* @returns A tuple: [items that pass, items that fail].
|
|
5
|
+
*/
|
|
6
|
+
export default function (predicate) {
|
|
7
|
+
const pass = [];
|
|
8
|
+
const fail = [];
|
|
9
|
+
for (let index = 0; index < this.length; index++) {
|
|
10
|
+
const item = this[index];
|
|
11
|
+
if (predicate(item, index, this))
|
|
12
|
+
pass.push(item);
|
|
13
|
+
else
|
|
14
|
+
fail.push(item);
|
|
15
|
+
}
|
|
16
|
+
return [pass, fail];
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random sample of items without replacement.
|
|
3
|
+
* @param count Number of items to sample.
|
|
4
|
+
* @returns A new array containing sampled items.
|
|
5
|
+
*/
|
|
6
|
+
export default function (count) {
|
|
7
|
+
if (!Number.isInteger(count) || count <= 0) {
|
|
8
|
+
throw new Error("count must be a positive integer");
|
|
9
|
+
}
|
|
10
|
+
const out = this.slice();
|
|
11
|
+
for (let i = out.length - 1; i > 0; i--) {
|
|
12
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
13
|
+
[out[i], out[j]] = [out[j], out[i]];
|
|
14
|
+
}
|
|
15
|
+
return out.slice(0, Math.min(count, out.length));
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function shuffle<T>(this: T[]): T[];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sorts items by a key or mapper function (stable).
|
|
3
|
+
* @param key The property name or mapper function to sort by.
|
|
4
|
+
* @returns A new array sorted by the key.
|
|
5
|
+
*/
|
|
6
|
+
export default function <T, K extends string | number | bigint>(this: T[], key: ((item: T, index: number, arr: T[]) => K) | keyof T): T[];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sorts items by a key or mapper function (stable).
|
|
3
|
+
* @param key The property name or mapper function to sort by.
|
|
4
|
+
* @returns A new array sorted by the key.
|
|
5
|
+
*/
|
|
6
|
+
export default function (key) {
|
|
7
|
+
const getKey = typeof key === "function"
|
|
8
|
+
? key
|
|
9
|
+
: (item) => item[key];
|
|
10
|
+
return this
|
|
11
|
+
.map((item, index) => ({ item, index, key: getKey(item, index, this) }))
|
|
12
|
+
.sort((a, b) => {
|
|
13
|
+
if (a.key < b.key)
|
|
14
|
+
return -1;
|
|
15
|
+
if (a.key > b.key)
|
|
16
|
+
return 1;
|
|
17
|
+
return a.index - b.index;
|
|
18
|
+
})
|
|
19
|
+
.map(entry => entry.item);
|
|
20
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the sum of all numbers in an array.
|
|
3
|
+
* @returns The sum of all numbers in the array.
|
|
4
|
+
* @throws Will throw an error if any element in the array is not a number.
|
|
5
|
+
* @example
|
|
6
|
+
* [1, 2, 3].sum() // returns 6
|
|
7
|
+
* [10, -2, 5].sum() // returns 13
|
|
8
|
+
* [1, '2', 3].sum() // throws Error
|
|
9
|
+
*/
|
|
10
|
+
export default function <T>(this: T[]): number;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the sum of all numbers in an array.
|
|
3
|
+
* @returns The sum of all numbers in the array.
|
|
4
|
+
* @throws Will throw an error if any element in the array is not a number.
|
|
5
|
+
* @example
|
|
6
|
+
* [1, 2, 3].sum() // returns 6
|
|
7
|
+
* [10, -2, 5].sum() // returns 13
|
|
8
|
+
* [1, '2', 3].sum() // throws Error
|
|
9
|
+
*/
|
|
10
|
+
export default function () {
|
|
11
|
+
if (this.some(el => typeof el !== 'number')) {
|
|
12
|
+
throw new Error('All elements must be numbers');
|
|
13
|
+
}
|
|
14
|
+
let sum = 0;
|
|
15
|
+
for (const num of this) {
|
|
16
|
+
sum += num;
|
|
17
|
+
}
|
|
18
|
+
return sum;
|
|
19
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a unique merge of the array and the other arrays.
|
|
3
|
+
* @param arrays Arrays to merge.
|
|
4
|
+
* @returns A new array with unique values.
|
|
5
|
+
*/
|
|
6
|
+
export default function (...arrays) {
|
|
7
|
+
const out = [];
|
|
8
|
+
const seen = new Set();
|
|
9
|
+
const push = (item) => {
|
|
10
|
+
if (seen.has(item))
|
|
11
|
+
return;
|
|
12
|
+
seen.add(item);
|
|
13
|
+
out.push(item);
|
|
14
|
+
};
|
|
15
|
+
for (const item of this)
|
|
16
|
+
push(item);
|
|
17
|
+
for (const arr of arrays) {
|
|
18
|
+
for (const item of arr)
|
|
19
|
+
push(item);
|
|
20
|
+
}
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns unique items by a key or mapper function.
|
|
3
|
+
* @param key The property name or mapper function to determine uniqueness.
|
|
4
|
+
* @returns A new array with unique items.
|
|
5
|
+
*/
|
|
6
|
+
export default function <T, K>(this: T[], key: ((item: T, index: number, arr: T[]) => K) | keyof T): T[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns unique items by a key or mapper function.
|
|
3
|
+
* @param key The property name or mapper function to determine uniqueness.
|
|
4
|
+
* @returns A new array with unique items.
|
|
5
|
+
*/
|
|
6
|
+
export default function (key) {
|
|
7
|
+
const result = [];
|
|
8
|
+
const seen = new Set();
|
|
9
|
+
const getKey = typeof key === "function"
|
|
10
|
+
? key
|
|
11
|
+
: (item) => item[key];
|
|
12
|
+
for (let index = 0; index < this.length; index++) {
|
|
13
|
+
const item = this[index];
|
|
14
|
+
const keyValue = getKey(item, index, this);
|
|
15
|
+
if (!seen.has(keyValue)) {
|
|
16
|
+
seen.add(keyValue);
|
|
17
|
+
result.push(item);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zips the array with other arrays.
|
|
3
|
+
* @param arrays Arrays to zip with.
|
|
4
|
+
* @returns An array of tuples, truncated to the shortest length.
|
|
5
|
+
*/
|
|
6
|
+
export default function (...arrays) {
|
|
7
|
+
if (arrays.length === 0)
|
|
8
|
+
return this.map(item => [item]);
|
|
9
|
+
const minLength = Math.min(this.length, ...arrays.map(arr => arr.length));
|
|
10
|
+
const out = [];
|
|
11
|
+
for (let index = 0; index < minLength; index++) {
|
|
12
|
+
out.push([this[index], ...arrays.map(arr => arr[index])]);
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|