@r01al/array-polyfills 1.0.2 → 1.0.5

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 CHANGED
@@ -99,46 +99,94 @@ function keyValueMap (key, value) {
99
99
  return map;
100
100
  }
101
101
 
102
- if (!Array.prototype.first) {
103
- Object.defineProperty(Array.prototype, "first", {
104
- value: first,
105
- writable: false,
106
- configurable: false
107
- });
102
+ /**
103
+ * Calculates the sum of all numbers in an array.
104
+ * @returns The sum of all numbers in the array.
105
+ * @throws Will throw an error if any element in the array is not a number.
106
+ * @example
107
+ * [1, 2, 3].sum() // returns 6
108
+ * [10, -2, 5].sum() // returns 13
109
+ * [1, '2', 3].sum() // throws Error
110
+ */
111
+ function sum () {
112
+ if (this.some(el => typeof el !== 'number')) {
113
+ throw new Error('All elements must be numbers');
114
+ }
115
+ let sum = 0;
116
+ for (const num of this) {
117
+ sum += num;
118
+ }
119
+ return sum;
108
120
  }
109
- if (!Array.prototype.last) {
110
- Object.defineProperty(Array.prototype, "last", {
111
- value: last,
112
- writable: false,
113
- configurable: false
114
- });
121
+
122
+ /**
123
+ * Calculates the average of all numbers in an array.
124
+ * @returns The average value.
125
+ * @throws If any element is not a number.
126
+ */
127
+ function avg () {
128
+ if (this.length === 0)
129
+ return undefined;
130
+ if (this.some(el => typeof el !== 'number')) {
131
+ throw new Error('All elements must be numbers');
132
+ }
133
+ return this.reduce((a, b) => a + b, 0) / this.length;
115
134
  }
116
- if (!Array.prototype.unique) {
117
- Object.defineProperty(Array.prototype, "unique", {
118
- value: unique,
119
- writable: false,
120
- configurable: false
121
- });
135
+
136
+ /**
137
+ * Returns the maximum number in the array.
138
+ * @returns The maximum value.
139
+ * @throws If any element is not a number.
140
+ */
141
+ function max () {
142
+ if (this.length === 0)
143
+ return undefined;
144
+ if (this.some(el => typeof el !== 'number')) {
145
+ throw new Error('All elements must be numbers');
146
+ }
147
+ return Math.max(...this);
122
148
  }
123
- if (!Array.prototype.chunk) {
124
- Object.defineProperty(Array.prototype, "chunk", {
125
- value: chunk,
126
- writable: false,
127
- configurable: false
128
- });
149
+
150
+ /**
151
+ * Returns the minimum number in the array.
152
+ * @returns The minimum value.
153
+ * @throws If any element is not a number.
154
+ */
155
+ function min () {
156
+ if (this.length === 0)
157
+ return undefined;
158
+ if (this.some(el => typeof el !== 'number')) {
159
+ throw new Error('All elements must be numbers');
160
+ }
161
+ return Math.min(...this);
129
162
  }
130
- if (!Array.prototype.random) {
131
- Object.defineProperty(Array.prototype, "random", {
132
- value: random,
133
- writable: false,
134
- configurable: false
135
- });
163
+
164
+ function shuffle() {
165
+ for (let i = this.length - 1; i > 0; i--) {
166
+ const j = Math.floor(Math.random() * (i + 1));
167
+ [this[i], this[j]] = [this[j], this[i]];
168
+ }
169
+ return this;
136
170
  }
137
- if (!Array.prototype.keyValueMap) {
138
- Object.defineProperty(Array.prototype, "keyValueMap", {
139
- value: keyValueMap,
140
- writable: false,
141
- configurable: false
142
- });
171
+
172
+ function defineArrayMethod(name, fn) {
173
+ if (!Array.prototype[name]) {
174
+ Object.defineProperty(Array.prototype, name, {
175
+ value: fn,
176
+ writable: false,
177
+ configurable: false
178
+ });
179
+ }
143
180
  }
181
+ defineArrayMethod("first", first);
182
+ defineArrayMethod("last", last);
183
+ defineArrayMethod("unique", unique);
184
+ defineArrayMethod("chunk", chunk);
185
+ defineArrayMethod("random", random);
186
+ defineArrayMethod("keyValueMap", keyValueMap);
187
+ defineArrayMethod("sum", sum);
188
+ defineArrayMethod("avg", avg);
189
+ defineArrayMethod("max", max);
190
+ defineArrayMethod("min", min);
191
+ defineArrayMethod("shuffle", shuffle);
144
192
  //# 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/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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AAE;AAC9C,QAAA,KAAK,EAAE,IAAI;AACX,QAAA,QAAQ,EAAE,KAAK;AACf,QAAA,YAAY,EAAE;AACd,KAAA,CAAC;AACH;AAEA,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE;IAC5B,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,EAAE,QAAQ,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,KAAK,EAAE;IAC3B,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE;AAC/C,QAAA,KAAK,EAAE,KAAK;AACZ,QAAA,QAAQ,EAAE,KAAK;AACf,QAAA,YAAY,EAAE;AACd,KAAA,CAAC;AACH;AAEA,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE;IAC5B,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,EAAE,QAAQ,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;;"}
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/functions/sum.ts","../src/functions/avg.ts","../src/functions/max.ts","../src/functions/min.ts","../src/functions/shuffle.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}","/**\n * Calculates the sum of all numbers in an array.\n * @returns The sum of all numbers in the array.\n * @throws Will throw an error if any element in the array is not a number.\n * @example\n * [1, 2, 3].sum() // returns 6\n * [10, -2, 5].sum() // returns 13\n * [1, '2', 3].sum() // throws Error\n */\n\nexport default function <T>(this: T[]) { \n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n\n let sum = 0;\n\n for (const num of this as unknown as number[]) {\n sum += num;\n }\n\n return sum\n}","/**\n * Calculates the average of all numbers in an array.\n * @returns The average value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return this.reduce((a, b) => a + b, 0) / this.length;\n}","/**\n * Returns the maximum number in the array.\n * @returns The maximum value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return Math.max(...this);\n}","/**\n * Returns the minimum number in the array.\n * @returns The minimum value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return Math.min(...this);\n}","export default function shuffle<T>(this: T[]): T[] {\n for (let i = this.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [this[i], this[j]] = [this[j], this[i]];\n }\n \n return this;\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\";\nimport sum from \"../functions/sum\";\nimport avg from \"../functions/avg\";\nimport max from \"../functions/max\";\nimport min from \"../functions/min\";\nimport shuffle from \"../functions/shuffle\";\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\tsum(this: number[]): number;\n\t\tavg(this: number[]): number | undefined;\n\t\tmax(this: number[]): number | undefined;\n\t\tmin(this: number[]): number | undefined;\n\t\tshuffle(): T[];\t\n\t}\n}\n\nfunction defineArrayMethod (name: string, fn: Function) {\n\tif (!(Array.prototype as any)[name]) {\n\t\tObject.defineProperty(Array.prototype, name, {\n\t\t\tvalue: fn,\n\t\t\twritable: false,\n\t\t\tconfigurable: false\n\t\t});\n\t}\n}\n\ndefineArrayMethod(\"first\", first);\ndefineArrayMethod(\"last\", last);\ndefineArrayMethod(\"unique\", unique);\ndefineArrayMethod(\"chunk\", chunk);\ndefineArrayMethod(\"random\", random);\ndefineArrayMethod(\"keyValueMap\", keyValueMap);\ndefineArrayMethod(\"sum\", sum);\ndefineArrayMethod(\"avg\", avg);\ndefineArrayMethod(\"max\", max);\ndefineArrayMethod(\"min\", min);\ndefineArrayMethod(\"shuffle\", shuffle);\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,GA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AE;QACpC,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,EAAE;AAC5C,YAAA,KAAK,EAAE,EAAE;AACT,YAAA,QAAQ,EAAE,KAAK;AACf,YAAA,YAAY,EAAE;AACd,SAAA,CAAC;IACH;AACD;AAEA,iBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC;AACjC,iBAAiB,CAAC,MAAM,EAAE,IAAI,CAAC;AAC/B,iBAAiB,CAAC,QAAQ,EAAE,MAAM,CAAC;AACnC,iBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC;AACjC,iBAAiB,CAAC,QAAQ,EAAE,MAAM,CAAC;AACnC,iBAAiB,CAAC,aAAa,EAAE,WAAW,CAAC;AAC7C,iBAAiB,CAAC,KAAK,EAAE,GAAG,CAAC;AAC7B,iBAAiB,CAAC,KAAK,EAAE,GAAG,CAAC;AAC7B,iBAAiB,CAAC,KAAK,EAAE,GAAG,CAAC;AAC7B,iBAAiB,CAAC,KAAK,EAAE,GAAG,CAAC;AAC7B,iBAAiB,CAAC,SAAS,EAAE,OAAO,CAAC;;"}
package/dist/auto.mjs CHANGED
@@ -97,46 +97,94 @@ function keyValueMap (key, value) {
97
97
  return map;
98
98
  }
99
99
 
100
- if (!Array.prototype.first) {
101
- Object.defineProperty(Array.prototype, "first", {
102
- value: first,
103
- writable: false,
104
- configurable: false
105
- });
100
+ /**
101
+ * Calculates the sum of all numbers in an array.
102
+ * @returns The sum of all numbers in the array.
103
+ * @throws Will throw an error if any element in the array is not a number.
104
+ * @example
105
+ * [1, 2, 3].sum() // returns 6
106
+ * [10, -2, 5].sum() // returns 13
107
+ * [1, '2', 3].sum() // throws Error
108
+ */
109
+ function sum () {
110
+ if (this.some(el => typeof el !== 'number')) {
111
+ throw new Error('All elements must be numbers');
112
+ }
113
+ let sum = 0;
114
+ for (const num of this) {
115
+ sum += num;
116
+ }
117
+ return sum;
106
118
  }
107
- if (!Array.prototype.last) {
108
- Object.defineProperty(Array.prototype, "last", {
109
- value: last,
110
- writable: false,
111
- configurable: false
112
- });
119
+
120
+ /**
121
+ * Calculates the average of all numbers in an array.
122
+ * @returns The average value.
123
+ * @throws If any element is not a number.
124
+ */
125
+ function avg () {
126
+ if (this.length === 0)
127
+ return undefined;
128
+ if (this.some(el => typeof el !== 'number')) {
129
+ throw new Error('All elements must be numbers');
130
+ }
131
+ return this.reduce((a, b) => a + b, 0) / this.length;
113
132
  }
114
- if (!Array.prototype.unique) {
115
- Object.defineProperty(Array.prototype, "unique", {
116
- value: unique,
117
- writable: false,
118
- configurable: false
119
- });
133
+
134
+ /**
135
+ * Returns the maximum number in the array.
136
+ * @returns The maximum value.
137
+ * @throws If any element is not a number.
138
+ */
139
+ function max () {
140
+ if (this.length === 0)
141
+ return undefined;
142
+ if (this.some(el => typeof el !== 'number')) {
143
+ throw new Error('All elements must be numbers');
144
+ }
145
+ return Math.max(...this);
120
146
  }
121
- if (!Array.prototype.chunk) {
122
- Object.defineProperty(Array.prototype, "chunk", {
123
- value: chunk,
124
- writable: false,
125
- configurable: false
126
- });
147
+
148
+ /**
149
+ * Returns the minimum number in the array.
150
+ * @returns The minimum value.
151
+ * @throws If any element is not a number.
152
+ */
153
+ function min () {
154
+ if (this.length === 0)
155
+ return undefined;
156
+ if (this.some(el => typeof el !== 'number')) {
157
+ throw new Error('All elements must be numbers');
158
+ }
159
+ return Math.min(...this);
127
160
  }
128
- if (!Array.prototype.random) {
129
- Object.defineProperty(Array.prototype, "random", {
130
- value: random,
131
- writable: false,
132
- configurable: false
133
- });
161
+
162
+ function shuffle() {
163
+ for (let i = this.length - 1; i > 0; i--) {
164
+ const j = Math.floor(Math.random() * (i + 1));
165
+ [this[i], this[j]] = [this[j], this[i]];
166
+ }
167
+ return this;
134
168
  }
135
- if (!Array.prototype.keyValueMap) {
136
- Object.defineProperty(Array.prototype, "keyValueMap", {
137
- value: keyValueMap,
138
- writable: false,
139
- configurable: false
140
- });
169
+
170
+ function defineArrayMethod(name, fn) {
171
+ if (!Array.prototype[name]) {
172
+ Object.defineProperty(Array.prototype, name, {
173
+ value: fn,
174
+ writable: false,
175
+ configurable: false
176
+ });
177
+ }
141
178
  }
179
+ defineArrayMethod("first", first);
180
+ defineArrayMethod("last", last);
181
+ defineArrayMethod("unique", unique);
182
+ defineArrayMethod("chunk", chunk);
183
+ defineArrayMethod("random", random);
184
+ defineArrayMethod("keyValueMap", keyValueMap);
185
+ defineArrayMethod("sum", sum);
186
+ defineArrayMethod("avg", avg);
187
+ defineArrayMethod("max", max);
188
+ defineArrayMethod("min", min);
189
+ defineArrayMethod("shuffle", shuffle);
142
190
  //# 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/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,SA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,SAAS,EAAE,QAAQ,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"}
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/functions/sum.ts","../src/functions/avg.ts","../src/functions/max.ts","../src/functions/min.ts","../src/functions/shuffle.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}","/**\n * Calculates the sum of all numbers in an array.\n * @returns The sum of all numbers in the array.\n * @throws Will throw an error if any element in the array is not a number.\n * @example\n * [1, 2, 3].sum() // returns 6\n * [10, -2, 5].sum() // returns 13\n * [1, '2', 3].sum() // throws Error\n */\n\nexport default function <T>(this: T[]) { \n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n\n let sum = 0;\n\n for (const num of this as unknown as number[]) {\n sum += num;\n }\n\n return sum\n}","/**\n * Calculates the average of all numbers in an array.\n * @returns The average value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return this.reduce((a, b) => a + b, 0) / this.length;\n}","/**\n * Returns the maximum number in the array.\n * @returns The maximum value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return Math.max(...this);\n}","/**\n * Returns the minimum number in the array.\n * @returns The minimum value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return Math.min(...this);\n}","export default function shuffle<T>(this: T[]): T[] {\n for (let i = this.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [this[i], this[j]] = [this[j], this[i]];\n }\n \n return this;\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\";\nimport sum from \"../functions/sum\";\nimport avg from \"../functions/avg\";\nimport max from \"../functions/max\";\nimport min from \"../functions/min\";\nimport shuffle from \"../functions/shuffle\";\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\tsum(this: number[]): number;\n\t\tavg(this: number[]): number | undefined;\n\t\tmax(this: number[]): number | undefined;\n\t\tmin(this: number[]): number | undefined;\n\t\tshuffle(): T[];\t\n\t}\n}\n\nfunction defineArrayMethod (name: string, fn: Function) {\n\tif (!(Array.prototype as any)[name]) {\n\t\tObject.defineProperty(Array.prototype, name, {\n\t\t\tvalue: fn,\n\t\t\twritable: false,\n\t\t\tconfigurable: false\n\t\t});\n\t}\n}\n\ndefineArrayMethod(\"first\", first);\ndefineArrayMethod(\"last\", last);\ndefineArrayMethod(\"unique\", unique);\ndefineArrayMethod(\"chunk\", chunk);\ndefineArrayMethod(\"random\", random);\ndefineArrayMethod(\"keyValueMap\", keyValueMap);\ndefineArrayMethod(\"sum\", sum);\ndefineArrayMethod(\"avg\", avg);\ndefineArrayMethod(\"max\", max);\ndefineArrayMethod(\"min\", min);\ndefineArrayMethod(\"shuffle\", shuffle);\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,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,OAAO,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Calculates the average of all numbers in an array.
3
+ * @returns The average value.
4
+ * @throws If any element is not a number.
5
+ */
6
+ export default function (this: number[]): number | undefined;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Calculates the average of all numbers in an array.
3
+ * @returns The average 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 this.reduce((a, b) => a + b, 0) / this.length;
13
+ }
@@ -0,0 +1,6 @@
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 (this: number[]): number | undefined;
@@ -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,6 @@
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 (this: number[]): number | undefined;
@@ -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 @@
1
+ export default function shuffle<T>(this: T[]): T[];
@@ -0,0 +1,7 @@
1
+ export default function shuffle() {
2
+ for (let i = this.length - 1; i > 0; i--) {
3
+ const j = Math.floor(Math.random() * (i + 1));
4
+ [this[i], this[j]] = [this[j], this[i]];
5
+ }
6
+ return this;
7
+ }
@@ -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
+ }
package/dist/index.cjs CHANGED
@@ -99,17 +99,97 @@ function _keyValueMap (key, value) {
99
99
  return map;
100
100
  }
101
101
 
102
+ /**
103
+ * Calculates the sum of all numbers in an array.
104
+ * @returns The sum of all numbers in the array.
105
+ * @throws Will throw an error if any element in the array is not a number.
106
+ * @example
107
+ * [1, 2, 3].sum() // returns 6
108
+ * [10, -2, 5].sum() // returns 13
109
+ * [1, '2', 3].sum() // throws Error
110
+ */
111
+ function _sum () {
112
+ if (this.some(el => typeof el !== 'number')) {
113
+ throw new Error('All elements must be numbers');
114
+ }
115
+ let sum = 0;
116
+ for (const num of this) {
117
+ sum += num;
118
+ }
119
+ return sum;
120
+ }
121
+
122
+ /**
123
+ * Calculates the average of all numbers in an array.
124
+ * @returns The average value.
125
+ * @throws If any element is not a number.
126
+ */
127
+ function _avg () {
128
+ if (this.length === 0)
129
+ return undefined;
130
+ if (this.some(el => typeof el !== 'number')) {
131
+ throw new Error('All elements must be numbers');
132
+ }
133
+ return this.reduce((a, b) => a + b, 0) / this.length;
134
+ }
135
+
136
+ /**
137
+ * Returns the maximum number in the array.
138
+ * @returns The maximum value.
139
+ * @throws If any element is not a number.
140
+ */
141
+ function _max () {
142
+ if (this.length === 0)
143
+ return undefined;
144
+ if (this.some(el => typeof el !== 'number')) {
145
+ throw new Error('All elements must be numbers');
146
+ }
147
+ return Math.max(...this);
148
+ }
149
+
150
+ /**
151
+ * Returns the minimum number in the array.
152
+ * @returns The minimum value.
153
+ * @throws If any element is not a number.
154
+ */
155
+ function _min () {
156
+ if (this.length === 0)
157
+ return undefined;
158
+ if (this.some(el => typeof el !== 'number')) {
159
+ throw new Error('All elements must be numbers');
160
+ }
161
+ return Math.min(...this);
162
+ }
163
+
164
+ function shuffle$1() {
165
+ for (let i = this.length - 1; i > 0; i--) {
166
+ const j = Math.floor(Math.random() * (i + 1));
167
+ [this[i], this[j]] = [this[j], this[i]];
168
+ }
169
+ return this;
170
+ }
171
+
102
172
  const first = (arr) => _first.apply(arr);
103
173
  const last = (arr) => _last.apply(arr);
104
174
  const unique = (arr) => _unique.apply(arr);
105
175
  const chunk = (arr, size) => _chunk.apply(arr, [size]);
106
176
  const random = (arr) => _random.apply(arr);
107
177
  const keyValueMap = (arr, key, value) => _keyValueMap.apply(arr, [key, value]);
178
+ const sum = (arr) => _sum.apply(arr);
179
+ const avg = (arr) => _avg.apply(arr);
180
+ const max = (arr) => _max.apply(arr);
181
+ const min = (arr) => _min.apply(arr);
182
+ const shuffle = (arr) => shuffle$1.apply(arr);
108
183
 
184
+ exports.avg = avg;
109
185
  exports.chunk = chunk;
110
186
  exports.first = first;
111
187
  exports.keyValueMap = keyValueMap;
112
188
  exports.last = last;
189
+ exports.max = max;
190
+ exports.min = min;
113
191
  exports.random = random;
192
+ exports.shuffle = shuffle;
193
+ exports.sum = sum;
114
194
  exports.unique = unique;
115
195
  //# sourceMappingURL=index.cjs.map
@@ -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/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,CA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AAI,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EAAE;AAC7C,QAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,GAAG,CAAA,+BAAA,CAAiC,CAAC;IAC3E;IAEA,IAAI,GAAG,GAAiB,EAAE;AAE1B,IAAA,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;AACjD,QAAA,IAAI,OAAO,GAAgB,IAAI,CAAC,KAAK,CAAC;QAEtC,GAAG,CAAC,OAAO,CAAC,GAAG,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;;;;;;;;;"}
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/functions/sum.ts","../src/functions/avg.ts","../src/functions/max.ts","../src/functions/min.ts","../src/functions/shuffle.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}","/**\n * Calculates the sum of all numbers in an array.\n * @returns The sum of all numbers in the array.\n * @throws Will throw an error if any element in the array is not a number.\n * @example\n * [1, 2, 3].sum() // returns 6\n * [10, -2, 5].sum() // returns 13\n * [1, '2', 3].sum() // throws Error\n */\n\nexport default function <T>(this: T[]) { \n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n\n let sum = 0;\n\n for (const num of this as unknown as number[]) {\n sum += num;\n }\n\n return sum\n}","/**\n * Calculates the average of all numbers in an array.\n * @returns The average value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return this.reduce((a, b) => a + b, 0) / this.length;\n}","/**\n * Returns the maximum number in the array.\n * @returns The maximum value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return Math.max(...this);\n}","/**\n * Returns the minimum number in the array.\n * @returns The minimum value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return Math.min(...this);\n}","export default function shuffle<T>(this: T[]): T[] {\n for (let i = this.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [this[i], this[j]] = [this[j], this[i]];\n }\n \n return this;\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\";\nimport _sum from \"./functions/sum\";\nimport _avg from \"./functions/avg\";\nimport _max from \"./functions/max\";\nimport _min from \"./functions/min\";\nimport _shuffle from \"./functions/shuffle\";\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]);\nexport const sum = (arr: number[]): ReturnType<typeof _sum> => _sum.apply(arr);\nexport const avg = (arr: number[]): ReturnType<typeof _avg> => _avg.apply(arr);\nexport const max = (arr: number[]): ReturnType<typeof _max> => _max.apply(arr);\nexport const min = (arr: number[]): ReturnType<typeof _min> => _min.apply(arr);\nexport const shuffle = <T>(arr: T[]): ReturnType<typeof _shuffle> => _shuffle.apply(arr);"],"names":["shuffle","_shuffle"],"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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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;AAC5H,MAAM,GAAG,GAAG,CAAC,GAAa,KAA8B,IAAI,CAAC,KAAK,CAAC,GAAG;AACtE,MAAM,GAAG,GAAG,CAAC,GAAa,KAA8B,IAAI,CAAC,KAAK,CAAC,GAAG;AACtE,MAAM,GAAG,GAAG,CAAC,GAAa,KAA8B,IAAI,CAAC,KAAK,CAAC,GAAG;AACtE,MAAM,GAAG,GAAG,CAAC,GAAa,KAA8B,IAAI,CAAC,KAAK,CAAC,GAAG;AACtE,MAAM,OAAO,GAAG,CAAI,GAAQ,KAAkCC,SAAQ,CAAC,KAAK,CAAC,GAAG;;;;;;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -3,9 +3,19 @@ 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 _sum from "./functions/sum";
7
+ import _avg from "./functions/avg";
8
+ import _max from "./functions/max";
9
+ import _min from "./functions/min";
10
+ import _shuffle from "./functions/shuffle";
6
11
  export declare const first: <T>(arr: T[]) => ReturnType<typeof _first>;
7
12
  export declare const last: <T>(arr: T[]) => ReturnType<typeof _last>;
8
13
  export declare const unique: <T>(arr: T[]) => ReturnType<typeof _unique>;
9
14
  export declare const chunk: <T>(arr: T[], size: number) => ReturnType<typeof _chunk>;
10
15
  export declare const random: <T>(arr: T[]) => ReturnType<typeof _random>;
11
16
  export declare const keyValueMap: <T>(arr: KeyValueMap[], key: string, value: string) => KeyValueMap;
17
+ export declare const sum: (arr: number[]) => ReturnType<typeof _sum>;
18
+ export declare const avg: (arr: number[]) => ReturnType<typeof _avg>;
19
+ export declare const max: (arr: number[]) => ReturnType<typeof _max>;
20
+ export declare const min: (arr: number[]) => ReturnType<typeof _min>;
21
+ export declare const shuffle: <T>(arr: T[]) => ReturnType<typeof _shuffle>;
package/dist/index.js CHANGED
@@ -4,9 +4,19 @@ import _unique from "./functions/unique";
4
4
  import _chunk from "./functions/chunk";
5
5
  import _random from "./functions/random";
6
6
  import _keyValueMap from "./functions/keyValueMap";
7
+ import _sum from "./functions/sum";
8
+ import _avg from "./functions/avg";
9
+ import _max from "./functions/max";
10
+ import _min from "./functions/min";
11
+ import _shuffle from "./functions/shuffle";
7
12
  export const first = (arr) => _first.apply(arr);
8
13
  export const last = (arr) => _last.apply(arr);
9
14
  export const unique = (arr) => _unique.apply(arr);
10
15
  export const chunk = (arr, size) => _chunk.apply(arr, [size]);
11
16
  export const random = (arr) => _random.apply(arr);
12
17
  export const keyValueMap = (arr, key, value) => _keyValueMap.apply(arr, [key, value]);
18
+ export const sum = (arr) => _sum.apply(arr);
19
+ export const avg = (arr) => _avg.apply(arr);
20
+ export const max = (arr) => _max.apply(arr);
21
+ export const min = (arr) => _min.apply(arr);
22
+ export const shuffle = (arr) => _shuffle.apply(arr);
package/dist/index.mjs CHANGED
@@ -97,12 +97,87 @@ function _keyValueMap (key, value) {
97
97
  return map;
98
98
  }
99
99
 
100
+ /**
101
+ * Calculates the sum of all numbers in an array.
102
+ * @returns The sum of all numbers in the array.
103
+ * @throws Will throw an error if any element in the array is not a number.
104
+ * @example
105
+ * [1, 2, 3].sum() // returns 6
106
+ * [10, -2, 5].sum() // returns 13
107
+ * [1, '2', 3].sum() // throws Error
108
+ */
109
+ function _sum () {
110
+ if (this.some(el => typeof el !== 'number')) {
111
+ throw new Error('All elements must be numbers');
112
+ }
113
+ let sum = 0;
114
+ for (const num of this) {
115
+ sum += num;
116
+ }
117
+ return sum;
118
+ }
119
+
120
+ /**
121
+ * Calculates the average of all numbers in an array.
122
+ * @returns The average value.
123
+ * @throws If any element is not a number.
124
+ */
125
+ function _avg () {
126
+ if (this.length === 0)
127
+ return undefined;
128
+ if (this.some(el => typeof el !== 'number')) {
129
+ throw new Error('All elements must be numbers');
130
+ }
131
+ return this.reduce((a, b) => a + b, 0) / this.length;
132
+ }
133
+
134
+ /**
135
+ * Returns the maximum number in the array.
136
+ * @returns The maximum value.
137
+ * @throws If any element is not a number.
138
+ */
139
+ function _max () {
140
+ if (this.length === 0)
141
+ return undefined;
142
+ if (this.some(el => typeof el !== 'number')) {
143
+ throw new Error('All elements must be numbers');
144
+ }
145
+ return Math.max(...this);
146
+ }
147
+
148
+ /**
149
+ * Returns the minimum number in the array.
150
+ * @returns The minimum value.
151
+ * @throws If any element is not a number.
152
+ */
153
+ function _min () {
154
+ if (this.length === 0)
155
+ return undefined;
156
+ if (this.some(el => typeof el !== 'number')) {
157
+ throw new Error('All elements must be numbers');
158
+ }
159
+ return Math.min(...this);
160
+ }
161
+
162
+ function shuffle$1() {
163
+ for (let i = this.length - 1; i > 0; i--) {
164
+ const j = Math.floor(Math.random() * (i + 1));
165
+ [this[i], this[j]] = [this[j], this[i]];
166
+ }
167
+ return this;
168
+ }
169
+
100
170
  const first = (arr) => _first.apply(arr);
101
171
  const last = (arr) => _last.apply(arr);
102
172
  const unique = (arr) => _unique.apply(arr);
103
173
  const chunk = (arr, size) => _chunk.apply(arr, [size]);
104
174
  const random = (arr) => _random.apply(arr);
105
175
  const keyValueMap = (arr, key, value) => _keyValueMap.apply(arr, [key, value]);
176
+ const sum = (arr) => _sum.apply(arr);
177
+ const avg = (arr) => _avg.apply(arr);
178
+ const max = (arr) => _max.apply(arr);
179
+ const min = (arr) => _min.apply(arr);
180
+ const shuffle = (arr) => shuffle$1.apply(arr);
106
181
 
107
- export { chunk, first, keyValueMap, last, random, unique };
182
+ export { avg, chunk, first, keyValueMap, last, max, min, random, shuffle, sum, unique };
108
183
  //# sourceMappingURL=index.mjs.map
@@ -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/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,SAA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CAAC,+CAA+C,CAAC;IACjE;AAEA,IAAA,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EAAE;AAC7C,QAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,GAAG,CAAA,+BAAA,CAAiC,CAAC;IAC3E;IAEA,IAAI,GAAG,GAAiB,EAAE;AAE1B,IAAA,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;AACjD,QAAA,IAAI,OAAO,GAAgB,IAAI,CAAC,KAAK,CAAC;QAEtC,GAAG,CAAC,OAAO,CAAC,GAAG,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;;;;"}
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/functions/sum.ts","../src/functions/avg.ts","../src/functions/max.ts","../src/functions/min.ts","../src/functions/shuffle.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}","/**\n * Calculates the sum of all numbers in an array.\n * @returns The sum of all numbers in the array.\n * @throws Will throw an error if any element in the array is not a number.\n * @example\n * [1, 2, 3].sum() // returns 6\n * [10, -2, 5].sum() // returns 13\n * [1, '2', 3].sum() // throws Error\n */\n\nexport default function <T>(this: T[]) { \n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n\n let sum = 0;\n\n for (const num of this as unknown as number[]) {\n sum += num;\n }\n\n return sum\n}","/**\n * Calculates the average of all numbers in an array.\n * @returns The average value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return this.reduce((a, b) => a + b, 0) / this.length;\n}","/**\n * Returns the maximum number in the array.\n * @returns The maximum value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return Math.max(...this);\n}","/**\n * Returns the minimum number in the array.\n * @returns The minimum value.\n * @throws If any element is not a number.\n */\nexport default function(this: number[]) {\n if (this.length === 0) return undefined;\n if (this.some(el => typeof el !== 'number')) {\n throw new Error('All elements must be numbers');\n }\n return Math.min(...this);\n}","export default function shuffle<T>(this: T[]): T[] {\n for (let i = this.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [this[i], this[j]] = [this[j], this[i]];\n }\n \n return this;\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\";\nimport _sum from \"./functions/sum\";\nimport _avg from \"./functions/avg\";\nimport _max from \"./functions/max\";\nimport _min from \"./functions/min\";\nimport _shuffle from \"./functions/shuffle\";\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]);\nexport const sum = (arr: number[]): ReturnType<typeof _sum> => _sum.apply(arr);\nexport const avg = (arr: number[]): ReturnType<typeof _avg> => _avg.apply(arr);\nexport const max = (arr: number[]): ReturnType<typeof _max> => _max.apply(arr);\nexport const min = (arr: number[]): ReturnType<typeof _min> => _min.apply(arr);\nexport const shuffle = <T>(arr: T[]): ReturnType<typeof _shuffle> => _shuffle.apply(arr);"],"names":["shuffle","_shuffle"],"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,SAA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O,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;AAC5H,MAAM,GAAG,GAAG,CAAC,GAAa,KAA8B,IAAI,CAAC,KAAK,CAAC,GAAG;AACtE,MAAM,GAAG,GAAG,CAAC,GAAa,KAA8B,IAAI,CAAC,KAAK,CAAC,GAAG;AACtE,MAAM,GAAG,GAAG,CAAC,GAAa,KAA8B,IAAI,CAAC,KAAK,CAAC,GAAG;AACtE,MAAM,GAAG,GAAG,CAAC,GAAa,KAA8B,IAAI,CAAC,KAAK,CAAC,GAAG;AACtE,MAAM,OAAO,GAAG,CAAI,GAAQ,KAAkCC,SAAQ,CAAC,KAAK,CAAC,GAAG;;;;"}
@@ -6,6 +6,11 @@ declare global {
6
6
  chunk(size: number): T[][];
7
7
  random(): T | undefined;
8
8
  keyValueMap(key: string, value: string): Record<string, any>[];
9
+ sum(this: number[]): number;
10
+ avg(this: number[]): number | undefined;
11
+ max(this: number[]): number | undefined;
12
+ min(this: number[]): number | undefined;
13
+ shuffle(): T[];
9
14
  }
10
15
  }
11
16
  export {};
@@ -4,45 +4,28 @@ import last from "../functions/last";
4
4
  import random from "../functions/random";
5
5
  import unique from "../functions/unique";
6
6
  import keyValueMap from "../functions/keyValueMap";
7
- if (!Array.prototype.first) {
8
- Object.defineProperty(Array.prototype, "first", {
9
- value: first,
10
- writable: false,
11
- configurable: false
12
- });
13
- }
14
- if (!Array.prototype.last) {
15
- Object.defineProperty(Array.prototype, "last", {
16
- value: last,
17
- writable: false,
18
- configurable: false
19
- });
20
- }
21
- if (!Array.prototype.unique) {
22
- Object.defineProperty(Array.prototype, "unique", {
23
- value: unique,
24
- writable: false,
25
- configurable: false
26
- });
27
- }
28
- if (!Array.prototype.chunk) {
29
- Object.defineProperty(Array.prototype, "chunk", {
30
- value: chunk,
31
- writable: false,
32
- configurable: false
33
- });
34
- }
35
- if (!Array.prototype.random) {
36
- Object.defineProperty(Array.prototype, "random", {
37
- value: random,
38
- writable: false,
39
- configurable: false
40
- });
41
- }
42
- if (!Array.prototype.keyValueMap) {
43
- Object.defineProperty(Array.prototype, "keyValueMap", {
44
- value: keyValueMap,
45
- writable: false,
46
- configurable: false
47
- });
7
+ import sum from "../functions/sum";
8
+ import avg from "../functions/avg";
9
+ import max from "../functions/max";
10
+ import min from "../functions/min";
11
+ import shuffle from "../functions/shuffle";
12
+ function defineArrayMethod(name, fn) {
13
+ if (!Array.prototype[name]) {
14
+ Object.defineProperty(Array.prototype, name, {
15
+ value: fn,
16
+ writable: false,
17
+ configurable: false
18
+ });
19
+ }
48
20
  }
21
+ defineArrayMethod("first", first);
22
+ defineArrayMethod("last", last);
23
+ defineArrayMethod("unique", unique);
24
+ defineArrayMethod("chunk", chunk);
25
+ defineArrayMethod("random", random);
26
+ defineArrayMethod("keyValueMap", keyValueMap);
27
+ defineArrayMethod("sum", sum);
28
+ defineArrayMethod("avg", avg);
29
+ defineArrayMethod("max", max);
30
+ defineArrayMethod("min", min);
31
+ defineArrayMethod("shuffle", shuffle);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@r01al/array-polyfills",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "description": "Cool JS Array Polyfills",
5
5
  "keywords": [
6
6
  "polyfills",