@js-utils-kit/array 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sriman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ function e(e,t=1){if(t<=0)throw RangeError(`chunk size must be greater than 0`);return Array.from({length:Math.ceil(e.length/t)},(n,r)=>e.slice(r*t,r*t+t))}function t(e){return e.filter(e=>!!e&&!Number.isNaN(e))}function n(e){return e.at(-1)}function r(e,{sort:t=!1,compareFn:n}={}){let r=[...new Set(e)];return t?n?r.sort(n):r.sort():r}exports.chunk=e,exports.compact=t,exports.lastElement=n,exports.unique=r;var i=require(`@js-utils-kit/types`);Object.keys(i).forEach(function(e){e!==`default`&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:function(){return i[e]}})});
@@ -0,0 +1,123 @@
1
+ import { Falsy } from "@js-utils-kit/types";
2
+ export * from "@js-utils-kit/types";
3
+
4
+ //#region src/chunk.d.ts
5
+
6
+ /**
7
+ * Splits an array into chunks of a specified size.
8
+ *
9
+ * @returns list of grouped chunks
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * chunk([1, 2, 3, 4], 2);
14
+ * // [[1, 2], [3, 4]]
15
+ *
16
+ * chunk(['a', 'b', 'c'], 1);
17
+ * // [['a'], ['b'], ['c']]
18
+ * ```
19
+ */
20
+ declare function chunk<T>(
21
+ /**
22
+ * List of elements.
23
+ */
24
+ array: readonly T[],
25
+ /**
26
+ * Length of each chunk to group.
27
+ */
28
+ size?: number): T[][];
29
+ //#endregion
30
+ //#region src/compact.d.ts
31
+ /**
32
+ * Removes all falsy values from an array.
33
+ *
34
+ * Falsy values include: `false`, `null`, `0`, `""`, `undefined`, and `NaN`.
35
+ *
36
+ * @returns A new array with all falsy values removed
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * compact([0, 1, false, 2, '', 3, null]);
41
+ * // [1, 2, 3]
42
+ *
43
+ * compact(['a', '', 'b', undefined]);
44
+ * // ['a', 'b']
45
+ * ```
46
+ */
47
+ declare function compact<T>(
48
+ /**
49
+ * A list of elements to compact
50
+ */
51
+ array: readonly T[]): Exclude<T, Falsy>[];
52
+ //#endregion
53
+ //#region src/lastElement.d.ts
54
+ /**
55
+ * Returns the last element of an array.
56
+ *
57
+ * @returns The last element, or `undefined` if the array is empty
58
+ *
59
+ * @example
60
+ * lastElement([1, 2, 3]);
61
+ * // 3
62
+ *
63
+ * lastElement([]);
64
+ * // undefined
65
+ */
66
+ declare function lastElement<T>(
67
+ /**
68
+ * A list of elements.
69
+ */
70
+ array: readonly T[]): T | undefined;
71
+ //#endregion
72
+ //#region src/unique.d.ts
73
+ /**
74
+ * Returns a new array with duplicate values removed.
75
+ *
76
+ * Uniqueness is determined using `Set`, meaning object uniqueness is based on reference equality.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * unique([3, 1, 2, 1]);
81
+ * // [3, 1, 2]
82
+ *
83
+ * unique([3, 1, 2, 1], { sort: true });
84
+ * // [1, 2, 3]
85
+ *
86
+ * const user1 = { id: 1, name: 'Alice' };
87
+ * const user2 = { id: 2, name: 'Bob' };
88
+ * const user3 = { id: 3, name: 'Charlie' };
89
+ * const users = [user3, user1, user2, user1];
90
+ *
91
+ * unique(users, {
92
+ * sort: true,
93
+ * compareFn: (a, b) => a.id - b.id,
94
+ * });
95
+ *
96
+ * // [
97
+ * // { id: 1, name: 'Alice' },
98
+ * // { id: 2, name: 'Bob' },
99
+ * // { id: 3, name: 'Charlie' }
100
+ * // ]
101
+ * ```
102
+ */
103
+ declare function unique<T>(
104
+ /**
105
+ * The source array.
106
+ */
107
+ array: readonly T[], {
108
+ sort,
109
+ compareFn
110
+ }?: {
111
+ /**
112
+ * Whether to sort the result.
113
+ *
114
+ * @default false
115
+ */
116
+ sort?: boolean;
117
+ /**
118
+ * Custom compare function for sorting.
119
+ */
120
+ compareFn?: (a: T, b: T) => number;
121
+ }): T[];
122
+ //#endregion
123
+ export { chunk, compact, lastElement, unique };
@@ -0,0 +1,123 @@
1
+ import { Falsy } from "@js-utils-kit/types";
2
+ export * from "@js-utils-kit/types";
3
+
4
+ //#region src/chunk.d.ts
5
+
6
+ /**
7
+ * Splits an array into chunks of a specified size.
8
+ *
9
+ * @returns list of grouped chunks
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * chunk([1, 2, 3, 4], 2);
14
+ * // [[1, 2], [3, 4]]
15
+ *
16
+ * chunk(['a', 'b', 'c'], 1);
17
+ * // [['a'], ['b'], ['c']]
18
+ * ```
19
+ */
20
+ declare function chunk<T>(
21
+ /**
22
+ * List of elements.
23
+ */
24
+ array: readonly T[],
25
+ /**
26
+ * Length of each chunk to group.
27
+ */
28
+ size?: number): T[][];
29
+ //#endregion
30
+ //#region src/compact.d.ts
31
+ /**
32
+ * Removes all falsy values from an array.
33
+ *
34
+ * Falsy values include: `false`, `null`, `0`, `""`, `undefined`, and `NaN`.
35
+ *
36
+ * @returns A new array with all falsy values removed
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * compact([0, 1, false, 2, '', 3, null]);
41
+ * // [1, 2, 3]
42
+ *
43
+ * compact(['a', '', 'b', undefined]);
44
+ * // ['a', 'b']
45
+ * ```
46
+ */
47
+ declare function compact<T>(
48
+ /**
49
+ * A list of elements to compact
50
+ */
51
+ array: readonly T[]): Exclude<T, Falsy>[];
52
+ //#endregion
53
+ //#region src/lastElement.d.ts
54
+ /**
55
+ * Returns the last element of an array.
56
+ *
57
+ * @returns The last element, or `undefined` if the array is empty
58
+ *
59
+ * @example
60
+ * lastElement([1, 2, 3]);
61
+ * // 3
62
+ *
63
+ * lastElement([]);
64
+ * // undefined
65
+ */
66
+ declare function lastElement<T>(
67
+ /**
68
+ * A list of elements.
69
+ */
70
+ array: readonly T[]): T | undefined;
71
+ //#endregion
72
+ //#region src/unique.d.ts
73
+ /**
74
+ * Returns a new array with duplicate values removed.
75
+ *
76
+ * Uniqueness is determined using `Set`, meaning object uniqueness is based on reference equality.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * unique([3, 1, 2, 1]);
81
+ * // [3, 1, 2]
82
+ *
83
+ * unique([3, 1, 2, 1], { sort: true });
84
+ * // [1, 2, 3]
85
+ *
86
+ * const user1 = { id: 1, name: 'Alice' };
87
+ * const user2 = { id: 2, name: 'Bob' };
88
+ * const user3 = { id: 3, name: 'Charlie' };
89
+ * const users = [user3, user1, user2, user1];
90
+ *
91
+ * unique(users, {
92
+ * sort: true,
93
+ * compareFn: (a, b) => a.id - b.id,
94
+ * });
95
+ *
96
+ * // [
97
+ * // { id: 1, name: 'Alice' },
98
+ * // { id: 2, name: 'Bob' },
99
+ * // { id: 3, name: 'Charlie' }
100
+ * // ]
101
+ * ```
102
+ */
103
+ declare function unique<T>(
104
+ /**
105
+ * The source array.
106
+ */
107
+ array: readonly T[], {
108
+ sort,
109
+ compareFn
110
+ }?: {
111
+ /**
112
+ * Whether to sort the result.
113
+ *
114
+ * @default false
115
+ */
116
+ sort?: boolean;
117
+ /**
118
+ * Custom compare function for sorting.
119
+ */
120
+ compareFn?: (a: T, b: T) => number;
121
+ }): T[];
122
+ //#endregion
123
+ export { chunk, compact, lastElement, unique };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ export*from"@js-utils-kit/types";function e(e,t=1){if(t<=0)throw RangeError(`chunk size must be greater than 0`);return Array.from({length:Math.ceil(e.length/t)},(n,r)=>e.slice(r*t,r*t+t))}function t(e){return e.filter(e=>!!e&&!Number.isNaN(e))}function n(e){return e.at(-1)}function r(e,{sort:t=!1,compareFn:n}={}){let r=[...new Set(e)];return t?n?r.sort(n):r.sort():r}export{e as chunk,t as compact,n as lastElement,r as unique};
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@js-utils-kit/array",
3
+ "version": "0.1.0",
4
+ "description": "Array utilities",
5
+ "private": false,
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "Sriman",
9
+ "email": "136729116+TenEplaysOfficial@users.noreply.github.com",
10
+ "url": "https://tene.vercel.app"
11
+ },
12
+ "homepage": "https://js-utils.js.org",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/teneplaysofficial/js-utils-kit",
16
+ "directory": "packages/array"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/teneplaysofficial/js-utils-kit/issues"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "engines": {
25
+ "node": ">=22"
26
+ },
27
+ "type": "module",
28
+ "main": "./dist/index.cjs",
29
+ "module": "./dist/index.mjs",
30
+ "types": "./dist/index.d.cts",
31
+ "exports": {
32
+ ".": {
33
+ "require": "./dist/index.cjs",
34
+ "import": "./dist/index.mjs"
35
+ }
36
+ },
37
+ "dependencies": {
38
+ "@js-utils-kit/types": "1.1.0"
39
+ },
40
+ "scripts": {
41
+ "build": "tsdown",
42
+ "test": "vitest run"
43
+ }
44
+ }