@catbee/utils 1.0.5 → 2.0.0-next.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 +1 -1
- package/README.md +27 -27
- package/array/array.utils.d.ts +191 -0
- package/array/index.cjs +246 -0
- package/array/index.d.ts +25 -0
- package/array/index.mjs +228 -0
- package/async/async.utils.d.ts +296 -0
- package/async/index.cjs +428 -0
- package/async/index.d.ts +25 -0
- package/async/index.mjs +407 -0
- package/cache/cache.utils.d.ts +176 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +25 -0
- package/cache/index.mjs +290 -0
- package/config/config.d.ts +57 -0
- package/config/index.cjs +136 -0
- package/config/index.d.ts +26 -0
- package/config/index.mjs +131 -0
- package/context-store/context-store.utils.d.ts +212 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +25 -0
- package/context-store/index.mjs +261 -0
- package/crypto/crypto.utils.d.ts +183 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +25 -0
- package/crypto/index.mjs +166 -0
- package/date/date.utils.d.ts +190 -0
- package/date/index.cjs +295 -0
- package/date/index.d.ts +25 -0
- package/date/index.mjs +283 -0
- package/decorators/decorators.utils.d.ts +705 -0
- package/decorators/index.cjs +913 -0
- package/decorators/index.d.ts +25 -0
- package/decorators/index.mjs +872 -0
- package/dir/dir.utils.d.ts +216 -0
- package/dir/index.cjs +416 -0
- package/dir/index.d.ts +25 -0
- package/dir/index.mjs +389 -0
- package/env/env.utils.d.ts +400 -0
- package/env/index.cjs +761 -0
- package/env/index.d.ts +25 -0
- package/env/index.mjs +758 -0
- package/exception/exception.utils.d.ts +253 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +25 -0
- package/exception/index.mjs +338 -0
- package/fs/fs.utils.d.ts +196 -0
- package/fs/index.cjs +253 -0
- package/fs/index.d.ts +25 -0
- package/fs/index.mjs +228 -0
- package/http-status-codes/http-status-codes.d.ts +289 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +25 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/id.utils.d.ts +59 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +25 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +25 -0
- package/logger/index.mjs +313 -0
- package/logger/logger.utils.d.ts +210 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +25 -0
- package/middleware/index.mjs +170 -0
- package/middleware/middleware.utils.d.ts +123 -0
- package/obj/index.cjs +317 -0
- package/obj/index.d.ts +25 -0
- package/obj/index.mjs +301 -0
- package/obj/obj.utils.d.ts +156 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +25 -0
- package/performance/index.mjs +225 -0
- package/performance/performance.utils.d.ts +159 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +26 -0
- package/request/index.mjs +194 -0
- package/request/request.utils.d.ts +109 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +26 -0
- package/response/index.mjs +222 -0
- package/response/response.utils.d.ts +186 -0
- package/server/index.cjs +1627 -0
- package/server/index.d.ts +28 -0
- package/server/index.mjs +1617 -0
- package/server/server.builder.d.ts +531 -0
- package/server/server.d.ts +303 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +25 -0
- package/stream/index.mjs +144 -0
- package/stream/stream.utils.d.ts +111 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +25 -0
- package/string/index.mjs +95 -0
- package/string/string.utils.d.ts +124 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +25 -0
- package/type/index.mjs +119 -0
- package/type/type.utils.d.ts +129 -0
- package/types/api-response.d.ts +175 -0
- package/types/common.d.ts +148 -0
- package/types/config.d.ts +88 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +28 -0
- package/types/index.mjs +32 -0
- package/types/server.d.ts +291 -0
- package/url/index.cjs +201 -0
- package/url/index.d.ts +25 -0
- package/url/index.mjs +189 -0
- package/url/url.utils.d.ts +164 -0
- package/validate/index.cjs +212 -0
- package/validate/index.d.ts +25 -0
- package/validate/index.mjs +188 -0
- package/validate/validate.utils.d.ts +200 -0
- package/build/index.cjs +0 -7579
- package/build/index.d.ts +0 -5774
- package/build/index.mjs +0 -7274
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2025 Catbee Technologies. https://catbee.
|
|
3
|
+
Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -77,31 +77,31 @@ console.log(isEmail("user@example.com")); // true
|
|
|
77
77
|
|
|
78
78
|
| Module | Description |
|
|
79
79
|
| ------ | ----------- |
|
|
80
|
-
| [Express Server](https://catbee.
|
|
81
|
-
| [Array Utilities](https://catbee.
|
|
82
|
-
| [Async Utilities](https://catbee.
|
|
83
|
-
| [Cache Utilities](https://catbee.
|
|
84
|
-
| [Context Store](https://catbee.
|
|
85
|
-
| [Crypto Utilities](https://catbee.
|
|
86
|
-
| [Date Utilities](https://catbee.
|
|
87
|
-
| [Decorators Utilities](https://catbee.
|
|
88
|
-
| [Directory Utilities](https://catbee.
|
|
89
|
-
| [Environment Utilities](https://catbee.
|
|
90
|
-
| [Exception Utilities](https://catbee.
|
|
91
|
-
| [File System Utilities](https://catbee.
|
|
92
|
-
| [HTTP Status Codes](https://catbee.
|
|
93
|
-
| [ID Utilities](https://catbee.
|
|
94
|
-
| [Logger Utilities](https://catbee.
|
|
95
|
-
| [Middleware Utilities](https://catbee.
|
|
96
|
-
| [Object Utilities](https://catbee.
|
|
97
|
-
| [Performance Utilities](https://catbee.
|
|
98
|
-
| [Request Utilities](https://catbee.
|
|
99
|
-
| [Response Utilities](https://catbee.
|
|
100
|
-
| [Stream Utilities](https://catbee.
|
|
101
|
-
| [String Utilities](https://catbee.
|
|
102
|
-
| [Type Utilities](https://catbee.
|
|
103
|
-
| [URL Utilities](https://catbee.
|
|
104
|
-
| [Validate Utilities](https://catbee.
|
|
80
|
+
| [Express Server](https://catbee.in/docs/@catbee/utils/express-server) | Fast, secure, and scalable server setup |
|
|
81
|
+
| [Array Utilities](https://catbee.in/docs/@catbee/utils/modules/array) | Advanced array manipulation |
|
|
82
|
+
| [Async Utilities](https://catbee.in/docs/@catbee/utils/modules/async) | Promise helpers, concurrency, timing |
|
|
83
|
+
| [Cache Utilities](https://catbee.in/docs/@catbee/utils/modules/cache) | In-memory caching with TTL |
|
|
84
|
+
| [Context Store](https://catbee.in/docs/@catbee/utils/modules/context-store) | Per-request context via AsyncLocalStorage |
|
|
85
|
+
| [Crypto Utilities](https://catbee.in/docs/@catbee/utils/modules/crypto) | Hashing, encryption, tokens |
|
|
86
|
+
| [Date Utilities](https://catbee.in/docs/@catbee/utils/modules/date) | Date/time manipulation |
|
|
87
|
+
| [Decorators Utilities](https://catbee.in/docs/@catbee/utils/modules/decorators) | TypeScript decorators for Express |
|
|
88
|
+
| [Directory Utilities](https://catbee.in/docs/@catbee/utils/modules/directory) | Directory and file system helpers |
|
|
89
|
+
| [Environment Utilities](https://catbee.in/docs/@catbee/utils/modules/environment) | Env variable management |
|
|
90
|
+
| [Exception Utilities](https://catbee.in/docs/@catbee/utils/modules/exception) | HTTP and error handling |
|
|
91
|
+
| [File System Utilities](https://catbee.in/docs/@catbee/utils/modules/file-system) | File operations |
|
|
92
|
+
| [HTTP Status Codes](https://catbee.in/docs/@catbee/utils/modules/http-status-codes) | Typed status codes |
|
|
93
|
+
| [ID Utilities](https://catbee.in/docs/@catbee/utils/modules/id) | UUID and ID generation |
|
|
94
|
+
| [Logger Utilities](https://catbee.in/docs/@catbee/utils/modules/logger) | Structured logging with Pino |
|
|
95
|
+
| [Middleware Utilities](https://catbee.in/docs/@catbee/utils/modules/middleware) | Express middleware collection |
|
|
96
|
+
| [Object Utilities](https://catbee.in/docs/@catbee/utils/modules/object) | Deep merge, flatten, pick/omit, etc. |
|
|
97
|
+
| [Performance Utilities](https://catbee.in/docs/@catbee/utils/modules/performance) | Timing, memoization, memory tracking |
|
|
98
|
+
| [Request Utilities](https://catbee.in/docs/@catbee/utils/modules/request) | HTTP request parameter parsing/validation |
|
|
99
|
+
| [Response Utilities](https://catbee.in/docs/@catbee/utils/modules/response) | Standardized API response formatting |
|
|
100
|
+
| [Stream Utilities](https://catbee.in/docs/@catbee/utils/modules/stream) | Stream conversion, batching, throttling, line splitting |
|
|
101
|
+
| [String Utilities](https://catbee.in/docs/@catbee/utils/modules/string) | Casing, masking, slugifying, formatting |
|
|
102
|
+
| [Type Utilities](https://catbee.in/docs/@catbee/utils/modules/type) | Type checking, conversion, guards |
|
|
103
|
+
| [URL Utilities](https://catbee.in/docs/@catbee/utils/modules/url) | URL parsing, query manipulation, normalization |
|
|
104
|
+
| [Validate Utilities](https://catbee.in/docs/@catbee/utils/modules/validation) | Input validation functions |
|
|
105
105
|
|
|
106
106
|
---
|
|
107
107
|
|
|
@@ -117,10 +117,10 @@ import { chunk, sleep, TTLCache, getLogger } from "@catbee/utils";
|
|
|
117
117
|
|
|
118
118
|
## 📖 Documentation
|
|
119
119
|
|
|
120
|
-
- [Full API Docs & Examples](https://catbee.
|
|
120
|
+
- [Full API Docs & Examples](https://catbee.in/docs/@catbee/utils/intro)
|
|
121
121
|
|
|
122
122
|
---
|
|
123
123
|
|
|
124
124
|
## 📜 License
|
|
125
125
|
|
|
126
|
-
MIT © Catbee Technologies (see the [LICENSE](https://catbee.
|
|
126
|
+
MIT © Catbee Technologies (see the [LICENSE](https://catbee.in/license/) file for the full text)
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Splits an array into chunks of the specified size.
|
|
27
|
+
*
|
|
28
|
+
* @template T The type of array elements.
|
|
29
|
+
* @param {T[]} array - The array to split into chunks.
|
|
30
|
+
* @param {number} size - The number of elements per chunk.
|
|
31
|
+
* @returns {T[][]} A new array containing chunked arrays.
|
|
32
|
+
* @throws {TypeError} If array is not an array.
|
|
33
|
+
* @throws {Error} If chunk size is not a positive integer.
|
|
34
|
+
*/
|
|
35
|
+
export declare function chunk<T>(array: readonly T[], size: number): T[][];
|
|
36
|
+
/**
|
|
37
|
+
* Removes duplicate values from an array.
|
|
38
|
+
* Optionally enforces uniqueness by a key function.
|
|
39
|
+
*
|
|
40
|
+
* @template T The type of array elements.
|
|
41
|
+
* @param {T[]} array - The input array.
|
|
42
|
+
* @param {(item: T) => unknown} [keyFn] - Optional function to determine uniqueness by key.
|
|
43
|
+
* @returns {T[]} A new array with unique values.
|
|
44
|
+
*/
|
|
45
|
+
export declare function unique<T>(array: readonly T[], keyFn?: (item: T) => unknown): T[];
|
|
46
|
+
/**
|
|
47
|
+
* Deeply flattens a nested array to a single-level array.
|
|
48
|
+
*
|
|
49
|
+
* @template T The leaf type of array elements.
|
|
50
|
+
* @param {any[]} array - The (possibly deeply nested) input array.
|
|
51
|
+
* @returns {T[]} A deeply flattened array.
|
|
52
|
+
*/
|
|
53
|
+
export declare function flattenDeep<T>(array: unknown[]): T[];
|
|
54
|
+
/**
|
|
55
|
+
* Returns a random element from an array, or undefined if empty.
|
|
56
|
+
*
|
|
57
|
+
* @template T The type of array elements.
|
|
58
|
+
* @param {T[]} array - The input array.
|
|
59
|
+
* @returns {T | undefined} A randomly selected item, or undefined if array is empty or not an array.
|
|
60
|
+
*/
|
|
61
|
+
export declare function random<T>(array: readonly T[]): T | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Groups items in an array by a key or key function.
|
|
64
|
+
*
|
|
65
|
+
* @template T The type of array elements.
|
|
66
|
+
* @overload
|
|
67
|
+
* @param {T[]} array - The array to group.
|
|
68
|
+
* @param {keyof T} key - Property key to group by.
|
|
69
|
+
* @returns {Record<string, T[]>}
|
|
70
|
+
* @overload
|
|
71
|
+
* @param {T[]} array - The array to group.
|
|
72
|
+
* @param {(item: T) => string | number | symbol} keyFn - Function to generate group key from item.
|
|
73
|
+
* @returns {Record<K, T[]>}
|
|
74
|
+
* @param {T[]} array - The array to group.
|
|
75
|
+
* @param {keyof T | ((item: T) => string | number | symbol)} keyOrFn - Property key or key-generating function.
|
|
76
|
+
* @returns {Record<string | number | symbol, T[]>} An object mapping group keys to item arrays.
|
|
77
|
+
*/
|
|
78
|
+
export declare function groupBy<T>(array: T[], key: keyof T): Record<string, T[]>;
|
|
79
|
+
export declare function groupBy<T, K extends string | number | symbol>(array: T[], keyFn: (item: T) => K): Record<K, T[]>;
|
|
80
|
+
/**
|
|
81
|
+
* Shuffles an array using the Fisher-Yates algorithm.
|
|
82
|
+
*
|
|
83
|
+
* @template T The type of array elements.
|
|
84
|
+
* @param {T[]} array - The input array.
|
|
85
|
+
* @returns {T[]} A new shuffled array.
|
|
86
|
+
* @throws {TypeError} If array is not an array.
|
|
87
|
+
*/
|
|
88
|
+
export declare function shuffle<T>(array: T[]): T[];
|
|
89
|
+
/**
|
|
90
|
+
* Returns an array of property values from an array of objects.
|
|
91
|
+
*
|
|
92
|
+
* @template T The type of array elements.
|
|
93
|
+
* @template K The object property to pluck.
|
|
94
|
+
* @param {T[]} array - The input array.
|
|
95
|
+
* @param {K} key - The property name to pluck.
|
|
96
|
+
* @returns {T[K][]} Array of property values.
|
|
97
|
+
*/
|
|
98
|
+
export declare function pluck<T, K extends keyof T>(array: readonly T[], key: K): T[K][];
|
|
99
|
+
/**
|
|
100
|
+
* Returns values in array A that are not in array B.
|
|
101
|
+
*
|
|
102
|
+
* @template T The type of array elements.
|
|
103
|
+
* @param {T[]} a - First array.
|
|
104
|
+
* @param {T[]} b - Second array.
|
|
105
|
+
* @returns {T[]} Elements in A that are not in B.
|
|
106
|
+
*/
|
|
107
|
+
export declare function difference<T>(a: readonly T[], b: readonly T[]): T[];
|
|
108
|
+
/**
|
|
109
|
+
* Returns common values between arrays A and B.
|
|
110
|
+
*
|
|
111
|
+
* @template T The type of array elements.
|
|
112
|
+
* @param {T[]} a - First array.
|
|
113
|
+
* @param {T[]} b - Second array.
|
|
114
|
+
* @returns {T[]} Elements that exist in both arrays.
|
|
115
|
+
*/
|
|
116
|
+
export declare function intersect<T>(a: readonly T[], b: readonly T[]): T[];
|
|
117
|
+
/**
|
|
118
|
+
* Sorts an array of objects by a nested key using Merge Sort (O(n log n)).
|
|
119
|
+
* Missing/undefined keys are sorted to the "end" (asc) or "start" (desc).
|
|
120
|
+
*
|
|
121
|
+
* @template T The type of array elements (objects).
|
|
122
|
+
* @param {T[]} array - Array of objects to sort.
|
|
123
|
+
* @param {string | ((item: T) => any)} key - Dot-notated key (e.g., "profile.age") or function.
|
|
124
|
+
* @param {"asc" | "desc"} [direction="asc"] - Sort direction: 'asc' or 'desc'.
|
|
125
|
+
* @returns {T[]} A new sorted array.
|
|
126
|
+
* @throws {TypeError} If array is not an array.
|
|
127
|
+
*/
|
|
128
|
+
export declare function mergeSort<T>(array: T[], key: string | ((item: T) => any), direction?: 'asc' | 'desc'): T[];
|
|
129
|
+
/**
|
|
130
|
+
* Combines multiple arrays into a single array of grouped elements.
|
|
131
|
+
* Output array length equals the length of the shortest input array.
|
|
132
|
+
*
|
|
133
|
+
* @example zip([1, 2], ['a', 'b']) => [[1, 'a'], [2, 'b']]
|
|
134
|
+
* @param {...Array<T>[]} arrays - Two or more arrays to zip together.
|
|
135
|
+
* @returns {Array<T[]>} Array of grouped elements.
|
|
136
|
+
*/
|
|
137
|
+
export declare function zip<T>(...arrays: ReadonlyArray<T>[]): T[][];
|
|
138
|
+
/**
|
|
139
|
+
* Splits an array into two arrays based on a predicate function.
|
|
140
|
+
*
|
|
141
|
+
* @template T The type of array elements.
|
|
142
|
+
* @param {T[]} array - The input array.
|
|
143
|
+
* @param {(item: T, index: number, array: T[]) => boolean} predicate - Function to test each element.
|
|
144
|
+
* @returns {[T[], T[]]} A tuple of two arrays: [matched, unmatched].
|
|
145
|
+
*/
|
|
146
|
+
export declare function partition<T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): [T[], T[]];
|
|
147
|
+
/**
|
|
148
|
+
* Generates an array of numbers within a specified range.
|
|
149
|
+
*
|
|
150
|
+
* @param {number} start - Start of range (inclusive).
|
|
151
|
+
* @param {number} end - End of range (exclusive).
|
|
152
|
+
* @param {number} [step=1] - Step between numbers.
|
|
153
|
+
* @returns {number[]} Array of numbers in range.
|
|
154
|
+
*/
|
|
155
|
+
export declare function range(start: number, end: number, step?: number): number[];
|
|
156
|
+
/**
|
|
157
|
+
* Returns the first n elements of an array.
|
|
158
|
+
*
|
|
159
|
+
* @template T The type of array elements.
|
|
160
|
+
* @param {T[]} array - The input array.
|
|
161
|
+
* @param {number} [n=1] - Number of elements to take.
|
|
162
|
+
* @returns {T[]} New array with first n elements.
|
|
163
|
+
*/
|
|
164
|
+
export declare function take<T>(array: T[], n?: number): T[];
|
|
165
|
+
/**
|
|
166
|
+
* Takes elements from the array while predicate returns true.
|
|
167
|
+
*
|
|
168
|
+
* @template T The type of array elements.
|
|
169
|
+
* @param {T[]} array - The input array.
|
|
170
|
+
* @param {(item: T, index: number) => boolean} predicate - Function to test each element.
|
|
171
|
+
* @returns {T[]} New array with taken elements.
|
|
172
|
+
*/
|
|
173
|
+
export declare function takeWhile<T>(array: T[], predicate: (item: T, index: number) => boolean): T[];
|
|
174
|
+
/**
|
|
175
|
+
* Removes all falsy values from an array.
|
|
176
|
+
* False, null, 0, "", undefined, and NaN are falsy.
|
|
177
|
+
*
|
|
178
|
+
* @template T The type of array elements.
|
|
179
|
+
* @param {T[]} array - The input array.
|
|
180
|
+
* @returns {NonNullable<T>[]} New array with falsy values removed.
|
|
181
|
+
*/
|
|
182
|
+
export declare function compact<T>(array: T[]): NonNullable<T>[];
|
|
183
|
+
/**
|
|
184
|
+
* Counts array elements by a key function.
|
|
185
|
+
*
|
|
186
|
+
* @template T The type of array elements.
|
|
187
|
+
* @param {T[]} array - The input array.
|
|
188
|
+
* @param {(item: T) => string | number | symbol} keyFn - Function to generate count key.
|
|
189
|
+
* @returns {Record<string, number>} Object with counts by key.
|
|
190
|
+
*/
|
|
191
|
+
export declare function countBy<T>(array: T[], keyFn: (item: T) => string | number | symbol): Record<string, number>;
|
package/array/index.cjs
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var obj = require('@catbee/utils/obj');
|
|
28
|
+
|
|
29
|
+
var __defProp = Object.defineProperty;
|
|
30
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
31
|
+
function chunk(array, size) {
|
|
32
|
+
if (!Array.isArray(array)) throw new TypeError("Expected an array");
|
|
33
|
+
if (array.length === 0) return [];
|
|
34
|
+
if (!Number.isInteger(size) || size <= 0) throw new Error("Chunk size must be a positive integer");
|
|
35
|
+
const result = [];
|
|
36
|
+
for (let i = 0; i < array.length; i += size) {
|
|
37
|
+
result.push(array.slice(i, i + size));
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
__name(chunk, "chunk");
|
|
42
|
+
function unique(array, keyFn) {
|
|
43
|
+
if (!Array.isArray(array) || array.length === 0) return [];
|
|
44
|
+
if (!keyFn) return Array.from(new Set(array));
|
|
45
|
+
const seen = /* @__PURE__ */ new Set();
|
|
46
|
+
return array.filter((item) => {
|
|
47
|
+
const key = keyFn(item);
|
|
48
|
+
if (seen.has(key)) return false;
|
|
49
|
+
seen.add(key);
|
|
50
|
+
return true;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
__name(unique, "unique");
|
|
54
|
+
function flattenDeep(array) {
|
|
55
|
+
if (!Array.isArray(array)) return [];
|
|
56
|
+
const result = [];
|
|
57
|
+
for (const val of array) {
|
|
58
|
+
if (Array.isArray(val)) {
|
|
59
|
+
result.push(...flattenDeep(val));
|
|
60
|
+
} else {
|
|
61
|
+
result.push(val);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
__name(flattenDeep, "flattenDeep");
|
|
67
|
+
function random(array) {
|
|
68
|
+
if (!Array.isArray(array) || array.length === 0) return void 0;
|
|
69
|
+
const idx = Math.floor(Math.random() * array.length);
|
|
70
|
+
return array[idx];
|
|
71
|
+
}
|
|
72
|
+
__name(random, "random");
|
|
73
|
+
function groupBy(array, keyOrFn) {
|
|
74
|
+
if (!Array.isArray(array) || array.length === 0) return {};
|
|
75
|
+
const keyFn = typeof keyOrFn === "function" ? keyOrFn : (item) => item[keyOrFn];
|
|
76
|
+
return array.reduce((acc, item) => {
|
|
77
|
+
const key = keyFn(item);
|
|
78
|
+
if (!acc[key]) acc[key] = [];
|
|
79
|
+
acc[key].push(item);
|
|
80
|
+
return acc;
|
|
81
|
+
}, {});
|
|
82
|
+
}
|
|
83
|
+
__name(groupBy, "groupBy");
|
|
84
|
+
function shuffle(array) {
|
|
85
|
+
if (!Array.isArray(array)) throw new TypeError("Expected an array");
|
|
86
|
+
const copy = array.slice();
|
|
87
|
+
for (let i = copy.length - 1; i > 0; i--) {
|
|
88
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
89
|
+
[copy[i], copy[j]] = [
|
|
90
|
+
copy[j],
|
|
91
|
+
copy[i]
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
return copy;
|
|
95
|
+
}
|
|
96
|
+
__name(shuffle, "shuffle");
|
|
97
|
+
function pluck(array, key) {
|
|
98
|
+
if (!Array.isArray(array)) return [];
|
|
99
|
+
return array.map((item) => item[key]);
|
|
100
|
+
}
|
|
101
|
+
__name(pluck, "pluck");
|
|
102
|
+
function difference(a, b) {
|
|
103
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return [];
|
|
104
|
+
const setB = new Set(b);
|
|
105
|
+
return a.filter((item) => !setB.has(item));
|
|
106
|
+
}
|
|
107
|
+
__name(difference, "difference");
|
|
108
|
+
function intersect(a, b) {
|
|
109
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return [];
|
|
110
|
+
const setB = new Set(b);
|
|
111
|
+
return a.filter((item) => setB.has(item));
|
|
112
|
+
}
|
|
113
|
+
__name(intersect, "intersect");
|
|
114
|
+
function mergeSort(array, key, direction = "asc") {
|
|
115
|
+
if (!Array.isArray(array)) throw new TypeError("Expected array");
|
|
116
|
+
if (array.length <= 1) return array.slice();
|
|
117
|
+
const keyFn = typeof key === "function" ? key : (item) => obj.getValueByPath(item, key);
|
|
118
|
+
const compare = /* @__PURE__ */ __name((a, b) => {
|
|
119
|
+
const aVal = keyFn(a);
|
|
120
|
+
const bVal = keyFn(b);
|
|
121
|
+
if (aVal === bVal) return 0;
|
|
122
|
+
if (aVal == null) return direction === "asc" ? 1 : -1;
|
|
123
|
+
if (bVal == null) return direction === "asc" ? -1 : 1;
|
|
124
|
+
return direction === "asc" ? aVal < bVal ? -1 : 1 : aVal > bVal ? -1 : 1;
|
|
125
|
+
}, "compare");
|
|
126
|
+
const merge = /* @__PURE__ */ __name((left, right) => {
|
|
127
|
+
const result = [];
|
|
128
|
+
let i = 0, j = 0;
|
|
129
|
+
while (i < left.length && j < right.length) {
|
|
130
|
+
if (compare(left[i], right[j]) <= 0) result.push(left[i++]);
|
|
131
|
+
else result.push(right[j++]);
|
|
132
|
+
}
|
|
133
|
+
return result.concat(left.slice(i)).concat(right.slice(j));
|
|
134
|
+
}, "merge");
|
|
135
|
+
const sort = /* @__PURE__ */ __name((arr) => {
|
|
136
|
+
if (arr.length <= 1) return arr;
|
|
137
|
+
const mid = Math.floor(arr.length / 2);
|
|
138
|
+
const left = sort(arr.slice(0, mid));
|
|
139
|
+
const right = sort(arr.slice(mid));
|
|
140
|
+
return merge(left, right);
|
|
141
|
+
}, "sort");
|
|
142
|
+
return sort(array);
|
|
143
|
+
}
|
|
144
|
+
__name(mergeSort, "mergeSort");
|
|
145
|
+
function zip(...arrays) {
|
|
146
|
+
if (arrays.length === 0) return [];
|
|
147
|
+
if (arrays.some((arr) => !Array.isArray(arr))) {
|
|
148
|
+
throw new TypeError("All arguments must be arrays");
|
|
149
|
+
}
|
|
150
|
+
const minLength = Math.min(...arrays.map((arr) => arr.length));
|
|
151
|
+
const result = [];
|
|
152
|
+
for (let i = 0; i < minLength; i++) {
|
|
153
|
+
result.push(arrays.map((arr) => arr[i]));
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
__name(zip, "zip");
|
|
158
|
+
function partition(array, predicate) {
|
|
159
|
+
if (!Array.isArray(array)) return [
|
|
160
|
+
[],
|
|
161
|
+
[]
|
|
162
|
+
];
|
|
163
|
+
return array.reduce(([pass, fail], item, index) => {
|
|
164
|
+
return predicate(item, index, array) ? [
|
|
165
|
+
[
|
|
166
|
+
...pass,
|
|
167
|
+
item
|
|
168
|
+
],
|
|
169
|
+
fail
|
|
170
|
+
] : [
|
|
171
|
+
pass,
|
|
172
|
+
[
|
|
173
|
+
...fail,
|
|
174
|
+
item
|
|
175
|
+
]
|
|
176
|
+
];
|
|
177
|
+
}, [
|
|
178
|
+
[],
|
|
179
|
+
[]
|
|
180
|
+
]);
|
|
181
|
+
}
|
|
182
|
+
__name(partition, "partition");
|
|
183
|
+
function range(start, end, step = 1) {
|
|
184
|
+
if (!Number.isFinite(start) || !Number.isFinite(end) || !Number.isFinite(step)) {
|
|
185
|
+
throw new TypeError("Arguments must be finite numbers");
|
|
186
|
+
}
|
|
187
|
+
if (step === 0) throw new Error("Step cannot be zero");
|
|
188
|
+
const isAscending = step > 0;
|
|
189
|
+
if (isAscending && start >= end || !isAscending && start <= end) {
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
const length = Math.max(Math.ceil((end - start) / step), 0);
|
|
193
|
+
const result = new Array(length);
|
|
194
|
+
for (let i = 0, value = start; i < length; i++, value += step) {
|
|
195
|
+
result[i] = value;
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
__name(range, "range");
|
|
200
|
+
function take(array, n = 1) {
|
|
201
|
+
if (!Array.isArray(array) || n <= 0) return [];
|
|
202
|
+
return array.slice(0, n);
|
|
203
|
+
}
|
|
204
|
+
__name(take, "take");
|
|
205
|
+
function takeWhile(array, predicate) {
|
|
206
|
+
if (!Array.isArray(array)) return [];
|
|
207
|
+
const result = [];
|
|
208
|
+
for (let i = 0; i < array.length; i++) {
|
|
209
|
+
if (!predicate(array[i], i)) break;
|
|
210
|
+
result.push(array[i]);
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
__name(takeWhile, "takeWhile");
|
|
215
|
+
function compact(array) {
|
|
216
|
+
if (!Array.isArray(array)) return [];
|
|
217
|
+
return array.filter(Boolean);
|
|
218
|
+
}
|
|
219
|
+
__name(compact, "compact");
|
|
220
|
+
function countBy(array, keyFn) {
|
|
221
|
+
if (!Array.isArray(array)) return {};
|
|
222
|
+
return array.reduce((acc, item) => {
|
|
223
|
+
const key = String(keyFn(item));
|
|
224
|
+
acc[key] = (acc[key] || 0) + 1;
|
|
225
|
+
return acc;
|
|
226
|
+
}, {});
|
|
227
|
+
}
|
|
228
|
+
__name(countBy, "countBy");
|
|
229
|
+
|
|
230
|
+
exports.chunk = chunk;
|
|
231
|
+
exports.compact = compact;
|
|
232
|
+
exports.countBy = countBy;
|
|
233
|
+
exports.difference = difference;
|
|
234
|
+
exports.flattenDeep = flattenDeep;
|
|
235
|
+
exports.groupBy = groupBy;
|
|
236
|
+
exports.intersect = intersect;
|
|
237
|
+
exports.mergeSort = mergeSort;
|
|
238
|
+
exports.partition = partition;
|
|
239
|
+
exports.pluck = pluck;
|
|
240
|
+
exports.random = random;
|
|
241
|
+
exports.range = range;
|
|
242
|
+
exports.shuffle = shuffle;
|
|
243
|
+
exports.take = take;
|
|
244
|
+
exports.takeWhile = takeWhile;
|
|
245
|
+
exports.unique = unique;
|
|
246
|
+
exports.zip = zip;
|
package/array/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export * from './array.utils';
|