@r01al/array-polyfills 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # Cool Array Polyfills
2
+ Side-effect polyfills for `Array.prototype` (+ matching polyfills) written in TypeScript.
package/dist/auto.cjs ADDED
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ function chunk (size) {
4
+ if (!Number.isInteger(size) || size <= 0)
5
+ throw new Error("size must be a positive integer");
6
+ const out = [];
7
+ for (let i = 0; i < this.length; i += size)
8
+ out.push(this.slice(i, i + size));
9
+ return out;
10
+ }
11
+
12
+ function first () { return this.length ? this[0] : undefined; }
13
+
14
+ function last () { return this.length ? this[this.length - 1] : undefined; }
15
+
16
+ function random () {
17
+ return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
18
+ }
19
+
20
+ function unique () { return [...new Set(this)]; }
21
+
22
+ if (!Array.prototype.first) {
23
+ Object.defineProperty(Array.prototype, "first", {
24
+ value: first,
25
+ writable: false,
26
+ configurable: false
27
+ });
28
+ }
29
+ if (!Array.prototype.last) {
30
+ Object.defineProperty(Array.prototype, "last", {
31
+ value: last,
32
+ writable: false,
33
+ configurable: false
34
+ });
35
+ }
36
+ if (!Array.prototype.unique) {
37
+ Object.defineProperty(Array.prototype, "unique", {
38
+ value: unique,
39
+ writable: false,
40
+ configurable: false
41
+ });
42
+ }
43
+ if (!Array.prototype.chunk) {
44
+ Object.defineProperty(Array.prototype, "chunk", {
45
+ value: chunk,
46
+ writable: false,
47
+ configurable: false
48
+ });
49
+ }
50
+ if (!Array.prototype.random) {
51
+ Object.defineProperty(Array.prototype, "random", {
52
+ value: random,
53
+ writable: false,
54
+ configurable: false
55
+ });
56
+ }
57
+ //# sourceMappingURL=auto.cjs.map
@@ -0,0 +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":["export 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}","export default function <T>(this: T[]) { return this.length ? this[0] : undefined; }","export default function <T>(this: T[]) { return this.length ? this[this.length - 1] : undefined; }","export default function <T>(this: T[]) {\n\treturn this.length ? this[Math.floor(Math.random() * this.length)] : undefined;\n}","export default function <T>(this: T[]) { return [...new Set(this)]; }","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\";\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}\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\nexport {};\n"],"names":[],"mappings":";;AAAc,cAAA,EAAyB,IAAY,EAAA;IAClD,IAAI,CAAC,MAAM,CAAC,SAAS,CA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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;;"}
package/dist/auto.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ import "./polyfills/array";
package/dist/auto.js ADDED
@@ -0,0 +1 @@
1
+ import "./polyfills/array"; // side-effect only, augment Array prototype
package/dist/auto.mjs ADDED
@@ -0,0 +1,55 @@
1
+ function chunk (size) {
2
+ if (!Number.isInteger(size) || size <= 0)
3
+ throw new Error("size must be a positive integer");
4
+ const out = [];
5
+ for (let i = 0; i < this.length; i += size)
6
+ out.push(this.slice(i, i + size));
7
+ return out;
8
+ }
9
+
10
+ function first () { return this.length ? this[0] : undefined; }
11
+
12
+ function last () { return this.length ? this[this.length - 1] : undefined; }
13
+
14
+ function random () {
15
+ return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
16
+ }
17
+
18
+ function unique () { return [...new Set(this)]; }
19
+
20
+ if (!Array.prototype.first) {
21
+ Object.defineProperty(Array.prototype, "first", {
22
+ value: first,
23
+ writable: false,
24
+ configurable: false
25
+ });
26
+ }
27
+ if (!Array.prototype.last) {
28
+ Object.defineProperty(Array.prototype, "last", {
29
+ value: last,
30
+ writable: false,
31
+ configurable: false
32
+ });
33
+ }
34
+ if (!Array.prototype.unique) {
35
+ Object.defineProperty(Array.prototype, "unique", {
36
+ value: unique,
37
+ writable: false,
38
+ configurable: false
39
+ });
40
+ }
41
+ if (!Array.prototype.chunk) {
42
+ Object.defineProperty(Array.prototype, "chunk", {
43
+ value: chunk,
44
+ writable: false,
45
+ configurable: false
46
+ });
47
+ }
48
+ if (!Array.prototype.random) {
49
+ Object.defineProperty(Array.prototype, "random", {
50
+ value: random,
51
+ writable: false,
52
+ configurable: false
53
+ });
54
+ }
55
+ //# sourceMappingURL=auto.mjs.map
@@ -0,0 +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":["export 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}","export default function <T>(this: T[]) { return this.length ? this[0] : undefined; }","export default function <T>(this: T[]) { return this.length ? this[this.length - 1] : undefined; }","export default function <T>(this: T[]) {\n\treturn this.length ? this[Math.floor(Math.random() * this.length)] : undefined;\n}","export default function <T>(this: T[]) { return [...new Set(this)]; }","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\";\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}\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\nexport {};\n"],"names":[],"mappings":"AAAc,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;;ACLc,cAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACArE,aAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GA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AAC,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"}
@@ -0,0 +1 @@
1
+ export default function <T>(this: T[], size: number): T[][];
@@ -0,0 +1,8 @@
1
+ export default function (size) {
2
+ if (!Number.isInteger(size) || size <= 0)
3
+ throw new Error("size must be a positive integer");
4
+ const out = [];
5
+ for (let i = 0; i < this.length; i += size)
6
+ out.push(this.slice(i, i + size));
7
+ return out;
8
+ }
@@ -0,0 +1 @@
1
+ export default function <T>(this: T[]): T | undefined;
@@ -0,0 +1 @@
1
+ export default function () { return this.length ? this[0] : undefined; }
@@ -0,0 +1 @@
1
+ export default function <T>(this: T[]): T | undefined;
@@ -0,0 +1 @@
1
+ export default function () { return this.length ? this[this.length - 1] : undefined; }
@@ -0,0 +1 @@
1
+ export default function <T>(this: T[]): T | undefined;
@@ -0,0 +1,3 @@
1
+ export default function () {
2
+ return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
3
+ }
@@ -0,0 +1 @@
1
+ export default function <T>(this: T[]): T[];
@@ -0,0 +1 @@
1
+ export default function () { return [...new Set(this)]; }
package/dist/index.cjs ADDED
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ function _first () { return this.length ? this[0] : undefined; }
4
+
5
+ function _last () { return this.length ? this[this.length - 1] : undefined; }
6
+
7
+ function _unique () { return [...new Set(this)]; }
8
+
9
+ function _chunk (size) {
10
+ if (!Number.isInteger(size) || size <= 0)
11
+ throw new Error("size must be a positive integer");
12
+ const out = [];
13
+ for (let i = 0; i < this.length; i += size)
14
+ out.push(this.slice(i, i + size));
15
+ return out;
16
+ }
17
+
18
+ function _random () {
19
+ return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
20
+ }
21
+
22
+ const first = (arr) => _first.apply(arr);
23
+ const last = (arr) => _last.apply(arr);
24
+ const unique = (arr) => _unique.apply(arr);
25
+ const chunk = (arr, size) => _chunk.apply(arr, [size]);
26
+ const random = (arr) => _random.apply(arr);
27
+
28
+ exports.chunk = chunk;
29
+ exports.first = first;
30
+ exports.last = last;
31
+ exports.random = random;
32
+ exports.unique = unique;
33
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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":["export default function <T>(this: T[]) { return this.length ? this[0] : undefined; }","export default function <T>(this: T[]) { return this.length ? this[this.length - 1] : undefined; }","export default function <T>(this: T[]) { return [...new Set(this)]; }","export 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}","export default function <T>(this: T[]) {\n\treturn this.length ? this[Math.floor(Math.random() * this.length)] : undefined;\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\";\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);\n\n"],"names":[],"mappings":";;AAAc,eAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACArE,cAAA,IAAA,EAA2B,OA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
+ import _first from "./functions/first";
2
+ import _last from "./functions/last";
3
+ import _unique from "./functions/unique";
4
+ import _chunk from "./functions/chunk";
5
+ import _random from "./functions/random";
6
+ export declare const first: <T>(arr: T[]) => ReturnType<typeof _first>;
7
+ export declare const last: <T>(arr: T[]) => ReturnType<typeof _last>;
8
+ export declare const unique: <T>(arr: T[]) => ReturnType<typeof _unique>;
9
+ export declare const chunk: <T>(arr: T[], size: number) => ReturnType<typeof _chunk>;
10
+ export declare const random: <T>(arr: T[]) => ReturnType<typeof _random>;
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import _first from "./functions/first";
2
+ import _last from "./functions/last";
3
+ import _unique from "./functions/unique";
4
+ import _chunk from "./functions/chunk";
5
+ import _random from "./functions/random";
6
+ export const first = (arr) => _first.apply(arr);
7
+ export const last = (arr) => _last.apply(arr);
8
+ export const unique = (arr) => _unique.apply(arr);
9
+ export const chunk = (arr, size) => _chunk.apply(arr, [size]);
10
+ export const random = (arr) => _random.apply(arr);
package/dist/index.mjs ADDED
@@ -0,0 +1,27 @@
1
+ function _first () { return this.length ? this[0] : undefined; }
2
+
3
+ function _last () { return this.length ? this[this.length - 1] : undefined; }
4
+
5
+ function _unique () { return [...new Set(this)]; }
6
+
7
+ function _chunk (size) {
8
+ if (!Number.isInteger(size) || size <= 0)
9
+ throw new Error("size must be a positive integer");
10
+ const out = [];
11
+ for (let i = 0; i < this.length; i += size)
12
+ out.push(this.slice(i, i + size));
13
+ return out;
14
+ }
15
+
16
+ function _random () {
17
+ return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
18
+ }
19
+
20
+ const first = (arr) => _first.apply(arr);
21
+ const last = (arr) => _last.apply(arr);
22
+ const unique = (arr) => _unique.apply(arr);
23
+ const chunk = (arr, size) => _chunk.apply(arr, [size]);
24
+ const random = (arr) => _random.apply(arr);
25
+
26
+ export { chunk, first, last, random, unique };
27
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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":["export default function <T>(this: T[]) { return this.length ? this[0] : undefined; }","export default function <T>(this: T[]) { return this.length ? this[this.length - 1] : undefined; }","export default function <T>(this: T[]) { return [...new Set(this)]; }","export 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}","export default function <T>(this: T[]) {\n\treturn this.length ? this[Math.floor(Math.random() * this.length)] : undefined;\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\";\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);\n\n"],"names":[],"mappings":"AAAc,eAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACArE,cAAA,IAAA,EAA2B,OAAO,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;;ACAnF,gBAAA,IAAA,EAA2B,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;;ACAtD,eAAA,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;;ACLc,gBAAA,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;;ACIO,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;;;;"}
@@ -0,0 +1,10 @@
1
+ declare global {
2
+ interface Array<T> {
3
+ first(): T | undefined;
4
+ last(): T | undefined;
5
+ unique(): T[];
6
+ chunk(size: number): T[][];
7
+ random(): T | undefined;
8
+ }
9
+ }
10
+ export {};
@@ -0,0 +1,40 @@
1
+ import chunk from "../functions/chunk";
2
+ import first from "../functions/first";
3
+ import last from "../functions/last";
4
+ import random from "../functions/random";
5
+ import unique from "../functions/unique";
6
+ if (!Array.prototype.first) {
7
+ Object.defineProperty(Array.prototype, "first", {
8
+ value: first,
9
+ writable: false,
10
+ configurable: false
11
+ });
12
+ }
13
+ if (!Array.prototype.last) {
14
+ Object.defineProperty(Array.prototype, "last", {
15
+ value: last,
16
+ writable: false,
17
+ configurable: false
18
+ });
19
+ }
20
+ if (!Array.prototype.unique) {
21
+ Object.defineProperty(Array.prototype, "unique", {
22
+ value: unique,
23
+ writable: false,
24
+ configurable: false
25
+ });
26
+ }
27
+ if (!Array.prototype.chunk) {
28
+ Object.defineProperty(Array.prototype, "chunk", {
29
+ value: chunk,
30
+ writable: false,
31
+ configurable: false
32
+ });
33
+ }
34
+ if (!Array.prototype.random) {
35
+ Object.defineProperty(Array.prototype, "random", {
36
+ value: random,
37
+ writable: false,
38
+ configurable: false
39
+ });
40
+ }
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@r01al/array-polyfills",
3
+ "version": "1.0.0",
4
+ "description": "Cool JS Array Polyfills",
5
+ "keywords": [
6
+ "polyfills",
7
+ "array",
8
+ "cool"
9
+ ],
10
+ "homepage": "https://github.com/RoiTal88/js-array-polyfills#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/RoiTal88/js-array-polyfills/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/RoiTal88/js-array-polyfills.git"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Roi Tal <roital88@gmail.com>",
20
+ "type": "module",
21
+ "scripts": {
22
+ "test": "vitest run",
23
+ "prebuild": "rm -rf dist",
24
+ "build": "tsc -p tsconfig.json && rollup -c",
25
+ "dev": "vitest --watch",
26
+ "prepublishOnly": "npm run build && npm test"
27
+ },
28
+ "devDependencies": {
29
+ "@rollup/plugin-commonjs": "^28.0.6",
30
+ "@rollup/plugin-node-resolve": "^16.0.1",
31
+ "@rollup/plugin-typescript": "^12.1.4",
32
+ "rollup": "^4.49.0",
33
+ "typescript": "^5.9.2",
34
+ "vitest": "^3.2.4"
35
+ },
36
+ "module": "./dist/index.mjs",
37
+ "main": "./dist/index.cjs",
38
+ "exports": {
39
+ ".": {
40
+ "types": "./dist/index.d.ts",
41
+ "import": "./dist/index.mjs",
42
+ "require": "./dist/index.cjs"
43
+ },
44
+ "./polyfill": {
45
+ "types": "./dist/index.d.ts",
46
+ "import": "./dist/index.mjs",
47
+ "require": "./dist/index.cjs"
48
+ },
49
+ "./auto": {
50
+ "types": "./dist/auto.d.ts",
51
+ "import": "./dist/auto.mjs",
52
+ "require": "./dist/auto.cjs"
53
+ }
54
+ },
55
+ "sideEffects": [
56
+ "./dist/auto.mjs",
57
+ "./dist/auto.cjs",
58
+ "./src/auto.ts",
59
+ "./src/polyfills/array.ts"
60
+ ],
61
+ "files": [
62
+ "dist",
63
+ "README.md",
64
+ "LICENSE"
65
+ ]
66
+ }