@r01al/array-polyfills 1.0.0 → 1.0.1
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/dist/auto.cjs +88 -1
- package/dist/auto.cjs.map +1 -1
- package/dist/auto.mjs +88 -1
- package/dist/auto.mjs.map +1 -1
- package/dist/functions/chunk.d.ts +5 -0
- package/dist/functions/chunk.js +5 -0
- package/dist/functions/first.d.ts +3 -0
- package/dist/functions/first.js +3 -0
- package/dist/functions/keyValueMap.d.ts +7 -0
- package/dist/functions/keyValueMap.js +29 -0
- package/dist/functions/last.d.ts +3 -0
- package/dist/functions/last.js +3 -0
- package/dist/functions/random.d.ts +3 -0
- package/dist/functions/random.js +3 -0
- package/dist/functions/unique.js +37 -1
- package/dist/index.cjs +83 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.mjs +83 -2
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills/array.d.ts +1 -0
- package/dist/polyfills/array.js +8 -0
- package/package.json +3 -3
package/dist/auto.cjs
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Splits the array into smaller arrays of the given size.
|
|
5
|
+
* @param size Number of items per chunk
|
|
6
|
+
* @returns Array of chunks
|
|
7
|
+
*/
|
|
3
8
|
function chunk (size) {
|
|
4
9
|
if (!Number.isInteger(size) || size <= 0)
|
|
5
10
|
throw new Error("size must be a positive integer");
|
|
@@ -9,15 +14,90 @@ function chunk (size) {
|
|
|
9
14
|
return out;
|
|
10
15
|
}
|
|
11
16
|
|
|
17
|
+
/**
|
|
18
|
+
* @returns the first element of the array, or undefined if the array is empty.
|
|
19
|
+
*/
|
|
12
20
|
function first () { return this.length ? this[0] : undefined; }
|
|
13
21
|
|
|
22
|
+
/**
|
|
23
|
+
* @returns the last element of the array, or undefined if the array is empty.
|
|
24
|
+
*/
|
|
14
25
|
function last () { return this.length ? this[this.length - 1] : undefined; }
|
|
15
26
|
|
|
27
|
+
/**
|
|
28
|
+
* @returns a random element from the array, or undefined if the array is empty.
|
|
29
|
+
*/
|
|
16
30
|
function random () {
|
|
17
31
|
return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
|
|
18
32
|
}
|
|
19
33
|
|
|
20
|
-
|
|
34
|
+
/**
|
|
35
|
+
* @returns a new array with only unique elements from the original array.
|
|
36
|
+
* Supports deep comparison for objects.
|
|
37
|
+
* Uses a map for improved performance with primitives.
|
|
38
|
+
*/
|
|
39
|
+
function deepEqual(a, b) {
|
|
40
|
+
if (a === b)
|
|
41
|
+
return true;
|
|
42
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
43
|
+
return false;
|
|
44
|
+
const keysA = Object.keys(a), keysB = Object.keys(b);
|
|
45
|
+
if (keysA.length !== keysB.length)
|
|
46
|
+
return false;
|
|
47
|
+
for (const key of keysA) {
|
|
48
|
+
if (!keysB.includes(key) || !deepEqual(a[key], b[key]))
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
function unique () {
|
|
54
|
+
const result = [];
|
|
55
|
+
const primitiveMap = new Map();
|
|
56
|
+
for (const item of this) {
|
|
57
|
+
if (item === null || typeof item !== "object") {
|
|
58
|
+
if (!primitiveMap.has(item)) {
|
|
59
|
+
primitiveMap.set(item, true);
|
|
60
|
+
result.push(item);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
if (!result.some(existing => deepEqual(existing, item))) {
|
|
65
|
+
result.push(item);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Creates an object mapping each value of the given key to the corresponding value from the array of objects.
|
|
74
|
+
* @param key The property name to use as keys in the result object.
|
|
75
|
+
* @param value The property name to use as values in the result object.
|
|
76
|
+
* @returns An object mapping key values to value values.
|
|
77
|
+
*/
|
|
78
|
+
function keyValueMap (key, value) {
|
|
79
|
+
if (!key) {
|
|
80
|
+
throw new Error('keyValueMap: key is required');
|
|
81
|
+
}
|
|
82
|
+
if (!value) {
|
|
83
|
+
throw new Error("keyValueMap: value is required");
|
|
84
|
+
}
|
|
85
|
+
if (this.some(el => !el)) {
|
|
86
|
+
throw new Error('keyValueMap: Array contains falsy values');
|
|
87
|
+
}
|
|
88
|
+
if (this.some(el => typeof el !== 'object')) {
|
|
89
|
+
throw new Error('keyValueMap: Array contains non-object values');
|
|
90
|
+
}
|
|
91
|
+
if (this.some(el => !el.hasOwnProperty(key))) {
|
|
92
|
+
throw new Error(`keyValueMap: key "${key}" does not exist on all objects`);
|
|
93
|
+
}
|
|
94
|
+
var map = {};
|
|
95
|
+
for (let index = 0; index < this.length; index++) {
|
|
96
|
+
var element = this[index];
|
|
97
|
+
map[element[key]] = element[value];
|
|
98
|
+
}
|
|
99
|
+
return map;
|
|
100
|
+
}
|
|
21
101
|
|
|
22
102
|
if (!Array.prototype.first) {
|
|
23
103
|
Object.defineProperty(Array.prototype, "first", {
|
|
@@ -54,4 +134,11 @@ if (!Array.prototype.random) {
|
|
|
54
134
|
configurable: false
|
|
55
135
|
});
|
|
56
136
|
}
|
|
137
|
+
if (!Array.prototype.keyValueMap) {
|
|
138
|
+
Object.defineProperty(Array.prototype, "keyValueMap", {
|
|
139
|
+
value: keyValueMap,
|
|
140
|
+
writable: false,
|
|
141
|
+
configurable: false
|
|
142
|
+
});
|
|
143
|
+
}
|
|
57
144
|
//# sourceMappingURL=auto.cjs.map
|
package/dist/auto.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto.cjs","sources":["../src/functions/chunk.ts","../src/functions/first.ts","../src/functions/last.ts","../src/functions/random.ts","../src/functions/unique.ts","../src/polyfills/array.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"auto.cjs","sources":["../src/functions/chunk.ts","../src/functions/first.ts","../src/functions/last.ts","../src/functions/random.ts","../src/functions/unique.ts","../src/functions/keyValueMap.ts","../src/polyfills/array.ts"],"sourcesContent":["/**\n* Splits the array into smaller arrays of the given size.\n* @param size Number of items per chunk\n* @returns Array of chunks\n*/\n\nexport default function <T>(this: T[], size: number) {\n\tif (!Number.isInteger(size) || size <= 0) throw new Error(\"size must be a positive integer\");\n\tconst out: T[][] = [];\n\tfor (let i = 0; i < this.length; i += size) out.push(this.slice(i, i + size));\n\treturn out;\n}","/**\n* @returns the first element of the array, or undefined if the array is empty.\n*/\nexport default function <T>(this: T[]) { return this.length ? this[0] : undefined; }","/**\n* @returns the last element of the array, or undefined if the array is empty.\n*/\nexport default function <T>(this: T[]) { return this.length ? this[this.length - 1] : undefined; }","/**\n * @returns a random element from the array, or undefined if the array is empty.\n */\nexport default function <T>(this: T[]) {\n\treturn this.length ? this[Math.floor(Math.random() * this.length)] : undefined;\n}","/**\n * @returns a new array with only unique elements from the original array.\n * Supports deep comparison for objects.\n * Uses a map for improved performance with primitives.\n */\nfunction deepEqual(a: any, b: any): boolean {\n if (a === b) return true;\n if (typeof a !== \"object\" || typeof b !== \"object\" || a === null || b === null) return false;\n const keysA = Object.keys(a), keysB = Object.keys(b);\n if (keysA.length !== keysB.length) return false;\n for (const key of keysA) {\n if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;\n }\n return true;\n}\n\nexport default function <T>(this: T[]): T[] {\n const result: T[] = [];\n const primitiveMap = new Map<any, boolean>();\n\n for (const item of this) {\n if (item === null || typeof item !== \"object\") {\n if (!primitiveMap.has(item)) {\n primitiveMap.set(item, true);\n result.push(item);\n }\n } else {\n if (!result.some(existing => deepEqual(existing, item))) {\n result.push(item);\n }\n }\n }\n return result;\n}","/**\n * Creates an object mapping each value of the given key to the corresponding value from the array of objects.\n * @param key The property name to use as keys in the result object.\n * @param value The property name to use as values in the result object.\n * @returns An object mapping key values to value values.\n */\nexport default function (this: KeyValueMap[], key: string, value: string) {\n\tif (!key) {\n\t\tthrow new Error('keyValueMap: key is required');\n\t}\n\n\tif (!value) {\n\t\tthrow new Error(\"keyValueMap: value is required\");\n\t}\n\n\tif (this.some(el => !el)) {\n\t\tthrow new Error('keyValueMap: Array contains falsy values');\n\t}\n\n\tif (this.some(el => typeof el !== 'object')) {\n\t\tthrow new Error('keyValueMap: Array contains non-object values');\n\t}\n\n\tif (this.some(el => !el.hasOwnProperty(key))) {\n\t\tthrow new Error(`keyValueMap: key \"${key}\" does not exist on all objects`);\n\t}\n\n\tvar map : KeyValueMap = {};\n\n\tfor (let index = 0; index < this.length; index++) {\n\t\tvar element: KeyValueMap = this[index];\n\n\t\tmap[element[key]] = element[value];\n\t}\n\t\n\treturn map;\n}","import chunk from \"../functions/chunk\";\nimport first from \"../functions/first\";\nimport last from \"../functions/last\";\nimport random from \"../functions/random\";\nimport unique from \"../functions/unique\";\nimport keyValueMap from \"../functions/keyValueMap\";\n\ndeclare global {\n\tinterface Array<T> {\n\t\tfirst(): T | undefined;\n\t\tlast(): T | undefined;\n\t\tunique(): T[];\n\t\tchunk(size: number): T[][];\n\t\trandom(): T | undefined;\n\t\tkeyValueMap(key: string, value: string): Record<string, any>[];\n\t}\n}\n\nif (!Array.prototype.first) {\n\tObject.defineProperty(Array.prototype, \"first\", {\n\t\tvalue: first,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.last) {\n\tObject.defineProperty(Array.prototype, \"last\", {\n\t\tvalue: last,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.unique) {\n\tObject.defineProperty(Array.prototype, \"unique\", {\n\t\tvalue: unique,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.chunk) {\n\tObject.defineProperty(Array.prototype, \"chunk\", {\n\t\tvalue: chunk,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.random) {\n\tObject.defineProperty(Array.prototype, \"random\", {\n\t\tvalue: random,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.keyValueMap) {\n\tObject.defineProperty(Array.prototype, \"keyValueMap\", {\n\t\tvalue: keyValueMap,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\t\n}\n\nexport {};\n"],"names":[],"mappings":";;AAAA;;;;AAIE;AAEY,cAAA,EAAyB,IAAY,EAAA;IAClD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC;IAC5F,MAAM,GAAG,GAAU,EAAE;AACrB,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,IAAI;AAAE,QAAA,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;AAC7E,IAAA,OAAO,GAAG;AACX;;ACXA;;AAEE;AACY,cAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACHnF;;AAEE;AACY,aAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACHjG;;AAEG;AACW,eAAA,IAAA;IACb,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,SAAS;AAC/E;;ACLA;;;;AAIG;AACH,SAAS,SAAS,CAAC,CAAM,EAAE,CAAM,EAAA;IAC7B,IAAI,CAAC,KAAK,CAAC;AAAE,QAAA,OAAO,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of the given size.
|
|
3
|
+
* @param size Number of items per chunk
|
|
4
|
+
* @returns Array of chunks
|
|
5
|
+
*/
|
|
1
6
|
function chunk (size) {
|
|
2
7
|
if (!Number.isInteger(size) || size <= 0)
|
|
3
8
|
throw new Error("size must be a positive integer");
|
|
@@ -7,15 +12,90 @@ function chunk (size) {
|
|
|
7
12
|
return out;
|
|
8
13
|
}
|
|
9
14
|
|
|
15
|
+
/**
|
|
16
|
+
* @returns the first element of the array, or undefined if the array is empty.
|
|
17
|
+
*/
|
|
10
18
|
function first () { return this.length ? this[0] : undefined; }
|
|
11
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @returns the last element of the array, or undefined if the array is empty.
|
|
22
|
+
*/
|
|
12
23
|
function last () { return this.length ? this[this.length - 1] : undefined; }
|
|
13
24
|
|
|
25
|
+
/**
|
|
26
|
+
* @returns a random element from the array, or undefined if the array is empty.
|
|
27
|
+
*/
|
|
14
28
|
function random () {
|
|
15
29
|
return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
|
|
16
30
|
}
|
|
17
31
|
|
|
18
|
-
|
|
32
|
+
/**
|
|
33
|
+
* @returns a new array with only unique elements from the original array.
|
|
34
|
+
* Supports deep comparison for objects.
|
|
35
|
+
* Uses a map for improved performance with primitives.
|
|
36
|
+
*/
|
|
37
|
+
function deepEqual(a, b) {
|
|
38
|
+
if (a === b)
|
|
39
|
+
return true;
|
|
40
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
41
|
+
return false;
|
|
42
|
+
const keysA = Object.keys(a), keysB = Object.keys(b);
|
|
43
|
+
if (keysA.length !== keysB.length)
|
|
44
|
+
return false;
|
|
45
|
+
for (const key of keysA) {
|
|
46
|
+
if (!keysB.includes(key) || !deepEqual(a[key], b[key]))
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
function unique () {
|
|
52
|
+
const result = [];
|
|
53
|
+
const primitiveMap = new Map();
|
|
54
|
+
for (const item of this) {
|
|
55
|
+
if (item === null || typeof item !== "object") {
|
|
56
|
+
if (!primitiveMap.has(item)) {
|
|
57
|
+
primitiveMap.set(item, true);
|
|
58
|
+
result.push(item);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
if (!result.some(existing => deepEqual(existing, item))) {
|
|
63
|
+
result.push(item);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Creates an object mapping each value of the given key to the corresponding value from the array of objects.
|
|
72
|
+
* @param key The property name to use as keys in the result object.
|
|
73
|
+
* @param value The property name to use as values in the result object.
|
|
74
|
+
* @returns An object mapping key values to value values.
|
|
75
|
+
*/
|
|
76
|
+
function keyValueMap (key, value) {
|
|
77
|
+
if (!key) {
|
|
78
|
+
throw new Error('keyValueMap: key is required');
|
|
79
|
+
}
|
|
80
|
+
if (!value) {
|
|
81
|
+
throw new Error("keyValueMap: value is required");
|
|
82
|
+
}
|
|
83
|
+
if (this.some(el => !el)) {
|
|
84
|
+
throw new Error('keyValueMap: Array contains falsy values');
|
|
85
|
+
}
|
|
86
|
+
if (this.some(el => typeof el !== 'object')) {
|
|
87
|
+
throw new Error('keyValueMap: Array contains non-object values');
|
|
88
|
+
}
|
|
89
|
+
if (this.some(el => !el.hasOwnProperty(key))) {
|
|
90
|
+
throw new Error(`keyValueMap: key "${key}" does not exist on all objects`);
|
|
91
|
+
}
|
|
92
|
+
var map = {};
|
|
93
|
+
for (let index = 0; index < this.length; index++) {
|
|
94
|
+
var element = this[index];
|
|
95
|
+
map[element[key]] = element[value];
|
|
96
|
+
}
|
|
97
|
+
return map;
|
|
98
|
+
}
|
|
19
99
|
|
|
20
100
|
if (!Array.prototype.first) {
|
|
21
101
|
Object.defineProperty(Array.prototype, "first", {
|
|
@@ -52,4 +132,11 @@ if (!Array.prototype.random) {
|
|
|
52
132
|
configurable: false
|
|
53
133
|
});
|
|
54
134
|
}
|
|
135
|
+
if (!Array.prototype.keyValueMap) {
|
|
136
|
+
Object.defineProperty(Array.prototype, "keyValueMap", {
|
|
137
|
+
value: keyValueMap,
|
|
138
|
+
writable: false,
|
|
139
|
+
configurable: false
|
|
140
|
+
});
|
|
141
|
+
}
|
|
55
142
|
//# sourceMappingURL=auto.mjs.map
|
package/dist/auto.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto.mjs","sources":["../src/functions/chunk.ts","../src/functions/first.ts","../src/functions/last.ts","../src/functions/random.ts","../src/functions/unique.ts","../src/polyfills/array.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"auto.mjs","sources":["../src/functions/chunk.ts","../src/functions/first.ts","../src/functions/last.ts","../src/functions/random.ts","../src/functions/unique.ts","../src/functions/keyValueMap.ts","../src/polyfills/array.ts"],"sourcesContent":["/**\n* Splits the array into smaller arrays of the given size.\n* @param size Number of items per chunk\n* @returns Array of chunks\n*/\n\nexport default function <T>(this: T[], size: number) {\n\tif (!Number.isInteger(size) || size <= 0) throw new Error(\"size must be a positive integer\");\n\tconst out: T[][] = [];\n\tfor (let i = 0; i < this.length; i += size) out.push(this.slice(i, i + size));\n\treturn out;\n}","/**\n* @returns the first element of the array, or undefined if the array is empty.\n*/\nexport default function <T>(this: T[]) { return this.length ? this[0] : undefined; }","/**\n* @returns the last element of the array, or undefined if the array is empty.\n*/\nexport default function <T>(this: T[]) { return this.length ? this[this.length - 1] : undefined; }","/**\n * @returns a random element from the array, or undefined if the array is empty.\n */\nexport default function <T>(this: T[]) {\n\treturn this.length ? this[Math.floor(Math.random() * this.length)] : undefined;\n}","/**\n * @returns a new array with only unique elements from the original array.\n * Supports deep comparison for objects.\n * Uses a map for improved performance with primitives.\n */\nfunction deepEqual(a: any, b: any): boolean {\n if (a === b) return true;\n if (typeof a !== \"object\" || typeof b !== \"object\" || a === null || b === null) return false;\n const keysA = Object.keys(a), keysB = Object.keys(b);\n if (keysA.length !== keysB.length) return false;\n for (const key of keysA) {\n if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;\n }\n return true;\n}\n\nexport default function <T>(this: T[]): T[] {\n const result: T[] = [];\n const primitiveMap = new Map<any, boolean>();\n\n for (const item of this) {\n if (item === null || typeof item !== \"object\") {\n if (!primitiveMap.has(item)) {\n primitiveMap.set(item, true);\n result.push(item);\n }\n } else {\n if (!result.some(existing => deepEqual(existing, item))) {\n result.push(item);\n }\n }\n }\n return result;\n}","/**\n * Creates an object mapping each value of the given key to the corresponding value from the array of objects.\n * @param key The property name to use as keys in the result object.\n * @param value The property name to use as values in the result object.\n * @returns An object mapping key values to value values.\n */\nexport default function (this: KeyValueMap[], key: string, value: string) {\n\tif (!key) {\n\t\tthrow new Error('keyValueMap: key is required');\n\t}\n\n\tif (!value) {\n\t\tthrow new Error(\"keyValueMap: value is required\");\n\t}\n\n\tif (this.some(el => !el)) {\n\t\tthrow new Error('keyValueMap: Array contains falsy values');\n\t}\n\n\tif (this.some(el => typeof el !== 'object')) {\n\t\tthrow new Error('keyValueMap: Array contains non-object values');\n\t}\n\n\tif (this.some(el => !el.hasOwnProperty(key))) {\n\t\tthrow new Error(`keyValueMap: key \"${key}\" does not exist on all objects`);\n\t}\n\n\tvar map : KeyValueMap = {};\n\n\tfor (let index = 0; index < this.length; index++) {\n\t\tvar element: KeyValueMap = this[index];\n\n\t\tmap[element[key]] = element[value];\n\t}\n\t\n\treturn map;\n}","import chunk from \"../functions/chunk\";\nimport first from \"../functions/first\";\nimport last from \"../functions/last\";\nimport random from \"../functions/random\";\nimport unique from \"../functions/unique\";\nimport keyValueMap from \"../functions/keyValueMap\";\n\ndeclare global {\n\tinterface Array<T> {\n\t\tfirst(): T | undefined;\n\t\tlast(): T | undefined;\n\t\tunique(): T[];\n\t\tchunk(size: number): T[][];\n\t\trandom(): T | undefined;\n\t\tkeyValueMap(key: string, value: string): Record<string, any>[];\n\t}\n}\n\nif (!Array.prototype.first) {\n\tObject.defineProperty(Array.prototype, \"first\", {\n\t\tvalue: first,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.last) {\n\tObject.defineProperty(Array.prototype, \"last\", {\n\t\tvalue: last,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.unique) {\n\tObject.defineProperty(Array.prototype, \"unique\", {\n\t\tvalue: unique,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.chunk) {\n\tObject.defineProperty(Array.prototype, \"chunk\", {\n\t\tvalue: chunk,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.random) {\n\tObject.defineProperty(Array.prototype, \"random\", {\n\t\tvalue: random,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\n}\n\nif (!Array.prototype.keyValueMap) {\n\tObject.defineProperty(Array.prototype, \"keyValueMap\", {\n\t\tvalue: keyValueMap,\n\t\twritable: false,\n\t\tconfigurable: false\n\t});\t\n}\n\nexport {};\n"],"names":[],"mappings":"AAAA;;;;AAIE;AAEY,cAAA,EAAyB,IAAY,EAAA;IAClD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC;IAC5F,MAAM,GAAG,GAAU,EAAE;AACrB,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,IAAI;AAAE,QAAA,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;AAC7E,IAAA,OAAO,GAAG;AACX;;ACXA;;AAEE;AACY,cAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;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AQ,EAAE;AAChD,QAAA,KAAK,EAAE,MAAM;AACb,QAAA,QAAQ,EAAE,KAAK;AACf,QAAA,YAAY,EAAE;AACd,KAAA,CAAC;AACH;AAEA,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,EAAE;IACjC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,EAAE,aAAa,EAAE;AACrD,QAAA,KAAK,EAAE,WAAW;AAClB,QAAA,QAAQ,EAAE,KAAK;AACf,QAAA,YAAY,EAAE;AACd,KAAA,CAAC;AACH"}
|
package/dist/functions/chunk.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits the array into smaller arrays of the given size.
|
|
3
|
+
* @param size Number of items per chunk
|
|
4
|
+
* @returns Array of chunks
|
|
5
|
+
*/
|
|
1
6
|
export default function (size) {
|
|
2
7
|
if (!Number.isInteger(size) || size <= 0)
|
|
3
8
|
throw new Error("size must be a positive integer");
|
package/dist/functions/first.js
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an object mapping each value of the given key to the corresponding value from the array of objects.
|
|
3
|
+
* @param key The property name to use as keys in the result object.
|
|
4
|
+
* @param value The property name to use as values in the result object.
|
|
5
|
+
* @returns An object mapping key values to value values.
|
|
6
|
+
*/
|
|
7
|
+
export default function (this: KeyValueMap[], key: string, value: string): KeyValueMap;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an object mapping each value of the given key to the corresponding value from the array of objects.
|
|
3
|
+
* @param key The property name to use as keys in the result object.
|
|
4
|
+
* @param value The property name to use as values in the result object.
|
|
5
|
+
* @returns An object mapping key values to value values.
|
|
6
|
+
*/
|
|
7
|
+
export default function (key, value) {
|
|
8
|
+
if (!key) {
|
|
9
|
+
throw new Error('keyValueMap: key is required');
|
|
10
|
+
}
|
|
11
|
+
if (!value) {
|
|
12
|
+
throw new Error("keyValueMap: value is required");
|
|
13
|
+
}
|
|
14
|
+
if (this.some(el => !el)) {
|
|
15
|
+
throw new Error('keyValueMap: Array contains falsy values');
|
|
16
|
+
}
|
|
17
|
+
if (this.some(el => typeof el !== 'object')) {
|
|
18
|
+
throw new Error('keyValueMap: Array contains non-object values');
|
|
19
|
+
}
|
|
20
|
+
if (this.some(el => !el.hasOwnProperty(key))) {
|
|
21
|
+
throw new Error(`keyValueMap: key "${key}" does not exist on all objects`);
|
|
22
|
+
}
|
|
23
|
+
var map = {};
|
|
24
|
+
for (let index = 0; index < this.length; index++) {
|
|
25
|
+
var element = this[index];
|
|
26
|
+
map[element[key]] = element[value];
|
|
27
|
+
}
|
|
28
|
+
return map;
|
|
29
|
+
}
|
package/dist/functions/last.d.ts
CHANGED
package/dist/functions/last.js
CHANGED
package/dist/functions/random.js
CHANGED
package/dist/functions/unique.js
CHANGED
|
@@ -1 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @returns a new array with only unique elements from the original array.
|
|
3
|
+
* Supports deep comparison for objects.
|
|
4
|
+
* Uses a map for improved performance with primitives.
|
|
5
|
+
*/
|
|
6
|
+
function deepEqual(a, b) {
|
|
7
|
+
if (a === b)
|
|
8
|
+
return true;
|
|
9
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
10
|
+
return false;
|
|
11
|
+
const keysA = Object.keys(a), keysB = Object.keys(b);
|
|
12
|
+
if (keysA.length !== keysB.length)
|
|
13
|
+
return false;
|
|
14
|
+
for (const key of keysA) {
|
|
15
|
+
if (!keysB.includes(key) || !deepEqual(a[key], b[key]))
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
export default function () {
|
|
21
|
+
const result = [];
|
|
22
|
+
const primitiveMap = new Map();
|
|
23
|
+
for (const item of this) {
|
|
24
|
+
if (item === null || typeof item !== "object") {
|
|
25
|
+
if (!primitiveMap.has(item)) {
|
|
26
|
+
primitiveMap.set(item, true);
|
|
27
|
+
result.push(item);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
if (!result.some(existing => deepEqual(existing, item))) {
|
|
32
|
+
result.push(item);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,58 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @returns the first element of the array, or undefined if the array is empty.
|
|
5
|
+
*/
|
|
3
6
|
function _first () { return this.length ? this[0] : undefined; }
|
|
4
7
|
|
|
8
|
+
/**
|
|
9
|
+
* @returns the last element of the array, or undefined if the array is empty.
|
|
10
|
+
*/
|
|
5
11
|
function _last () { return this.length ? this[this.length - 1] : undefined; }
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
/**
|
|
14
|
+
* @returns a new array with only unique elements from the original array.
|
|
15
|
+
* Supports deep comparison for objects.
|
|
16
|
+
* Uses a map for improved performance with primitives.
|
|
17
|
+
*/
|
|
18
|
+
function deepEqual(a, b) {
|
|
19
|
+
if (a === b)
|
|
20
|
+
return true;
|
|
21
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
22
|
+
return false;
|
|
23
|
+
const keysA = Object.keys(a), keysB = Object.keys(b);
|
|
24
|
+
if (keysA.length !== keysB.length)
|
|
25
|
+
return false;
|
|
26
|
+
for (const key of keysA) {
|
|
27
|
+
if (!keysB.includes(key) || !deepEqual(a[key], b[key]))
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
function _unique () {
|
|
33
|
+
const result = [];
|
|
34
|
+
const primitiveMap = new Map();
|
|
35
|
+
for (const item of this) {
|
|
36
|
+
if (item === null || typeof item !== "object") {
|
|
37
|
+
if (!primitiveMap.has(item)) {
|
|
38
|
+
primitiveMap.set(item, true);
|
|
39
|
+
result.push(item);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
if (!result.some(existing => deepEqual(existing, item))) {
|
|
44
|
+
result.push(item);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
8
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Splits the array into smaller arrays of the given size.
|
|
53
|
+
* @param size Number of items per chunk
|
|
54
|
+
* @returns Array of chunks
|
|
55
|
+
*/
|
|
9
56
|
function _chunk (size) {
|
|
10
57
|
if (!Number.isInteger(size) || size <= 0)
|
|
11
58
|
throw new Error("size must be a positive integer");
|
|
@@ -15,18 +62,53 @@ function _chunk (size) {
|
|
|
15
62
|
return out;
|
|
16
63
|
}
|
|
17
64
|
|
|
65
|
+
/**
|
|
66
|
+
* @returns a random element from the array, or undefined if the array is empty.
|
|
67
|
+
*/
|
|
18
68
|
function _random () {
|
|
19
69
|
return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
|
|
20
70
|
}
|
|
21
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Creates an object mapping each value of the given key to the corresponding value from the array of objects.
|
|
74
|
+
* @param key The property name to use as keys in the result object.
|
|
75
|
+
* @param value The property name to use as values in the result object.
|
|
76
|
+
* @returns An object mapping key values to value values.
|
|
77
|
+
*/
|
|
78
|
+
function _keyValueMap (key, value) {
|
|
79
|
+
if (!key) {
|
|
80
|
+
throw new Error('keyValueMap: key is required');
|
|
81
|
+
}
|
|
82
|
+
if (!value) {
|
|
83
|
+
throw new Error("keyValueMap: value is required");
|
|
84
|
+
}
|
|
85
|
+
if (this.some(el => !el)) {
|
|
86
|
+
throw new Error('keyValueMap: Array contains falsy values');
|
|
87
|
+
}
|
|
88
|
+
if (this.some(el => typeof el !== 'object')) {
|
|
89
|
+
throw new Error('keyValueMap: Array contains non-object values');
|
|
90
|
+
}
|
|
91
|
+
if (this.some(el => !el.hasOwnProperty(key))) {
|
|
92
|
+
throw new Error(`keyValueMap: key "${key}" does not exist on all objects`);
|
|
93
|
+
}
|
|
94
|
+
var map = {};
|
|
95
|
+
for (let index = 0; index < this.length; index++) {
|
|
96
|
+
var element = this[index];
|
|
97
|
+
map[element[key]] = element[value];
|
|
98
|
+
}
|
|
99
|
+
return map;
|
|
100
|
+
}
|
|
101
|
+
|
|
22
102
|
const first = (arr) => _first.apply(arr);
|
|
23
103
|
const last = (arr) => _last.apply(arr);
|
|
24
104
|
const unique = (arr) => _unique.apply(arr);
|
|
25
105
|
const chunk = (arr, size) => _chunk.apply(arr, [size]);
|
|
26
106
|
const random = (arr) => _random.apply(arr);
|
|
107
|
+
const keyValueMap = (arr, key, value) => _keyValueMap.apply(arr, [key, value]);
|
|
27
108
|
|
|
28
109
|
exports.chunk = chunk;
|
|
29
110
|
exports.first = first;
|
|
111
|
+
exports.keyValueMap = keyValueMap;
|
|
30
112
|
exports.last = last;
|
|
31
113
|
exports.random = random;
|
|
32
114
|
exports.unique = unique;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/functions/first.ts","../src/functions/last.ts","../src/functions/unique.ts","../src/functions/chunk.ts","../src/functions/random.ts","../src/index.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/functions/first.ts","../src/functions/last.ts","../src/functions/unique.ts","../src/functions/chunk.ts","../src/functions/random.ts","../src/functions/keyValueMap.ts","../src/index.ts"],"sourcesContent":["/**\n* @returns the first element of the array, or undefined if the array is empty.\n*/\nexport default function <T>(this: T[]) { return this.length ? this[0] : undefined; }","/**\n* @returns the last element of the array, or undefined if the array is empty.\n*/\nexport default function <T>(this: T[]) { return this.length ? this[this.length - 1] : undefined; }","/**\n * @returns a new array with only unique elements from the original array.\n * Supports deep comparison for objects.\n * Uses a map for improved performance with primitives.\n */\nfunction deepEqual(a: any, b: any): boolean {\n if (a === b) return true;\n if (typeof a !== \"object\" || typeof b !== \"object\" || a === null || b === null) return false;\n const keysA = Object.keys(a), keysB = Object.keys(b);\n if (keysA.length !== keysB.length) return false;\n for (const key of keysA) {\n if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;\n }\n return true;\n}\n\nexport default function <T>(this: T[]): T[] {\n const result: T[] = [];\n const primitiveMap = new Map<any, boolean>();\n\n for (const item of this) {\n if (item === null || typeof item !== \"object\") {\n if (!primitiveMap.has(item)) {\n primitiveMap.set(item, true);\n result.push(item);\n }\n } else {\n if (!result.some(existing => deepEqual(existing, item))) {\n result.push(item);\n }\n }\n }\n return result;\n}","/**\n* Splits the array into smaller arrays of the given size.\n* @param size Number of items per chunk\n* @returns Array of chunks\n*/\n\nexport default function <T>(this: T[], size: number) {\n\tif (!Number.isInteger(size) || size <= 0) throw new Error(\"size must be a positive integer\");\n\tconst out: T[][] = [];\n\tfor (let i = 0; i < this.length; i += size) out.push(this.slice(i, i + size));\n\treturn out;\n}","/**\n * @returns a random element from the array, or undefined if the array is empty.\n */\nexport default function <T>(this: T[]) {\n\treturn this.length ? this[Math.floor(Math.random() * this.length)] : undefined;\n}","/**\n * Creates an object mapping each value of the given key to the corresponding value from the array of objects.\n * @param key The property name to use as keys in the result object.\n * @param value The property name to use as values in the result object.\n * @returns An object mapping key values to value values.\n */\nexport default function (this: KeyValueMap[], key: string, value: string) {\n\tif (!key) {\n\t\tthrow new Error('keyValueMap: key is required');\n\t}\n\n\tif (!value) {\n\t\tthrow new Error(\"keyValueMap: value is required\");\n\t}\n\n\tif (this.some(el => !el)) {\n\t\tthrow new Error('keyValueMap: Array contains falsy values');\n\t}\n\n\tif (this.some(el => typeof el !== 'object')) {\n\t\tthrow new Error('keyValueMap: Array contains non-object values');\n\t}\n\n\tif (this.some(el => !el.hasOwnProperty(key))) {\n\t\tthrow new Error(`keyValueMap: key \"${key}\" does not exist on all objects`);\n\t}\n\n\tvar map : KeyValueMap = {};\n\n\tfor (let index = 0; index < this.length; index++) {\n\t\tvar element: KeyValueMap = this[index];\n\n\t\tmap[element[key]] = element[value];\n\t}\n\t\n\treturn map;\n}","import _first from \"./functions/first\";\nimport _last from \"./functions/last\";\nimport _unique from \"./functions/unique\";\nimport _chunk from \"./functions/chunk\";\nimport _random from \"./functions/random\";\nimport _keyValueMap from \"./functions/keyValueMap\";\n\nexport const first = <T>(arr: T[]): ReturnType<typeof _first> => _first.apply(arr);\nexport const last = <T>(arr: T[]): ReturnType<typeof _last> => _last.apply(arr);\nexport const unique = <T>(arr: T[]): ReturnType<typeof _unique> => _unique.apply(arr);\nexport const chunk = <T>(arr: T[], size: number): ReturnType<typeof _chunk> => _chunk.apply(arr, [size]);\nexport const random = <T>(arr: T[]): ReturnType<typeof _random> => _random.apply(arr);\nexport const keyValueMap = <T>(arr: KeyValueMap[], key: string, value: string): KeyValueMap => _keyValueMap.apply(arr, [key, value]);\n"],"names":[],"mappings":";;AAAA;;AAEE;AACY,eAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACHnF;;AAEE;AACY,cAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACHjG;;;;AAIG;AACH,SAAS,SAAS,CAAC,CAAM,EAAE,CAAM,EAAA;IAC7B,IAAI,CAAC,KAAK,CAAC;AAAE,QAAA,OAAO,IAAI;AACxB,IAAA,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,IAAI;AAAE,QAAA,OAAO,KAAK;AAC5F,IAAA,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACpD,IAAA,IAAI,KAAK,CAAC,MAAM,KAAK,KA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CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC;IACnC;AAEA,IAAA,OAAO,GAAG;AACX;;AC7BO,MAAM,KAAK,GAAG,CAAI,GAAQ,KAAgC,MAAM,CAAC,KAAK,CAAC,GAAG;AAC1E,MAAM,IAAI,GAAG,CAAI,GAAQ,KAA+B,KAAK,CAAC,KAAK,CAAC,GAAG;AACvE,MAAM,MAAM,GAAG,CAAI,GAAQ,KAAiC,OAAO,CAAC,KAAK,CAAC,GAAG;MACvE,KAAK,GAAG,CAAI,GAAQ,EAAE,IAAY,KAAgC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC;AAChG,MAAM,MAAM,GAAG,CAAI,GAAQ,KAAiC,OAAO,CAAC,KAAK,CAAC,GAAG;AAC7E,MAAM,WAAW,GAAG,CAAI,GAAkB,EAAE,GAAW,EAAE,KAAa,KAAkB,YAAY,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,3 +8,4 @@ export declare const last: <T>(arr: T[]) => ReturnType<typeof _last>;
|
|
|
8
8
|
export declare const unique: <T>(arr: T[]) => ReturnType<typeof _unique>;
|
|
9
9
|
export declare const chunk: <T>(arr: T[], size: number) => ReturnType<typeof _chunk>;
|
|
10
10
|
export declare const random: <T>(arr: T[]) => ReturnType<typeof _random>;
|
|
11
|
+
export declare const keyValueMap: <T>(arr: KeyValueMap[], key: string, value: string) => KeyValueMap;
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,10 @@ import _last from "./functions/last";
|
|
|
3
3
|
import _unique from "./functions/unique";
|
|
4
4
|
import _chunk from "./functions/chunk";
|
|
5
5
|
import _random from "./functions/random";
|
|
6
|
+
import _keyValueMap from "./functions/keyValueMap";
|
|
6
7
|
export const first = (arr) => _first.apply(arr);
|
|
7
8
|
export const last = (arr) => _last.apply(arr);
|
|
8
9
|
export const unique = (arr) => _unique.apply(arr);
|
|
9
10
|
export const chunk = (arr, size) => _chunk.apply(arr, [size]);
|
|
10
11
|
export const random = (arr) => _random.apply(arr);
|
|
12
|
+
export const keyValueMap = (arr, key, value) => _keyValueMap.apply(arr, [key, value]);
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @returns the first element of the array, or undefined if the array is empty.
|
|
3
|
+
*/
|
|
1
4
|
function _first () { return this.length ? this[0] : undefined; }
|
|
2
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @returns the last element of the array, or undefined if the array is empty.
|
|
8
|
+
*/
|
|
3
9
|
function _last () { return this.length ? this[this.length - 1] : undefined; }
|
|
4
10
|
|
|
5
|
-
|
|
11
|
+
/**
|
|
12
|
+
* @returns a new array with only unique elements from the original array.
|
|
13
|
+
* Supports deep comparison for objects.
|
|
14
|
+
* Uses a map for improved performance with primitives.
|
|
15
|
+
*/
|
|
16
|
+
function deepEqual(a, b) {
|
|
17
|
+
if (a === b)
|
|
18
|
+
return true;
|
|
19
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
20
|
+
return false;
|
|
21
|
+
const keysA = Object.keys(a), keysB = Object.keys(b);
|
|
22
|
+
if (keysA.length !== keysB.length)
|
|
23
|
+
return false;
|
|
24
|
+
for (const key of keysA) {
|
|
25
|
+
if (!keysB.includes(key) || !deepEqual(a[key], b[key]))
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
function _unique () {
|
|
31
|
+
const result = [];
|
|
32
|
+
const primitiveMap = new Map();
|
|
33
|
+
for (const item of this) {
|
|
34
|
+
if (item === null || typeof item !== "object") {
|
|
35
|
+
if (!primitiveMap.has(item)) {
|
|
36
|
+
primitiveMap.set(item, true);
|
|
37
|
+
result.push(item);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
if (!result.some(existing => deepEqual(existing, item))) {
|
|
42
|
+
result.push(item);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
6
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Splits the array into smaller arrays of the given size.
|
|
51
|
+
* @param size Number of items per chunk
|
|
52
|
+
* @returns Array of chunks
|
|
53
|
+
*/
|
|
7
54
|
function _chunk (size) {
|
|
8
55
|
if (!Number.isInteger(size) || size <= 0)
|
|
9
56
|
throw new Error("size must be a positive integer");
|
|
@@ -13,15 +60,49 @@ function _chunk (size) {
|
|
|
13
60
|
return out;
|
|
14
61
|
}
|
|
15
62
|
|
|
63
|
+
/**
|
|
64
|
+
* @returns a random element from the array, or undefined if the array is empty.
|
|
65
|
+
*/
|
|
16
66
|
function _random () {
|
|
17
67
|
return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
|
|
18
68
|
}
|
|
19
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Creates an object mapping each value of the given key to the corresponding value from the array of objects.
|
|
72
|
+
* @param key The property name to use as keys in the result object.
|
|
73
|
+
* @param value The property name to use as values in the result object.
|
|
74
|
+
* @returns An object mapping key values to value values.
|
|
75
|
+
*/
|
|
76
|
+
function _keyValueMap (key, value) {
|
|
77
|
+
if (!key) {
|
|
78
|
+
throw new Error('keyValueMap: key is required');
|
|
79
|
+
}
|
|
80
|
+
if (!value) {
|
|
81
|
+
throw new Error("keyValueMap: value is required");
|
|
82
|
+
}
|
|
83
|
+
if (this.some(el => !el)) {
|
|
84
|
+
throw new Error('keyValueMap: Array contains falsy values');
|
|
85
|
+
}
|
|
86
|
+
if (this.some(el => typeof el !== 'object')) {
|
|
87
|
+
throw new Error('keyValueMap: Array contains non-object values');
|
|
88
|
+
}
|
|
89
|
+
if (this.some(el => !el.hasOwnProperty(key))) {
|
|
90
|
+
throw new Error(`keyValueMap: key "${key}" does not exist on all objects`);
|
|
91
|
+
}
|
|
92
|
+
var map = {};
|
|
93
|
+
for (let index = 0; index < this.length; index++) {
|
|
94
|
+
var element = this[index];
|
|
95
|
+
map[element[key]] = element[value];
|
|
96
|
+
}
|
|
97
|
+
return map;
|
|
98
|
+
}
|
|
99
|
+
|
|
20
100
|
const first = (arr) => _first.apply(arr);
|
|
21
101
|
const last = (arr) => _last.apply(arr);
|
|
22
102
|
const unique = (arr) => _unique.apply(arr);
|
|
23
103
|
const chunk = (arr, size) => _chunk.apply(arr, [size]);
|
|
24
104
|
const random = (arr) => _random.apply(arr);
|
|
105
|
+
const keyValueMap = (arr, key, value) => _keyValueMap.apply(arr, [key, value]);
|
|
25
106
|
|
|
26
|
-
export { chunk, first, last, random, unique };
|
|
107
|
+
export { chunk, first, keyValueMap, last, random, unique };
|
|
27
108
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/functions/first.ts","../src/functions/last.ts","../src/functions/unique.ts","../src/functions/chunk.ts","../src/functions/random.ts","../src/index.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/functions/first.ts","../src/functions/last.ts","../src/functions/unique.ts","../src/functions/chunk.ts","../src/functions/random.ts","../src/functions/keyValueMap.ts","../src/index.ts"],"sourcesContent":["/**\n* @returns the first element of the array, or undefined if the array is empty.\n*/\nexport default function <T>(this: T[]) { return this.length ? this[0] : undefined; }","/**\n* @returns the last element of the array, or undefined if the array is empty.\n*/\nexport default function <T>(this: T[]) { return this.length ? this[this.length - 1] : undefined; }","/**\n * @returns a new array with only unique elements from the original array.\n * Supports deep comparison for objects.\n * Uses a map for improved performance with primitives.\n */\nfunction deepEqual(a: any, b: any): boolean {\n if (a === b) return true;\n if (typeof a !== \"object\" || typeof b !== \"object\" || a === null || b === null) return false;\n const keysA = Object.keys(a), keysB = Object.keys(b);\n if (keysA.length !== keysB.length) return false;\n for (const key of keysA) {\n if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;\n }\n return true;\n}\n\nexport default function <T>(this: T[]): T[] {\n const result: T[] = [];\n const primitiveMap = new Map<any, boolean>();\n\n for (const item of this) {\n if (item === null || typeof item !== \"object\") {\n if (!primitiveMap.has(item)) {\n primitiveMap.set(item, true);\n result.push(item);\n }\n } else {\n if (!result.some(existing => deepEqual(existing, item))) {\n result.push(item);\n }\n }\n }\n return result;\n}","/**\n* Splits the array into smaller arrays of the given size.\n* @param size Number of items per chunk\n* @returns Array of chunks\n*/\n\nexport default function <T>(this: T[], size: number) {\n\tif (!Number.isInteger(size) || size <= 0) throw new Error(\"size must be a positive integer\");\n\tconst out: T[][] = [];\n\tfor (let i = 0; i < this.length; i += size) out.push(this.slice(i, i + size));\n\treturn out;\n}","/**\n * @returns a random element from the array, or undefined if the array is empty.\n */\nexport default function <T>(this: T[]) {\n\treturn this.length ? this[Math.floor(Math.random() * this.length)] : undefined;\n}","/**\n * Creates an object mapping each value of the given key to the corresponding value from the array of objects.\n * @param key The property name to use as keys in the result object.\n * @param value The property name to use as values in the result object.\n * @returns An object mapping key values to value values.\n */\nexport default function (this: KeyValueMap[], key: string, value: string) {\n\tif (!key) {\n\t\tthrow new Error('keyValueMap: key is required');\n\t}\n\n\tif (!value) {\n\t\tthrow new Error(\"keyValueMap: value is required\");\n\t}\n\n\tif (this.some(el => !el)) {\n\t\tthrow new Error('keyValueMap: Array contains falsy values');\n\t}\n\n\tif (this.some(el => typeof el !== 'object')) {\n\t\tthrow new Error('keyValueMap: Array contains non-object values');\n\t}\n\n\tif (this.some(el => !el.hasOwnProperty(key))) {\n\t\tthrow new Error(`keyValueMap: key \"${key}\" does not exist on all objects`);\n\t}\n\n\tvar map : KeyValueMap = {};\n\n\tfor (let index = 0; index < this.length; index++) {\n\t\tvar element: KeyValueMap = this[index];\n\n\t\tmap[element[key]] = element[value];\n\t}\n\t\n\treturn map;\n}","import _first from \"./functions/first\";\nimport _last from \"./functions/last\";\nimport _unique from \"./functions/unique\";\nimport _chunk from \"./functions/chunk\";\nimport _random from \"./functions/random\";\nimport _keyValueMap from \"./functions/keyValueMap\";\n\nexport const first = <T>(arr: T[]): ReturnType<typeof _first> => _first.apply(arr);\nexport const last = <T>(arr: T[]): ReturnType<typeof _last> => _last.apply(arr);\nexport const unique = <T>(arr: T[]): ReturnType<typeof _unique> => _unique.apply(arr);\nexport const chunk = <T>(arr: T[], size: number): ReturnType<typeof _chunk> => _chunk.apply(arr, [size]);\nexport const random = <T>(arr: T[]): ReturnType<typeof _random> => _random.apply(arr);\nexport const keyValueMap = <T>(arr: KeyValueMap[], key: string, value: string): KeyValueMap => _keyValueMap.apply(arr, [key, value]);\n"],"names":[],"mappings":"AAAA;;AAEE;AACY,eAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACHnF;;AAEE;AACY,cAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACHjG;;;;AAIG;AACH,SAAS,SAAS,CAAC,CAAM,EAAE,CAAM,EAAA;IAC7B,IAAI,CAAC,KAAK,CAAC;AAAE,QAAA,OAAO,IAAI;AACxB,IAAA,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,IAAI;AAAE,QAAA,OAAO,KAAK;AAC5F,IAAA,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACpD,IAAA,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM;AAAE,QAAA,OAAO,KAAK;AAC/C,IAAA,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;QACrB,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GA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,KAAK,CAAC,GAAG;AAC1E,MAAM,IAAI,GAAG,CAAI,GAAQ,KAA+B,KAAK,CAAC,KAAK,CAAC,GAAG;AACvE,MAAM,MAAM,GAAG,CAAI,GAAQ,KAAiC,OAAO,CAAC,KAAK,CAAC,GAAG;MACvE,KAAK,GAAG,CAAI,GAAQ,EAAE,IAAY,KAAgC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC;AAChG,MAAM,MAAM,GAAG,CAAI,GAAQ,KAAiC,OAAO,CAAC,KAAK,CAAC,GAAG;AAC7E,MAAM,WAAW,GAAG,CAAI,GAAkB,EAAE,GAAW,EAAE,KAAa,KAAkB,YAAY,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC;;;;"}
|
package/dist/polyfills/array.js
CHANGED
|
@@ -3,6 +3,7 @@ import first from "../functions/first";
|
|
|
3
3
|
import last from "../functions/last";
|
|
4
4
|
import random from "../functions/random";
|
|
5
5
|
import unique from "../functions/unique";
|
|
6
|
+
import keyValueMap from "../functions/keyValueMap";
|
|
6
7
|
if (!Array.prototype.first) {
|
|
7
8
|
Object.defineProperty(Array.prototype, "first", {
|
|
8
9
|
value: first,
|
|
@@ -38,3 +39,10 @@ if (!Array.prototype.random) {
|
|
|
38
39
|
configurable: false
|
|
39
40
|
});
|
|
40
41
|
}
|
|
42
|
+
if (!Array.prototype.keyValueMap) {
|
|
43
|
+
Object.defineProperty(Array.prototype, "keyValueMap", {
|
|
44
|
+
value: keyValueMap,
|
|
45
|
+
writable: false,
|
|
46
|
+
configurable: false
|
|
47
|
+
});
|
|
48
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@r01al/array-polyfills",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Cool JS Array Polyfills",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"polyfills",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"module": "./dist/index.mjs",
|
|
37
37
|
"main": "./dist/index.cjs",
|
|
38
|
-
|
|
38
|
+
"exports": {
|
|
39
39
|
".": {
|
|
40
40
|
"types": "./dist/index.d.ts",
|
|
41
41
|
"import": "./dist/index.mjs",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"import": "./dist/auto.mjs",
|
|
52
52
|
"require": "./dist/auto.cjs"
|
|
53
53
|
}
|
|
54
|
-
|
|
54
|
+
},
|
|
55
55
|
"sideEffects": [
|
|
56
56
|
"./dist/auto.mjs",
|
|
57
57
|
"./dist/auto.cjs",
|