@catbee/utils 0.0.8-rc.3 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/build/index.cjs +6912 -0
- package/build/index.d.ts +5284 -0
- package/build/index.mjs +6635 -0
- package/package.json +13 -292
- package/build/cjs/config.d.ts +0 -121
- package/build/cjs/config.js +0 -137
- package/build/cjs/index.d.ts +0 -26
- package/build/cjs/index.js +0 -68
- package/build/cjs/servers/server.builder.d.ts +0 -507
- package/build/cjs/servers/server.builder.js +0 -658
- package/build/cjs/servers/server.d.ts +0 -255
- package/build/cjs/servers/server.js +0 -970
- package/build/cjs/types/api-response.d.ts +0 -151
- package/build/cjs/types/api-response.js +0 -36
- package/build/cjs/types/index.d.ts +0 -124
- package/build/cjs/types/index.js +0 -25
- package/build/cjs/types/server.d.ts +0 -267
- package/build/cjs/types/server.js +0 -25
- package/build/cjs/utils/array.utils.d.ts +0 -167
- package/build/cjs/utils/array.utils.js +0 -363
- package/build/cjs/utils/async.utils.d.ts +0 -264
- package/build/cjs/utils/async.utils.js +0 -640
- package/build/cjs/utils/cache.utils.d.ts +0 -152
- package/build/cjs/utils/cache.utils.js +0 -298
- package/build/cjs/utils/context-store.utils.d.ts +0 -188
- package/build/cjs/utils/context-store.utils.js +0 -297
- package/build/cjs/utils/crypto.utils.d.ts +0 -159
- package/build/cjs/utils/crypto.utils.js +0 -295
- package/build/cjs/utils/date.utils.d.ts +0 -158
- package/build/cjs/utils/date.utils.js +0 -395
- package/build/cjs/utils/decorators.utils.d.ts +0 -511
- package/build/cjs/utils/decorators.utils.js +0 -1035
- package/build/cjs/utils/dir.utils.d.ts +0 -195
- package/build/cjs/utils/dir.utils.js +0 -500
- package/build/cjs/utils/env.utils.d.ts +0 -376
- package/build/cjs/utils/env.utils.js +0 -786
- package/build/cjs/utils/exception.utils.d.ts +0 -229
- package/build/cjs/utils/exception.utils.js +0 -408
- package/build/cjs/utils/fs.utils.d.ts +0 -163
- package/build/cjs/utils/fs.utils.js +0 -371
- package/build/cjs/utils/http-status-codes.d.ts +0 -265
- package/build/cjs/utils/http-status-codes.js +0 -297
- package/build/cjs/utils/id.utils.d.ts +0 -35
- package/build/cjs/utils/id.utils.js +0 -90
- package/build/cjs/utils/logger.utils.d.ts +0 -159
- package/build/cjs/utils/logger.utils.js +0 -350
- package/build/cjs/utils/middleware.utils.d.ts +0 -99
- package/build/cjs/utils/middleware.utils.js +0 -243
- package/build/cjs/utils/obj.utils.d.ts +0 -123
- package/build/cjs/utils/obj.utils.js +0 -428
- package/build/cjs/utils/performance.utils.d.ts +0 -135
- package/build/cjs/utils/performance.utils.js +0 -280
- package/build/cjs/utils/request.utils.d.ts +0 -85
- package/build/cjs/utils/request.utils.js +0 -199
- package/build/cjs/utils/response.utils.d.ts +0 -162
- package/build/cjs/utils/response.utils.js +0 -274
- package/build/cjs/utils/stream.utils.d.ts +0 -87
- package/build/cjs/utils/stream.utils.js +0 -217
- package/build/cjs/utils/string.utils.d.ts +0 -92
- package/build/cjs/utils/string.utils.js +0 -178
- package/build/cjs/utils/type.utils.d.ts +0 -89
- package/build/cjs/utils/type.utils.js +0 -195
- package/build/cjs/utils/url.utils.d.ts +0 -140
- package/build/cjs/utils/url.utils.js +0 -316
- package/build/cjs/utils/validate.utils.d.ts +0 -176
- package/build/cjs/utils/validate.utils.js +0 -344
- package/build/esm/config.d.ts +0 -121
- package/build/esm/config.js +0 -132
- package/build/esm/index.d.ts +0 -26
- package/build/esm/index.js +0 -49
- package/build/esm/servers/server.builder.d.ts +0 -507
- package/build/esm/servers/server.builder.js +0 -654
- package/build/esm/servers/server.d.ts +0 -255
- package/build/esm/servers/server.js +0 -963
- package/build/esm/types/api-response.d.ts +0 -151
- package/build/esm/types/api-response.js +0 -33
- package/build/esm/types/index.d.ts +0 -124
- package/build/esm/types/index.js +0 -24
- package/build/esm/types/server.d.ts +0 -267
- package/build/esm/types/server.js +0 -24
- package/build/esm/utils/array.utils.d.ts +0 -167
- package/build/esm/utils/array.utils.js +0 -344
- package/build/esm/utils/async.utils.d.ts +0 -264
- package/build/esm/utils/async.utils.js +0 -619
- package/build/esm/utils/cache.utils.d.ts +0 -152
- package/build/esm/utils/cache.utils.js +0 -294
- package/build/esm/utils/context-store.utils.d.ts +0 -188
- package/build/esm/utils/context-store.utils.js +0 -290
- package/build/esm/utils/crypto.utils.d.ts +0 -159
- package/build/esm/utils/crypto.utils.js +0 -278
- package/build/esm/utils/date.utils.d.ts +0 -158
- package/build/esm/utils/date.utils.js +0 -383
- package/build/esm/utils/decorators.utils.d.ts +0 -511
- package/build/esm/utils/decorators.utils.js +0 -1013
- package/build/esm/utils/dir.utils.d.ts +0 -195
- package/build/esm/utils/dir.utils.js +0 -476
- package/build/esm/utils/env.utils.d.ts +0 -376
- package/build/esm/utils/env.utils.js +0 -782
- package/build/esm/utils/exception.utils.d.ts +0 -229
- package/build/esm/utils/exception.utils.js +0 -382
- package/build/esm/utils/fs.utils.d.ts +0 -163
- package/build/esm/utils/fs.utils.js +0 -348
- package/build/esm/utils/http-status-codes.d.ts +0 -265
- package/build/esm/utils/http-status-codes.js +0 -294
- package/build/esm/utils/id.utils.d.ts +0 -35
- package/build/esm/utils/id.utils.js +0 -83
- package/build/esm/utils/logger.utils.d.ts +0 -159
- package/build/esm/utils/logger.utils.js +0 -304
- package/build/esm/utils/middleware.utils.d.ts +0 -99
- package/build/esm/utils/middleware.utils.js +0 -235
- package/build/esm/utils/obj.utils.d.ts +0 -123
- package/build/esm/utils/obj.utils.js +0 -412
- package/build/esm/utils/performance.utils.d.ts +0 -135
- package/build/esm/utils/performance.utils.js +0 -273
- package/build/esm/utils/request.utils.d.ts +0 -85
- package/build/esm/utils/request.utils.js +0 -190
- package/build/esm/utils/response.utils.d.ts +0 -162
- package/build/esm/utils/response.utils.js +0 -261
- package/build/esm/utils/stream.utils.d.ts +0 -87
- package/build/esm/utils/stream.utils.js +0 -209
- package/build/esm/utils/string.utils.d.ts +0 -92
- package/build/esm/utils/string.utils.js +0 -164
- package/build/esm/utils/type.utils.d.ts +0 -89
- package/build/esm/utils/type.utils.js +0 -186
- package/build/esm/utils/url.utils.d.ts +0 -140
- package/build/esm/utils/url.utils.js +0 -303
- package/build/esm/utils/validate.utils.d.ts +0 -176
- package/build/esm/utils/validate.utils.js +0 -319
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Splits an array into chunks of the specified size.
|
|
3
|
-
*
|
|
4
|
-
* @template T The type of array elements.
|
|
5
|
-
* @param {T[]} array - The array to split into chunks.
|
|
6
|
-
* @param {number} size - The number of elements per chunk.
|
|
7
|
-
* @returns {T[][]} A new array containing chunked arrays.
|
|
8
|
-
* @throws {TypeError} If array is not an array.
|
|
9
|
-
* @throws {Error} If chunk size is not a positive integer.
|
|
10
|
-
*/
|
|
11
|
-
export declare function chunk<T>(array: T[], size: number): T[][];
|
|
12
|
-
/**
|
|
13
|
-
* Removes duplicate values from an array.
|
|
14
|
-
* Optionally enforces uniqueness by a key function.
|
|
15
|
-
*
|
|
16
|
-
* @template T The type of array elements.
|
|
17
|
-
* @param {T[]} array - The input array.
|
|
18
|
-
* @param {(item: T) => unknown} [keyFn] - Optional function to determine uniqueness by key.
|
|
19
|
-
* @returns {T[]} A new array with unique values.
|
|
20
|
-
*/
|
|
21
|
-
export declare function unique<T>(array: T[], keyFn?: (item: T) => unknown): T[];
|
|
22
|
-
/**
|
|
23
|
-
* Deeply flattens a nested array to a single-level array.
|
|
24
|
-
*
|
|
25
|
-
* @template T The leaf type of array elements.
|
|
26
|
-
* @param {any[]} array - The (possibly deeply nested) input array.
|
|
27
|
-
* @returns {T[]} A deeply flattened array.
|
|
28
|
-
*/
|
|
29
|
-
export declare function flattenDeep<T>(array: any[]): T[];
|
|
30
|
-
/**
|
|
31
|
-
* Returns a random element from an array, or undefined if empty.
|
|
32
|
-
*
|
|
33
|
-
* @template T The type of array elements.
|
|
34
|
-
* @param {T[]} array - The input array.
|
|
35
|
-
* @returns {T | undefined} A randomly selected item, or undefined if array is empty or not an array.
|
|
36
|
-
*/
|
|
37
|
-
export declare function random<T>(array: T[]): T | undefined;
|
|
38
|
-
/**
|
|
39
|
-
* Groups items in an array by a key or key function.
|
|
40
|
-
*
|
|
41
|
-
* @template T The type of array elements.
|
|
42
|
-
* @overload
|
|
43
|
-
* @param {T[]} array - The array to group.
|
|
44
|
-
* @param {keyof T} key - Property key to group by.
|
|
45
|
-
* @returns {Record<string, T[]>}
|
|
46
|
-
* @overload
|
|
47
|
-
* @param {T[]} array - The array to group.
|
|
48
|
-
* @param {(item: T) => string | number | symbol} keyFn - Function to generate group key from item.
|
|
49
|
-
* @returns {Record<K, T[]>}
|
|
50
|
-
* @param {T[]} array - The array to group.
|
|
51
|
-
* @param {keyof T | ((item: T) => string | number | symbol)} keyOrFn - Property key or key-generating function.
|
|
52
|
-
* @returns {Record<string | number | symbol, T[]>} An object mapping group keys to item arrays.
|
|
53
|
-
*/
|
|
54
|
-
export declare function groupBy<T>(array: T[], key: keyof T): Record<string, T[]>;
|
|
55
|
-
export declare function groupBy<T, K extends string | number | symbol>(array: T[], keyFn: (item: T) => K): Record<K, T[]>;
|
|
56
|
-
/**
|
|
57
|
-
* Shuffles an array using the Fisher-Yates algorithm.
|
|
58
|
-
*
|
|
59
|
-
* @template T The type of array elements.
|
|
60
|
-
* @param {T[]} array - The input array.
|
|
61
|
-
* @returns {T[]} A new shuffled array.
|
|
62
|
-
* @throws {TypeError} If array is not an array.
|
|
63
|
-
*/
|
|
64
|
-
export declare function shuffle<T>(array: T[]): T[];
|
|
65
|
-
/**
|
|
66
|
-
* Returns an array of property values from an array of objects.
|
|
67
|
-
*
|
|
68
|
-
* @template T The type of array elements.
|
|
69
|
-
* @template K The object property to pluck.
|
|
70
|
-
* @param {T[]} array - The input array.
|
|
71
|
-
* @param {K} key - The property name to pluck.
|
|
72
|
-
* @returns {T[K][]} Array of property values.
|
|
73
|
-
*/
|
|
74
|
-
export declare function pluck<T, K extends keyof T>(array: T[], key: K): T[K][];
|
|
75
|
-
/**
|
|
76
|
-
* Returns values in array A that are not in array B.
|
|
77
|
-
*
|
|
78
|
-
* @template T The type of array elements.
|
|
79
|
-
* @param {T[]} a - First array.
|
|
80
|
-
* @param {T[]} b - Second array.
|
|
81
|
-
* @returns {T[]} Elements in A that are not in B.
|
|
82
|
-
*/
|
|
83
|
-
export declare function difference<T>(a: T[], b: T[]): T[];
|
|
84
|
-
/**
|
|
85
|
-
* Returns common values between arrays A and B.
|
|
86
|
-
*
|
|
87
|
-
* @template T The type of array elements.
|
|
88
|
-
* @param {T[]} a - First array.
|
|
89
|
-
* @param {T[]} b - Second array.
|
|
90
|
-
* @returns {T[]} Elements that exist in both arrays.
|
|
91
|
-
*/
|
|
92
|
-
export declare function intersect<T>(a: T[], b: T[]): T[];
|
|
93
|
-
/**
|
|
94
|
-
* Sorts an array of objects by a nested key using Merge Sort (O(n log n)).
|
|
95
|
-
* Missing/undefined keys are sorted to the "end" (asc) or "start" (desc).
|
|
96
|
-
*
|
|
97
|
-
* @template T The type of array elements (objects).
|
|
98
|
-
* @param {T[]} array - Array of objects to sort.
|
|
99
|
-
* @param {string | ((item: T) => any)} key - Dot-notated key (e.g., "profile.age") or function.
|
|
100
|
-
* @param {"asc" | "desc"} [direction="asc"] - Sort direction: 'asc' or 'desc'.
|
|
101
|
-
* @returns {T[]} A new sorted array.
|
|
102
|
-
* @throws {TypeError} If array is not an array.
|
|
103
|
-
*/
|
|
104
|
-
export declare function mergeSort<T>(array: T[], key: string | ((item: T) => any), direction?: 'asc' | 'desc'): T[];
|
|
105
|
-
/**
|
|
106
|
-
* Combines multiple arrays into a single array of grouped elements.
|
|
107
|
-
* Output array length equals the length of the shortest input array.
|
|
108
|
-
*
|
|
109
|
-
* @example zip([1, 2], ['a', 'b']) => [[1, 'a'], [2, 'b']]
|
|
110
|
-
* @param {...Array<T>[]} arrays - Two or more arrays to zip together.
|
|
111
|
-
* @returns {Array<T[]>} Array of grouped elements.
|
|
112
|
-
*/
|
|
113
|
-
export declare function zip<T>(...arrays: T[][]): T[][];
|
|
114
|
-
/**
|
|
115
|
-
* Splits an array into two arrays based on a predicate function.
|
|
116
|
-
*
|
|
117
|
-
* @template T The type of array elements.
|
|
118
|
-
* @param {T[]} array - The input array.
|
|
119
|
-
* @param {(item: T, index: number, array: T[]) => boolean} predicate - Function to test each element.
|
|
120
|
-
* @returns {[T[], T[]]} A tuple of two arrays: [matched, unmatched].
|
|
121
|
-
*/
|
|
122
|
-
export declare function partition<T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): [T[], T[]];
|
|
123
|
-
/**
|
|
124
|
-
* Generates an array of numbers within a specified range.
|
|
125
|
-
*
|
|
126
|
-
* @param {number} start - Start of range (inclusive).
|
|
127
|
-
* @param {number} end - End of range (exclusive).
|
|
128
|
-
* @param {number} [step=1] - Step between numbers.
|
|
129
|
-
* @returns {number[]} Array of numbers in range.
|
|
130
|
-
*/
|
|
131
|
-
export declare function range(start: number, end: number, step?: number): number[];
|
|
132
|
-
/**
|
|
133
|
-
* Returns the first n elements of an array.
|
|
134
|
-
*
|
|
135
|
-
* @template T The type of array elements.
|
|
136
|
-
* @param {T[]} array - The input array.
|
|
137
|
-
* @param {number} [n=1] - Number of elements to take.
|
|
138
|
-
* @returns {T[]} New array with first n elements.
|
|
139
|
-
*/
|
|
140
|
-
export declare function take<T>(array: T[], n?: number): T[];
|
|
141
|
-
/**
|
|
142
|
-
* Takes elements from the array while predicate returns true.
|
|
143
|
-
*
|
|
144
|
-
* @template T The type of array elements.
|
|
145
|
-
* @param {T[]} array - The input array.
|
|
146
|
-
* @param {(item: T, index: number) => boolean} predicate - Function to test each element.
|
|
147
|
-
* @returns {T[]} New array with taken elements.
|
|
148
|
-
*/
|
|
149
|
-
export declare function takeWhile<T>(array: T[], predicate: (item: T, index: number) => boolean): T[];
|
|
150
|
-
/**
|
|
151
|
-
* Removes all falsy values from an array.
|
|
152
|
-
* False, null, 0, "", undefined, and NaN are falsy.
|
|
153
|
-
*
|
|
154
|
-
* @template T The type of array elements.
|
|
155
|
-
* @param {T[]} array - The input array.
|
|
156
|
-
* @returns {NonNullable<T>[]} New array with falsy values removed.
|
|
157
|
-
*/
|
|
158
|
-
export declare function compact<T>(array: T[]): NonNullable<T>[];
|
|
159
|
-
/**
|
|
160
|
-
* Counts array elements by a key function.
|
|
161
|
-
*
|
|
162
|
-
* @template T The type of array elements.
|
|
163
|
-
* @param {T[]} array - The input array.
|
|
164
|
-
* @param {(item: T) => string | number | symbol} keyFn - Function to generate count key.
|
|
165
|
-
* @returns {Record<string, number>} Object with counts by key.
|
|
166
|
-
*/
|
|
167
|
-
export declare function countBy<T>(array: T[], keyFn: (item: T) => string | number | symbol): Record<string, number>;
|
|
@@ -1,363 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* The MIT License
|
|
4
|
-
*
|
|
5
|
-
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
6
|
-
*
|
|
7
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
* in the Software without restriction, including without limitation the rights
|
|
10
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
* furnished to do so, subject to the following conditions:
|
|
13
|
-
*
|
|
14
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
-
* copies or substantial portions of the Software.
|
|
16
|
-
*
|
|
17
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
-
* SOFTWARE.
|
|
24
|
-
*/
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.chunk = chunk;
|
|
27
|
-
exports.unique = unique;
|
|
28
|
-
exports.flattenDeep = flattenDeep;
|
|
29
|
-
exports.random = random;
|
|
30
|
-
exports.groupBy = groupBy;
|
|
31
|
-
exports.shuffle = shuffle;
|
|
32
|
-
exports.pluck = pluck;
|
|
33
|
-
exports.difference = difference;
|
|
34
|
-
exports.intersect = intersect;
|
|
35
|
-
exports.mergeSort = mergeSort;
|
|
36
|
-
exports.zip = zip;
|
|
37
|
-
exports.partition = partition;
|
|
38
|
-
exports.range = range;
|
|
39
|
-
exports.take = take;
|
|
40
|
-
exports.takeWhile = takeWhile;
|
|
41
|
-
exports.compact = compact;
|
|
42
|
-
exports.countBy = countBy;
|
|
43
|
-
const obj_utils_1 = require("./obj.utils");
|
|
44
|
-
/**
|
|
45
|
-
* Splits an array into chunks of the specified size.
|
|
46
|
-
*
|
|
47
|
-
* @template T The type of array elements.
|
|
48
|
-
* @param {T[]} array - The array to split into chunks.
|
|
49
|
-
* @param {number} size - The number of elements per chunk.
|
|
50
|
-
* @returns {T[][]} A new array containing chunked arrays.
|
|
51
|
-
* @throws {TypeError} If array is not an array.
|
|
52
|
-
* @throws {Error} If chunk size is not a positive integer.
|
|
53
|
-
*/
|
|
54
|
-
function chunk(array, size) {
|
|
55
|
-
if (!Array.isArray(array))
|
|
56
|
-
throw new TypeError('Expected an array');
|
|
57
|
-
if (!array.length)
|
|
58
|
-
return [];
|
|
59
|
-
if (!Number.isInteger(size) || size <= 0)
|
|
60
|
-
throw new Error('Chunk size must be a positive integer');
|
|
61
|
-
return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size));
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Removes duplicate values from an array.
|
|
65
|
-
* Optionally enforces uniqueness by a key function.
|
|
66
|
-
*
|
|
67
|
-
* @template T The type of array elements.
|
|
68
|
-
* @param {T[]} array - The input array.
|
|
69
|
-
* @param {(item: T) => unknown} [keyFn] - Optional function to determine uniqueness by key.
|
|
70
|
-
* @returns {T[]} A new array with unique values.
|
|
71
|
-
*/
|
|
72
|
-
function unique(array, keyFn) {
|
|
73
|
-
if (!Array.isArray(array) || array.length === 0)
|
|
74
|
-
return [];
|
|
75
|
-
if (!keyFn)
|
|
76
|
-
return Array.from(new Set(array));
|
|
77
|
-
const seen = new Set();
|
|
78
|
-
return array.filter(item => {
|
|
79
|
-
const key = keyFn(item);
|
|
80
|
-
if (seen.has(key))
|
|
81
|
-
return false;
|
|
82
|
-
seen.add(key);
|
|
83
|
-
return true;
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Deeply flattens a nested array to a single-level array.
|
|
88
|
-
*
|
|
89
|
-
* @template T The leaf type of array elements.
|
|
90
|
-
* @param {any[]} array - The (possibly deeply nested) input array.
|
|
91
|
-
* @returns {T[]} A deeply flattened array.
|
|
92
|
-
*/
|
|
93
|
-
function flattenDeep(array) {
|
|
94
|
-
if (!Array.isArray(array))
|
|
95
|
-
return [];
|
|
96
|
-
return array.reduce((acc, val) => {
|
|
97
|
-
if (Array.isArray(val)) {
|
|
98
|
-
acc.push(...flattenDeep(val));
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
acc.push(val);
|
|
102
|
-
}
|
|
103
|
-
return acc;
|
|
104
|
-
}, []);
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Returns a random element from an array, or undefined if empty.
|
|
108
|
-
*
|
|
109
|
-
* @template T The type of array elements.
|
|
110
|
-
* @param {T[]} array - The input array.
|
|
111
|
-
* @returns {T | undefined} A randomly selected item, or undefined if array is empty or not an array.
|
|
112
|
-
*/
|
|
113
|
-
function random(array) {
|
|
114
|
-
if (!Array.isArray(array) || array.length === 0)
|
|
115
|
-
return undefined;
|
|
116
|
-
return array[Math.floor(Math.random() * array.length)];
|
|
117
|
-
}
|
|
118
|
-
function groupBy(array, keyOrFn) {
|
|
119
|
-
if (!Array.isArray(array) || array.length === 0)
|
|
120
|
-
return {};
|
|
121
|
-
const keyFn = typeof keyOrFn === 'function' ? keyOrFn : (item) => item[keyOrFn];
|
|
122
|
-
return array.reduce((acc, item) => {
|
|
123
|
-
const key = keyFn(item);
|
|
124
|
-
if (!acc[key])
|
|
125
|
-
acc[key] = [];
|
|
126
|
-
acc[key].push(item);
|
|
127
|
-
return acc;
|
|
128
|
-
}, {});
|
|
129
|
-
}
|
|
130
|
-
/* eslint-enable no-redeclare */
|
|
131
|
-
/**
|
|
132
|
-
* Shuffles an array using the Fisher-Yates algorithm.
|
|
133
|
-
*
|
|
134
|
-
* @template T The type of array elements.
|
|
135
|
-
* @param {T[]} array - The input array.
|
|
136
|
-
* @returns {T[]} A new shuffled array.
|
|
137
|
-
* @throws {TypeError} If array is not an array.
|
|
138
|
-
*/
|
|
139
|
-
function shuffle(array) {
|
|
140
|
-
if (!Array.isArray(array))
|
|
141
|
-
throw new TypeError('Expected an array');
|
|
142
|
-
const copy = array.slice();
|
|
143
|
-
for (let i = copy.length - 1; i > 0; i--) {
|
|
144
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
145
|
-
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
146
|
-
}
|
|
147
|
-
return copy;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Returns an array of property values from an array of objects.
|
|
151
|
-
*
|
|
152
|
-
* @template T The type of array elements.
|
|
153
|
-
* @template K The object property to pluck.
|
|
154
|
-
* @param {T[]} array - The input array.
|
|
155
|
-
* @param {K} key - The property name to pluck.
|
|
156
|
-
* @returns {T[K][]} Array of property values.
|
|
157
|
-
*/
|
|
158
|
-
function pluck(array, key) {
|
|
159
|
-
if (!Array.isArray(array))
|
|
160
|
-
return [];
|
|
161
|
-
return array.map(item => item[key]);
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Returns values in array A that are not in array B.
|
|
165
|
-
*
|
|
166
|
-
* @template T The type of array elements.
|
|
167
|
-
* @param {T[]} a - First array.
|
|
168
|
-
* @param {T[]} b - Second array.
|
|
169
|
-
* @returns {T[]} Elements in A that are not in B.
|
|
170
|
-
*/
|
|
171
|
-
function difference(a, b) {
|
|
172
|
-
if (!Array.isArray(a) || !Array.isArray(b))
|
|
173
|
-
return [];
|
|
174
|
-
const setB = new Set(b);
|
|
175
|
-
return a.filter(item => !setB.has(item));
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Returns common values between arrays A and B.
|
|
179
|
-
*
|
|
180
|
-
* @template T The type of array elements.
|
|
181
|
-
* @param {T[]} a - First array.
|
|
182
|
-
* @param {T[]} b - Second array.
|
|
183
|
-
* @returns {T[]} Elements that exist in both arrays.
|
|
184
|
-
*/
|
|
185
|
-
function intersect(a, b) {
|
|
186
|
-
if (!Array.isArray(a) || !Array.isArray(b))
|
|
187
|
-
return [];
|
|
188
|
-
const setB = new Set(b);
|
|
189
|
-
return a.filter(item => setB.has(item));
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Sorts an array of objects by a nested key using Merge Sort (O(n log n)).
|
|
193
|
-
* Missing/undefined keys are sorted to the "end" (asc) or "start" (desc).
|
|
194
|
-
*
|
|
195
|
-
* @template T The type of array elements (objects).
|
|
196
|
-
* @param {T[]} array - Array of objects to sort.
|
|
197
|
-
* @param {string | ((item: T) => any)} key - Dot-notated key (e.g., "profile.age") or function.
|
|
198
|
-
* @param {"asc" | "desc"} [direction="asc"] - Sort direction: 'asc' or 'desc'.
|
|
199
|
-
* @returns {T[]} A new sorted array.
|
|
200
|
-
* @throws {TypeError} If array is not an array.
|
|
201
|
-
*/
|
|
202
|
-
function mergeSort(array, key, direction = 'asc') {
|
|
203
|
-
if (!Array.isArray(array))
|
|
204
|
-
throw new TypeError('Expected array');
|
|
205
|
-
if (array.length <= 1)
|
|
206
|
-
return array.slice();
|
|
207
|
-
const keyFn = typeof key === 'function' ? key : (item) => (0, obj_utils_1.getValueByPath)(item, key);
|
|
208
|
-
const compare = (a, b) => {
|
|
209
|
-
const aVal = keyFn(a);
|
|
210
|
-
const bVal = keyFn(b);
|
|
211
|
-
// Sorts undefined/null last for "asc", first for "desc"
|
|
212
|
-
if (aVal === bVal)
|
|
213
|
-
return 0;
|
|
214
|
-
if (aVal == null)
|
|
215
|
-
return direction === 'asc' ? 1 : -1;
|
|
216
|
-
if (bVal == null)
|
|
217
|
-
return direction === 'asc' ? -1 : 1;
|
|
218
|
-
return direction === 'asc' ? (aVal < bVal ? -1 : 1) : aVal > bVal ? -1 : 1;
|
|
219
|
-
};
|
|
220
|
-
const merge = (left, right) => {
|
|
221
|
-
const result = [];
|
|
222
|
-
let i = 0, j = 0;
|
|
223
|
-
while (i < left.length && j < right.length) {
|
|
224
|
-
if (compare(left[i], right[j]) <= 0)
|
|
225
|
-
result.push(left[i++]);
|
|
226
|
-
else
|
|
227
|
-
result.push(right[j++]);
|
|
228
|
-
}
|
|
229
|
-
return result.concat(left.slice(i)).concat(right.slice(j));
|
|
230
|
-
};
|
|
231
|
-
const sort = (arr) => {
|
|
232
|
-
if (arr.length <= 1)
|
|
233
|
-
return arr;
|
|
234
|
-
const mid = Math.floor(arr.length / 2);
|
|
235
|
-
const left = sort(arr.slice(0, mid));
|
|
236
|
-
const right = sort(arr.slice(mid));
|
|
237
|
-
return merge(left, right);
|
|
238
|
-
};
|
|
239
|
-
return sort(array);
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Combines multiple arrays into a single array of grouped elements.
|
|
243
|
-
* Output array length equals the length of the shortest input array.
|
|
244
|
-
*
|
|
245
|
-
* @example zip([1, 2], ['a', 'b']) => [[1, 'a'], [2, 'b']]
|
|
246
|
-
* @param {...Array<T>[]} arrays - Two or more arrays to zip together.
|
|
247
|
-
* @returns {Array<T[]>} Array of grouped elements.
|
|
248
|
-
*/
|
|
249
|
-
function zip(...arrays) {
|
|
250
|
-
if (arrays.length === 0)
|
|
251
|
-
return [];
|
|
252
|
-
if (arrays.some(arr => !Array.isArray(arr))) {
|
|
253
|
-
throw new TypeError('All arguments must be arrays');
|
|
254
|
-
}
|
|
255
|
-
const minLength = Math.min(...arrays.map(arr => arr.length));
|
|
256
|
-
const result = [];
|
|
257
|
-
for (let i = 0; i < minLength; i++) {
|
|
258
|
-
result.push(arrays.map(arr => arr[i]));
|
|
259
|
-
}
|
|
260
|
-
return result;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Splits an array into two arrays based on a predicate function.
|
|
264
|
-
*
|
|
265
|
-
* @template T The type of array elements.
|
|
266
|
-
* @param {T[]} array - The input array.
|
|
267
|
-
* @param {(item: T, index: number, array: T[]) => boolean} predicate - Function to test each element.
|
|
268
|
-
* @returns {[T[], T[]]} A tuple of two arrays: [matched, unmatched].
|
|
269
|
-
*/
|
|
270
|
-
function partition(array, predicate) {
|
|
271
|
-
if (!Array.isArray(array))
|
|
272
|
-
return [[], []];
|
|
273
|
-
return array.reduce(([pass, fail], item, index) => {
|
|
274
|
-
return predicate(item, index, array) ? [[...pass, item], fail] : [pass, [...fail, item]];
|
|
275
|
-
}, [[], []]);
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Generates an array of numbers within a specified range.
|
|
279
|
-
*
|
|
280
|
-
* @param {number} start - Start of range (inclusive).
|
|
281
|
-
* @param {number} end - End of range (exclusive).
|
|
282
|
-
* @param {number} [step=1] - Step between numbers.
|
|
283
|
-
* @returns {number[]} Array of numbers in range.
|
|
284
|
-
*/
|
|
285
|
-
function range(start, end, step = 1) {
|
|
286
|
-
if (!Number.isFinite(start) || !Number.isFinite(end) || !Number.isFinite(step)) {
|
|
287
|
-
throw new TypeError('Arguments must be finite numbers');
|
|
288
|
-
}
|
|
289
|
-
if (step === 0)
|
|
290
|
-
throw new Error('Step cannot be zero');
|
|
291
|
-
const isAscending = step > 0;
|
|
292
|
-
if ((isAscending && start >= end) || (!isAscending && start <= end)) {
|
|
293
|
-
return [];
|
|
294
|
-
}
|
|
295
|
-
const length = Math.max(Math.ceil((end - start) / step), 0);
|
|
296
|
-
const result = new Array(length);
|
|
297
|
-
for (let i = 0, value = start; i < length; i++, value += step) {
|
|
298
|
-
result[i] = value;
|
|
299
|
-
}
|
|
300
|
-
return result;
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Returns the first n elements of an array.
|
|
304
|
-
*
|
|
305
|
-
* @template T The type of array elements.
|
|
306
|
-
* @param {T[]} array - The input array.
|
|
307
|
-
* @param {number} [n=1] - Number of elements to take.
|
|
308
|
-
* @returns {T[]} New array with first n elements.
|
|
309
|
-
*/
|
|
310
|
-
function take(array, n = 1) {
|
|
311
|
-
if (!Array.isArray(array) || n <= 0)
|
|
312
|
-
return [];
|
|
313
|
-
return array.slice(0, n);
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Takes elements from the array while predicate returns true.
|
|
317
|
-
*
|
|
318
|
-
* @template T The type of array elements.
|
|
319
|
-
* @param {T[]} array - The input array.
|
|
320
|
-
* @param {(item: T, index: number) => boolean} predicate - Function to test each element.
|
|
321
|
-
* @returns {T[]} New array with taken elements.
|
|
322
|
-
*/
|
|
323
|
-
function takeWhile(array, predicate) {
|
|
324
|
-
if (!Array.isArray(array))
|
|
325
|
-
return [];
|
|
326
|
-
const result = [];
|
|
327
|
-
for (let i = 0; i < array.length; i++) {
|
|
328
|
-
if (!predicate(array[i], i))
|
|
329
|
-
break;
|
|
330
|
-
result.push(array[i]);
|
|
331
|
-
}
|
|
332
|
-
return result;
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Removes all falsy values from an array.
|
|
336
|
-
* False, null, 0, "", undefined, and NaN are falsy.
|
|
337
|
-
*
|
|
338
|
-
* @template T The type of array elements.
|
|
339
|
-
* @param {T[]} array - The input array.
|
|
340
|
-
* @returns {NonNullable<T>[]} New array with falsy values removed.
|
|
341
|
-
*/
|
|
342
|
-
function compact(array) {
|
|
343
|
-
if (!Array.isArray(array))
|
|
344
|
-
return [];
|
|
345
|
-
return array.filter(Boolean);
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* Counts array elements by a key function.
|
|
349
|
-
*
|
|
350
|
-
* @template T The type of array elements.
|
|
351
|
-
* @param {T[]} array - The input array.
|
|
352
|
-
* @param {(item: T) => string | number | symbol} keyFn - Function to generate count key.
|
|
353
|
-
* @returns {Record<string, number>} Object with counts by key.
|
|
354
|
-
*/
|
|
355
|
-
function countBy(array, keyFn) {
|
|
356
|
-
if (!Array.isArray(array))
|
|
357
|
-
return {};
|
|
358
|
-
return array.reduce((acc, item) => {
|
|
359
|
-
const key = String(keyFn(item));
|
|
360
|
-
acc[key] = (acc[key] || 0) + 1;
|
|
361
|
-
return acc;
|
|
362
|
-
}, {});
|
|
363
|
-
}
|