@catbee/utils 2.0.0-next.0 → 2.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.
Files changed (122) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +102 -52
  3. package/array/index.cjs +215 -74
  4. package/array/index.d.ts +345 -2
  5. package/array/index.mjs +201 -74
  6. package/async/index.cjs +116 -39
  7. package/async/index.d.ts +292 -2
  8. package/async/index.mjs +116 -40
  9. package/cache/index.cjs +2 -2
  10. package/cache/index.d.ts +156 -2
  11. package/cache/index.mjs +3 -3
  12. package/config/index.cjs +80 -66
  13. package/config/index.d.ts +65 -3
  14. package/config/index.mjs +77 -65
  15. package/context-store/index.cjs +2 -3
  16. package/context-store/index.d.ts +193 -2
  17. package/context-store/index.mjs +2 -3
  18. package/crypto/index.cjs +55 -5
  19. package/crypto/index.d.ts +225 -2
  20. package/crypto/index.mjs +52 -7
  21. package/date/index.cjs +676 -2
  22. package/date/index.d.ts +676 -2
  23. package/date/index.mjs +665 -3
  24. package/decorator/index.cjs +2172 -0
  25. package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
  26. package/decorator/index.mjs +2131 -0
  27. package/{dir → directory}/index.cjs +5 -4
  28. package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
  29. package/{dir → directory}/index.mjs +5 -4
  30. package/env/index.cjs +100 -68
  31. package/env/index.d.ts +391 -2
  32. package/env/index.mjs +100 -68
  33. package/exception/index.cjs +1 -1
  34. package/exception/index.d.ts +233 -2
  35. package/exception/index.mjs +1 -1
  36. package/fs/index.cjs +71 -37
  37. package/fs/index.d.ts +206 -2
  38. package/fs/index.mjs +65 -35
  39. package/http-status-codes/index.cjs +1 -1
  40. package/http-status-codes/index.d.ts +268 -2
  41. package/http-status-codes/index.mjs +1 -1
  42. package/id/index.cjs +1 -1
  43. package/id/index.d.ts +38 -2
  44. package/id/index.mjs +1 -1
  45. package/index.cjs +13 -13
  46. package/index.d.ts +5 -5
  47. package/index.mjs +5 -5
  48. package/logger/index.cjs +13 -15
  49. package/logger/index.d.ts +190 -2
  50. package/logger/index.mjs +14 -16
  51. package/middleware/index.cjs +1 -1
  52. package/middleware/index.d.ts +104 -2
  53. package/middleware/index.mjs +1 -1
  54. package/object/index.cjs +379 -0
  55. package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
  56. package/object/index.mjs +360 -0
  57. package/package.json +41 -23
  58. package/performance/index.cjs +4 -4
  59. package/performance/index.d.ts +139 -2
  60. package/performance/index.mjs +4 -4
  61. package/request/index.cjs +37 -25
  62. package/request/index.d.ts +242 -3
  63. package/request/index.mjs +37 -25
  64. package/response/index.cjs +1 -1
  65. package/response/index.d.ts +319 -3
  66. package/response/index.mjs +1 -1
  67. package/server/index.cjs +249 -146
  68. package/server/index.d.ts +866 -5
  69. package/server/index.mjs +248 -144
  70. package/stream/index.cjs +1 -1
  71. package/stream/index.d.ts +91 -2
  72. package/stream/index.mjs +1 -1
  73. package/string/index.cjs +34 -1
  74. package/string/index.d.ts +146 -2
  75. package/string/index.mjs +31 -2
  76. package/type/index.cjs +19 -2
  77. package/type/index.d.ts +144 -2
  78. package/type/index.mjs +17 -3
  79. package/types/index.cjs +1 -1
  80. package/types/index.d.ts +775 -5
  81. package/types/index.mjs +1 -1
  82. package/url/index.cjs +63 -5
  83. package/url/index.d.ts +200 -2
  84. package/url/index.mjs +59 -6
  85. package/{validate → validation}/index.cjs +91 -44
  86. package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
  87. package/{validate → validation}/index.mjs +87 -44
  88. package/array/array.utils.d.ts +0 -191
  89. package/async/async.utils.d.ts +0 -296
  90. package/cache/cache.utils.d.ts +0 -176
  91. package/config/config.d.ts +0 -57
  92. package/context-store/context-store.utils.d.ts +0 -212
  93. package/crypto/crypto.utils.d.ts +0 -183
  94. package/date/date.utils.d.ts +0 -190
  95. package/decorators/index.cjs +0 -913
  96. package/decorators/index.d.ts +0 -25
  97. package/decorators/index.mjs +0 -872
  98. package/dir/index.d.ts +0 -25
  99. package/env/env.utils.d.ts +0 -400
  100. package/exception/exception.utils.d.ts +0 -253
  101. package/fs/fs.utils.d.ts +0 -196
  102. package/http-status-codes/http-status-codes.d.ts +0 -289
  103. package/id/id.utils.d.ts +0 -59
  104. package/logger/logger.utils.d.ts +0 -210
  105. package/middleware/middleware.utils.d.ts +0 -123
  106. package/obj/index.cjs +0 -317
  107. package/obj/index.d.ts +0 -25
  108. package/obj/index.mjs +0 -301
  109. package/performance/performance.utils.d.ts +0 -159
  110. package/request/request.utils.d.ts +0 -109
  111. package/response/response.utils.d.ts +0 -186
  112. package/server/server.builder.d.ts +0 -531
  113. package/server/server.d.ts +0 -303
  114. package/stream/stream.utils.d.ts +0 -111
  115. package/string/string.utils.d.ts +0 -124
  116. package/type/type.utils.d.ts +0 -129
  117. package/types/api-response.d.ts +0 -175
  118. package/types/common.d.ts +0 -148
  119. package/types/config.d.ts +0 -88
  120. package/types/server.d.ts +0 -291
  121. package/url/url.utils.d.ts +0 -164
  122. package/validate/index.d.ts +0 -25
package/array/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
4
+ * Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
5
5
  *
6
6
  * Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
  * of this software and associated documentation files (the "Software"), to deal
@@ -22,4 +22,347 @@
22
22
  * SOFTWARE.
23
23
  */
24
24
 
25
- export * from './array.utils';
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
+ 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
+ declare function unique<T>(array: readonly T[], keyFn?: (item: T) => unknown): T[];
46
+ /**
47
+ * Deeply flattens a nested array to a single-level array (iterative, stack-based).
48
+ *
49
+ * @template T The leaf type of array elements.
50
+ * @param {readonly unknown[]} array - The (possibly deeply nested) input array.
51
+ * @returns {T[]} A deeply flattened array.
52
+ */
53
+ declare function flattenDeep<T>(array: readonly unknown[]): T[];
54
+ /**
55
+ * Returns a random element from an array, or undefined if empty. Uses crypto-secure randomness
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
+ * @example
62
+ * ```ts
63
+ * securePick(['a','b','c']); // -> 'b'
64
+ * ```
65
+ */
66
+ declare function random<T>(array: readonly T[]): T | undefined;
67
+ type StrNumSym = string | number | symbol;
68
+ /**
69
+ * Groups items in an array by a nested key or key function.
70
+ *
71
+ * @template T The type of array elements.
72
+ * @overload
73
+ * @param {T[]} array - The array to group.
74
+ * @param {keyof T} key - Property key to group by.
75
+ * @returns {Record<string, readonly T[]>}
76
+ * @overload
77
+ * @param {T[]} array - The array to group.
78
+ * @param {(item: T) => StrNumSym} keyFn - Function to generate group key from item.
79
+ * @returns {Record<K, readonly T[]>}
80
+ * @param {T[]} array - The array to group.
81
+ * @param {keyof T | ((item: T) => StrNumSym)} keyOrFn - Nested property key or key selector.
82
+ * @returns {Record<StrNumSym, readonly T[]>} Grouped result object.
83
+ */
84
+ declare function groupBy<T>(array: T[], key: keyof T): Record<string, readonly T[]>;
85
+ declare function groupBy<T, K extends StrNumSym>(array: T[], keyFn: (item: T) => K): Record<K, readonly T[]>;
86
+ /**
87
+ * Shuffles an array using the Fisher-Yates algorithm. Uses crypto-secure randomness.
88
+ *
89
+ * @template T The type of array elements.
90
+ * @param {T[]} array - The input array.
91
+ * @returns {T[]} A new shuffled array.
92
+ * @throws {TypeError} If array is not an array.
93
+ */
94
+ declare function shuffle<T>(array: readonly T[]): T[];
95
+ /**
96
+ * Returns an array of property values from an array of objects. Returns undefined for missing properties.
97
+ *
98
+ * @template T The type of array elements.
99
+ * @template K The object property to pluck.
100
+ * @param {T[]} array - The input array.
101
+ * @param {K} key - The property name to pluck.
102
+ * @returns {T[K][]} Array of property values.
103
+ */
104
+ declare function pluck<T, K extends keyof T>(array: readonly T[], key: K): T[K][];
105
+ /**
106
+ * Returns values in array A that are not in array B.
107
+ *
108
+ * @template T The type of array elements.
109
+ * @param {T[]} a - First array.
110
+ * @param {T[]} b - Second array.
111
+ * @returns {T[]} Elements in A that are not in B.
112
+ */
113
+ declare function difference<T>(a: readonly T[], b: readonly T[]): T[];
114
+ /**
115
+ * Returns common values between arrays A and B.
116
+ *
117
+ * @template T The type of array elements.
118
+ * @param {T[]} a - First array.
119
+ * @param {T[]} b - Second array.
120
+ * @returns {T[]} Elements that exist in both arrays.
121
+ */
122
+ declare function intersect<T>(a: readonly T[], b: readonly T[]): T[];
123
+ /**
124
+ * Sorts an array of objects by a nested key using Merge Sort (O(n log n)).
125
+ * Missing/undefined keys are sorted to the "end" (asc) or "start" (desc").
126
+ * Optionally accepts a custom compare function or collator.
127
+ *
128
+ * @template T The type of array elements (objects).
129
+ * @param {T[]} array - Array of objects to sort.
130
+ * @param {string | ((item: T) => any)} key - Dot-notated key (e.g., "profile.age") or function.
131
+ * @param {"asc" | "desc"} [direction="asc"] - Sort direction: 'asc' or 'desc'.
132
+ * @param {(a: T, b: T) => number} [compareFn] - Optional custom compare function.
133
+ * @returns {T[]} A new sorted array.
134
+ * @throws {TypeError} If array is not an array.
135
+ */
136
+ declare function mergeSort<T>(array: readonly T[], key: string | ((item: T) => unknown), direction?: 'asc' | 'desc', compareFn?: (a: T, b: T) => number): T[];
137
+ /**
138
+ * Combines multiple arrays into a single array of grouped elements.
139
+ * Output length equals the length of the shortest input array.
140
+ *
141
+ * This implementation ensures type safety and avoids holes in output.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * zip([1, 2], ['a', 'b']) => [[1, 'a'], [2, 'b']]
146
+ * ```
147
+ * @param {...Array<T>[]} arrays - Two or more arrays to zip together.
148
+ * @returns {Array<T[]>} Array of grouped elements.
149
+ */
150
+ declare function zip<T>(...arrays: ReadonlyArray<T>[]): T[][];
151
+ /**
152
+ * Splits an array into two arrays based on a predicate function.
153
+ * Supports type-guard narrowing via overload.
154
+ *
155
+ * @template T The type of array elements.
156
+ * @overload
157
+ * @param {readonly T[]} array - The input array.
158
+ * @param {(item: T, index: number, array: readonly T[]) => item is U} predicate - Type guard predicate.
159
+ * @returns {[U[], Exclude<T, U>[]]} A tuple of two arrays: [matched, unmatched].
160
+ * @overload
161
+ * @param {readonly T[]} array - The input array.
162
+ * @param {(item: T, index: number, array: readonly T[]) => boolean} predicate - Boolean predicate.
163
+ * @returns {[T[], T[]]} A tuple of two arrays: [matched, unmatched].
164
+ */
165
+ declare function partition<T, U extends T>(array: readonly T[], predicate: (item: T, index: number, array: readonly T[]) => item is U): [U[], Exclude<T, U>[]];
166
+ declare function partition<T>(array: readonly T[], predicate: (item: T, index: number, array: readonly T[]) => boolean): [T[], T[]];
167
+ /**
168
+ * Generates an array of numbers within a specified range.
169
+ *
170
+ * @param {number} start - Start of range (inclusive).
171
+ * @param {number} end - End of range (exclusive).
172
+ * @param {number} [step=1] - Step between numbers.
173
+ * @returns {number[]} Array of numbers in range.
174
+ */
175
+ declare function range(start: number, end: number, step?: number): number[];
176
+ /**
177
+ * Returns the first `n` elements from an array.
178
+ *
179
+ * @template T The type of array elements.
180
+ * @param {T[]} array - The input array.
181
+ * @param {number} [n=1] - Number of elements to take.
182
+ * @returns {T[]} New array with first n elements.
183
+ */
184
+ declare function take<T>(array: readonly T[], n?: number): T[];
185
+ /**
186
+ * Takes elements from an array while predicate returns true.
187
+ *
188
+ * @template T The type of array elements.
189
+ * @param {readonly T[]} array - Input array.
190
+ * @param {(item: T, index: number) => boolean} predicate - Condition function.
191
+ * @returns {T[]} New array with taken elements.
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * takeWhile([1,2,3,4], (n) => n < 3); // -> [1,2]
196
+ * ```
197
+ */
198
+ declare function takeWhile<T>(array: readonly T[], predicate: (item: T, index: number) => boolean): T[];
199
+ /**
200
+ * Removes all falsy values from an array.
201
+ * `false`, `null`, `0`, `""`, `undefined`, and `NaN` are falsy.
202
+ *
203
+ * @template T The type of array elements.
204
+ * @param {T[]} array - The input array.
205
+ * @returns {NonNullable<T>[]} New array with falsy values removed.
206
+ */
207
+ declare function compact<T>(array: readonly T[]): NonNullable<T>[];
208
+ /**
209
+ * Counts array elements by a key function.
210
+ *
211
+ * @template T The type of array elements.
212
+ * @param {T[]} array - The input array.
213
+ * @param {(item: T) => StrNumSym} keyFn - Function to generate count key.
214
+ * @returns {Record<string, number>} Object with counts by key.
215
+ */
216
+ declare function countBy<T>(array: readonly T[], keyFn: (item: T) => StrNumSym): Record<string, number>;
217
+ /**
218
+ * Toggles an item in array (adds if not present, removes if present).
219
+ *
220
+ * @template T
221
+ * @param {readonly T[]} array
222
+ * @param {T} item
223
+ * @returns {T[]} New array with item toggled.
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * toggle([1,2,3], 2); // -> [1,3]
228
+ * toggle([1,3], 2); // -> [1,3,2]
229
+ * ```
230
+ */
231
+ declare function toggle<T>(array: readonly T[], item: T): T[];
232
+ /**
233
+ * Returns a cryptographically secure random index for an array.
234
+ * Used internally for secure pick/shuffle operations.
235
+ *
236
+ * @param {number} max Upper bound (exclusive).
237
+ * @returns {number} A secure random integer in range `[0, max)`.
238
+ * @throws {RangeError} If `max` is not a positive number.
239
+ *
240
+ * @example
241
+ * ```ts
242
+ * secureIndex(10); // -> 3 (unpredictable)
243
+ * ```
244
+ */
245
+ declare function secureIndex(max: number): number;
246
+ /**
247
+ * Returns a secure random element from an array using Node crypto.
248
+ *
249
+ * @template T
250
+ * @param {readonly T[]} array
251
+ * @returns {T | undefined}
252
+ */
253
+ declare const secureRandom: <T>(array: readonly T[]) => T | undefined;
254
+ /**
255
+ * Returns the last element in the array that satisfies the provided testing function.
256
+ *
257
+ * @template T
258
+ * @param {readonly T[]} array - The input array.
259
+ * @param {(item: T, index: number, array: readonly T[]) => boolean} predicate - Function to test each element.
260
+ * @returns {T | undefined} The found element, or undefined if not found.
261
+ */
262
+ declare function findLast<T>(array: readonly T[], predicate: (item: T, index: number, array: readonly T[]) => boolean): T | undefined;
263
+ /**
264
+ * Returns the index of the last element in the array that satisfies the provided testing function.
265
+ *
266
+ * @template T
267
+ * @param {readonly T[]} array - The input array.
268
+ * @param {(item: T, index: number, array: readonly T[]) => boolean} predicate - Function to test each element.
269
+ * @returns {number} The index, or -1 if not found.
270
+ */
271
+ declare function findLastIndex<T>(array: readonly T[], predicate: (item: T, index: number, array: readonly T[]) => boolean): number;
272
+ /**
273
+ * Splits an array into chunks based on a predicate function.
274
+ * Each chunk starts when predicate returns true.
275
+ *
276
+ * @template T
277
+ * @param {readonly T[]} array - The input array.
278
+ * @param {(item: T, index: number, array: readonly T[]) => boolean} predicate - Function to determine chunk boundaries.
279
+ * @returns {T[][]} Array of chunked arrays.
280
+ */
281
+ declare function chunkBy<T>(array: readonly T[], predicate: (item: T, index: number, array: readonly T[]) => boolean): T[][];
282
+ /**
283
+ * Removes all occurrences of a value from an array.
284
+ *
285
+ * @template T
286
+ * @param {readonly T[]} array - The input array.
287
+ * @param {T} value - Value to remove.
288
+ * @returns {T[]} New array with value removed.
289
+ */
290
+ declare function remove<T>(array: readonly T[], value: T): T[];
291
+ /**
292
+ * Checks if an array is sorted in ascending or descending order.
293
+ *
294
+ * @template T
295
+ * @param {readonly T[]} array - The input array.
296
+ * @param {'asc' | 'desc'} [direction='asc'] - Sort direction.
297
+ * @param {(a: T, b: T) => number} [compareFn] - Optional compare function.
298
+ * @returns {boolean} True if sorted, false otherwise.
299
+ */
300
+ declare function isSorted<T>(array: readonly T[], direction?: 'asc' | 'desc', compareFn?: (a: T, b: T) => number): boolean;
301
+ /**
302
+ * Returns the first element of an array, or undefined if empty.
303
+ *
304
+ * @template T
305
+ * @param {readonly T[]} array
306
+ * @returns {T | undefined}
307
+ */
308
+ declare function headOfArr<T>(array: readonly T[]): T | undefined;
309
+ /**
310
+ * Returns the last element of an array, or undefined if empty.
311
+ *
312
+ * @template T
313
+ * @param {readonly T[]} array
314
+ * @returns {T | undefined}
315
+ */
316
+ declare function lastOfArr<T>(array: readonly T[]): T | undefined;
317
+ /**
318
+ * Drops the first n elements from an array.
319
+ *
320
+ * @template T
321
+ * @param {readonly T[]} array - The source array.
322
+ * @param {number} n - Number of elements to drop.
323
+ * @returns {T[]} Array with first n elements removed.
324
+ *
325
+ * @example
326
+ * drop([1, 2, 3, 4, 5], 2); // [3, 4, 5]
327
+ */
328
+ declare function drop<T>(array: readonly T[], n: number): T[];
329
+ /**
330
+ * Drops elements from the start of an array while predicate returns true.
331
+ *
332
+ * @template T
333
+ * @param {readonly T[]} array - The source array.
334
+ * @param {(item: T, index: number) => boolean} predicate - Condition function.
335
+ * @returns {T[]} Array with elements dropped.
336
+ *
337
+ * @example
338
+ * dropWhile([1, 2, 3, 4, 1], x => x < 3); // [3, 4, 1]
339
+ */
340
+ declare function dropWhile<T>(array: readonly T[], predicate: (item: T, index: number) => boolean): T[];
341
+ /**
342
+ * Finds the element with the maximum value for a given key or function.
343
+ *
344
+ * @template T
345
+ * @param {readonly T[]} array - The source array.
346
+ * @param {keyof T | ((item: T) => number)} keyOrFn - Property key or function.
347
+ * @returns {T | undefined} Element with maximum value.
348
+ *
349
+ * @example
350
+ * maxBy([{a: 1}, {a: 5}, {a: 3}], 'a'); // {a: 5}
351
+ * maxBy([{a: 1}, {a: 5}, {a: 3}], x => x.a); // {a: 5}
352
+ */
353
+ declare function maxBy<T>(array: readonly T[], keyOrFn: keyof T | ((item: T) => number)): T | undefined;
354
+ /**
355
+ * Finds the element with the minimum value for a given key or function.
356
+ *
357
+ * @template T
358
+ * @param {readonly T[]} array - The source array.
359
+ * @param {keyof T | ((item: T) => number)} keyOrFn - Property key or function.
360
+ * @returns {T | undefined} Element with minimum value.
361
+ *
362
+ * @example
363
+ * minBy([{a: 1}, {a: 5}, {a: 3}], 'a'); // {a: 1}
364
+ * minBy([{a: 1}, {a: 5}, {a: 3}], x => x.a); // {a: 1}
365
+ */
366
+ declare function minBy<T>(array: readonly T[], keyOrFn: keyof T | ((item: T) => number)): T | undefined;
367
+
368
+ export { chunk, chunkBy, compact, countBy, difference, drop, dropWhile, findLast, findLastIndex, flattenDeep, groupBy, headOfArr, intersect, isSorted, lastOfArr, maxBy, mergeSort, minBy, partition, pluck, random, range, remove, secureIndex, secureRandom, shuffle, take, takeWhile, toggle, unique, zip };