@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.
- package/LICENSE +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/object/index.mjs
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2026 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
|
+
var __defProp = Object.defineProperty;
|
|
26
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
27
|
+
|
|
28
|
+
// src/object/object.utils.ts
|
|
29
|
+
function isObject(value) {
|
|
30
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
31
|
+
}
|
|
32
|
+
__name(isObject, "isObject");
|
|
33
|
+
function isObjEmpty(obj) {
|
|
34
|
+
return isObject(obj) && Object.keys(obj).length === 0;
|
|
35
|
+
}
|
|
36
|
+
__name(isObjEmpty, "isObjEmpty");
|
|
37
|
+
function pick(obj, keys) {
|
|
38
|
+
return Object.fromEntries(keys.map((key) => [
|
|
39
|
+
key,
|
|
40
|
+
obj[key]
|
|
41
|
+
]));
|
|
42
|
+
}
|
|
43
|
+
__name(pick, "pick");
|
|
44
|
+
function omit(obj, keys) {
|
|
45
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key)));
|
|
46
|
+
}
|
|
47
|
+
__name(omit, "omit");
|
|
48
|
+
function deepObjMerge(target, ...sources) {
|
|
49
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
50
|
+
for (const s of sources) {
|
|
51
|
+
target = mergeInto(target, s, seen);
|
|
52
|
+
}
|
|
53
|
+
return target;
|
|
54
|
+
}
|
|
55
|
+
__name(deepObjMerge, "deepObjMerge");
|
|
56
|
+
function mergeInto(t, s, seen) {
|
|
57
|
+
if (s === void 0) return t;
|
|
58
|
+
if (typeof s === "function" || typeof s === "symbol") return s;
|
|
59
|
+
if (Array.isArray(s)) {
|
|
60
|
+
const cloned = deepClone(s);
|
|
61
|
+
seen.set(s, cloned);
|
|
62
|
+
return cloned;
|
|
63
|
+
}
|
|
64
|
+
if (s && typeof s === "object") {
|
|
65
|
+
if (seen.has(s)) return seen.get(s);
|
|
66
|
+
if (s instanceof Date || s instanceof Map || s instanceof Set || s instanceof RegExp || s instanceof ArrayBuffer || ArrayBuffer.isView(s)) {
|
|
67
|
+
const cloned = deepClone(s);
|
|
68
|
+
seen.set(s, cloned);
|
|
69
|
+
return cloned;
|
|
70
|
+
}
|
|
71
|
+
if (!t || typeof t !== "object") {
|
|
72
|
+
t = Object.create(Object.getPrototypeOf(s));
|
|
73
|
+
}
|
|
74
|
+
seen.set(s, t);
|
|
75
|
+
for (const key of Reflect.ownKeys(s)) {
|
|
76
|
+
if (key === "__proto__") continue;
|
|
77
|
+
const sv = s[key];
|
|
78
|
+
if (sv === void 0) continue;
|
|
79
|
+
t[key] = mergeInto(t[key], sv, seen);
|
|
80
|
+
}
|
|
81
|
+
return t;
|
|
82
|
+
}
|
|
83
|
+
return s;
|
|
84
|
+
}
|
|
85
|
+
__name(mergeInto, "mergeInto");
|
|
86
|
+
function isPlainObject(value) {
|
|
87
|
+
if (typeof value !== "object" || value === null) return false;
|
|
88
|
+
if (Array.isArray(value)) return false;
|
|
89
|
+
const proto = Object.getPrototypeOf(value);
|
|
90
|
+
return proto === Object.prototype || proto === null;
|
|
91
|
+
}
|
|
92
|
+
__name(isPlainObject, "isPlainObject");
|
|
93
|
+
function flattenObject(obj, prefix = "") {
|
|
94
|
+
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
95
|
+
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
96
|
+
if (key === "__proto__") return acc;
|
|
97
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
98
|
+
Object.assign(acc, flattenObject(value, newKey));
|
|
99
|
+
} else {
|
|
100
|
+
acc[newKey] = value;
|
|
101
|
+
}
|
|
102
|
+
return acc;
|
|
103
|
+
}, {});
|
|
104
|
+
}
|
|
105
|
+
__name(flattenObject, "flattenObject");
|
|
106
|
+
function getValueByPath(obj, path) {
|
|
107
|
+
if (!obj || typeof obj !== "object") return void 0;
|
|
108
|
+
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
109
|
+
return parts.reduce((acc, key) => acc?.[key], obj);
|
|
110
|
+
}
|
|
111
|
+
__name(getValueByPath, "getValueByPath");
|
|
112
|
+
function ensureNextContainer(curr, nextIsIndex) {
|
|
113
|
+
if (curr === void 0 || typeof curr !== "object" || curr === null) {
|
|
114
|
+
return nextIsIndex ? [] : /* @__PURE__ */ Object.create(null);
|
|
115
|
+
}
|
|
116
|
+
return curr;
|
|
117
|
+
}
|
|
118
|
+
__name(ensureNextContainer, "ensureNextContainer");
|
|
119
|
+
function setValueByPath(obj, path, value) {
|
|
120
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
121
|
+
const clone = deepClone(obj);
|
|
122
|
+
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
123
|
+
if (parts.length === 0) return clone;
|
|
124
|
+
const forbiddenKeys = /* @__PURE__ */ new Set([
|
|
125
|
+
"__proto__",
|
|
126
|
+
"prototype",
|
|
127
|
+
"constructor"
|
|
128
|
+
]);
|
|
129
|
+
let current = clone;
|
|
130
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
131
|
+
const key = parts[i];
|
|
132
|
+
if (forbiddenKeys.has(key)) return clone;
|
|
133
|
+
const nextKey = parts[i + 1];
|
|
134
|
+
const nextIsIndex = !Number.isNaN(Number(nextKey));
|
|
135
|
+
current[key] = ensureNextContainer(current[key], nextIsIndex);
|
|
136
|
+
current = current[key];
|
|
137
|
+
}
|
|
138
|
+
const finalKey = parts.at(-1);
|
|
139
|
+
if (forbiddenKeys.has(finalKey)) return clone;
|
|
140
|
+
current[finalKey] = value;
|
|
141
|
+
return clone;
|
|
142
|
+
}
|
|
143
|
+
__name(setValueByPath, "setValueByPath");
|
|
144
|
+
function isEqual(a, b) {
|
|
145
|
+
if (a === b) return true;
|
|
146
|
+
if (a == null || b == null || typeof a !== "object" || typeof b !== "object") return false;
|
|
147
|
+
if (a instanceof Date && b instanceof Date) return equalDates(a, b);
|
|
148
|
+
if (a instanceof RegExp && b instanceof RegExp) return equalRegExp(a, b);
|
|
149
|
+
if (a instanceof Set && b instanceof Set) return equalSet(a, b);
|
|
150
|
+
if (a instanceof Map && b instanceof Map) return equalMap(a, b);
|
|
151
|
+
if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) return equalArrayBuffer(a, b);
|
|
152
|
+
if (Array.isArray(a) && Array.isArray(b)) return equalArray(a, b);
|
|
153
|
+
return equalPlainObjects(a, b);
|
|
154
|
+
}
|
|
155
|
+
__name(isEqual, "isEqual");
|
|
156
|
+
function equalDates(a, b) {
|
|
157
|
+
return a.getTime() === b.getTime();
|
|
158
|
+
}
|
|
159
|
+
__name(equalDates, "equalDates");
|
|
160
|
+
function equalRegExp(a, b) {
|
|
161
|
+
return a.source === b.source && a.flags === b.flags;
|
|
162
|
+
}
|
|
163
|
+
__name(equalRegExp, "equalRegExp");
|
|
164
|
+
function equalSet(a, b) {
|
|
165
|
+
if (a.size !== b.size) return false;
|
|
166
|
+
for (const v of a) if (!b.has(v)) return false;
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
__name(equalSet, "equalSet");
|
|
170
|
+
function equalMap(a, b) {
|
|
171
|
+
if (a.size !== b.size) return false;
|
|
172
|
+
for (const [k, v] of a) {
|
|
173
|
+
if (!b.has(k)) return false;
|
|
174
|
+
if (!isEqual(v, b.get(k))) return false;
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
__name(equalMap, "equalMap");
|
|
179
|
+
function equalArrayBuffer(a, b) {
|
|
180
|
+
return isEqual(new Uint8Array(a), new Uint8Array(b));
|
|
181
|
+
}
|
|
182
|
+
__name(equalArrayBuffer, "equalArrayBuffer");
|
|
183
|
+
function equalArray(a, b) {
|
|
184
|
+
if (a.length !== b.length) return false;
|
|
185
|
+
for (let i = 0; i < a.length; i++) if (!isEqual(a[i], b[i])) return false;
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
__name(equalArray, "equalArray");
|
|
189
|
+
function equalPlainObjects(a, b) {
|
|
190
|
+
const aKeys = Object.keys(a);
|
|
191
|
+
const bKeys = Object.keys(b);
|
|
192
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
193
|
+
for (const key of aKeys) {
|
|
194
|
+
if (!Object.hasOwn(b, key)) return false;
|
|
195
|
+
if (!isEqual(a[key], b[key])) return false;
|
|
196
|
+
}
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
__name(equalPlainObjects, "equalPlainObjects");
|
|
200
|
+
function filterObject(obj, predicate) {
|
|
201
|
+
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(value, key, obj)));
|
|
202
|
+
}
|
|
203
|
+
__name(filterObject, "filterObject");
|
|
204
|
+
function mapObject(obj, mapFn) {
|
|
205
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
206
|
+
key,
|
|
207
|
+
mapFn(value, key, obj)
|
|
208
|
+
]));
|
|
209
|
+
}
|
|
210
|
+
__name(mapObject, "mapObject");
|
|
211
|
+
function _deepClone(value, seen) {
|
|
212
|
+
if (typeof value === "function" || typeof value === "symbol") return value;
|
|
213
|
+
if (value === null || typeof value !== "object") return value;
|
|
214
|
+
if (seen.has(value)) return seen.get(value);
|
|
215
|
+
if (Array.isArray(value)) return cloneArray(value, seen);
|
|
216
|
+
if (value instanceof Date) return cloneDate(value);
|
|
217
|
+
if (value instanceof Map) return cloneMap(value, seen);
|
|
218
|
+
if (value instanceof Set) return cloneSet(value, seen);
|
|
219
|
+
if (value instanceof RegExp) return cloneRegExp(value);
|
|
220
|
+
if (ArrayBuffer.isView(value)) return cloneArrayBufferView(value, seen);
|
|
221
|
+
if (value instanceof ArrayBuffer) return cloneArrayBuffer(value, seen);
|
|
222
|
+
return cloneObject(value, seen);
|
|
223
|
+
}
|
|
224
|
+
__name(_deepClone, "_deepClone");
|
|
225
|
+
function cloneArray(arr, seen) {
|
|
226
|
+
const result = [];
|
|
227
|
+
seen.set(arr, result);
|
|
228
|
+
for (const item of arr) result.push(_deepClone(item, seen));
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
__name(cloneArray, "cloneArray");
|
|
232
|
+
function cloneDate(d) {
|
|
233
|
+
const r = new Date(d.getTime());
|
|
234
|
+
return r;
|
|
235
|
+
}
|
|
236
|
+
__name(cloneDate, "cloneDate");
|
|
237
|
+
function cloneMap(m, seen) {
|
|
238
|
+
const result = /* @__PURE__ */ new Map();
|
|
239
|
+
seen.set(m, result);
|
|
240
|
+
for (const [k, v] of m) result.set(_deepClone(k, seen), _deepClone(v, seen));
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
__name(cloneMap, "cloneMap");
|
|
244
|
+
function cloneSet(s, seen) {
|
|
245
|
+
const result = /* @__PURE__ */ new Set();
|
|
246
|
+
seen.set(s, result);
|
|
247
|
+
for (const v of s) result.add(_deepClone(v, seen));
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
__name(cloneSet, "cloneSet");
|
|
251
|
+
function cloneRegExp(r) {
|
|
252
|
+
const result = new RegExp(r.source, r.flags);
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
__name(cloneRegExp, "cloneRegExp");
|
|
256
|
+
function cloneArrayBufferView(view, seen) {
|
|
257
|
+
if (view instanceof DataView) {
|
|
258
|
+
const buf2 = _deepClone(view.buffer, seen);
|
|
259
|
+
const clone2 = new DataView(buf2, view.byteOffset, view.byteLength);
|
|
260
|
+
seen.set(view, clone2);
|
|
261
|
+
return clone2;
|
|
262
|
+
}
|
|
263
|
+
const typed = view;
|
|
264
|
+
const buf = _deepClone(typed.buffer, seen);
|
|
265
|
+
const clone = new typed.constructor(buf, typed.byteOffset, typed.length);
|
|
266
|
+
seen.set(view, clone);
|
|
267
|
+
return clone;
|
|
268
|
+
}
|
|
269
|
+
__name(cloneArrayBufferView, "cloneArrayBufferView");
|
|
270
|
+
function cloneArrayBuffer(buf, seen) {
|
|
271
|
+
const result = buf.slice(0);
|
|
272
|
+
seen.set(buf, result);
|
|
273
|
+
return result;
|
|
274
|
+
}
|
|
275
|
+
__name(cloneArrayBuffer, "cloneArrayBuffer");
|
|
276
|
+
function cloneObject(obj, seen) {
|
|
277
|
+
const result = Object.create(Object.getPrototypeOf(obj));
|
|
278
|
+
seen.set(obj, result);
|
|
279
|
+
for (const key of Reflect.ownKeys(obj)) {
|
|
280
|
+
const desc = Object.getOwnPropertyDescriptor(obj, key);
|
|
281
|
+
if (desc?.get || desc?.set) {
|
|
282
|
+
Object.defineProperty(result, key, {
|
|
283
|
+
get: desc.get,
|
|
284
|
+
set: desc.set,
|
|
285
|
+
enumerable: desc.enumerable,
|
|
286
|
+
configurable: desc.configurable
|
|
287
|
+
});
|
|
288
|
+
} else {
|
|
289
|
+
Object.defineProperty(result, key, {
|
|
290
|
+
value: _deepClone(obj[key], seen),
|
|
291
|
+
writable: desc?.writable ?? true,
|
|
292
|
+
enumerable: desc?.enumerable ?? true,
|
|
293
|
+
configurable: desc?.configurable ?? true
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return result;
|
|
298
|
+
}
|
|
299
|
+
__name(cloneObject, "cloneObject");
|
|
300
|
+
function deepClone(value) {
|
|
301
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
302
|
+
return _deepClone(value, seen);
|
|
303
|
+
}
|
|
304
|
+
__name(deepClone, "deepClone");
|
|
305
|
+
function deepFreeze(obj) {
|
|
306
|
+
Object.freeze(obj);
|
|
307
|
+
for (const key of Reflect.ownKeys(obj)) {
|
|
308
|
+
const val = obj[key];
|
|
309
|
+
if (isObject(val) && !Object.isFrozen(val)) {
|
|
310
|
+
deepFreeze(val);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return obj;
|
|
314
|
+
}
|
|
315
|
+
__name(deepFreeze, "deepFreeze");
|
|
316
|
+
function getAllPaths(obj, parentPath = "") {
|
|
317
|
+
if (!isObject(obj)) return [];
|
|
318
|
+
return Object.entries(obj).flatMap(([key, value]) => {
|
|
319
|
+
const currentPath = parentPath ? `${parentPath}.${key}` : key;
|
|
320
|
+
if (isObject(value)) {
|
|
321
|
+
return [
|
|
322
|
+
currentPath,
|
|
323
|
+
...getAllPaths(value, currentPath)
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
return [
|
|
327
|
+
currentPath
|
|
328
|
+
];
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
__name(getAllPaths, "getAllPaths");
|
|
332
|
+
function invert(obj) {
|
|
333
|
+
if (!isObject(obj)) return {};
|
|
334
|
+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [
|
|
335
|
+
String(v),
|
|
336
|
+
k
|
|
337
|
+
]));
|
|
338
|
+
}
|
|
339
|
+
__name(invert, "invert");
|
|
340
|
+
function invertBy(obj, keyFn) {
|
|
341
|
+
if (!isObject(obj)) return {};
|
|
342
|
+
const result = {};
|
|
343
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
344
|
+
const newKey = keyFn(value);
|
|
345
|
+
if (!result[newKey]) result[newKey] = [];
|
|
346
|
+
result[newKey].push(key);
|
|
347
|
+
}
|
|
348
|
+
return result;
|
|
349
|
+
}
|
|
350
|
+
__name(invertBy, "invertBy");
|
|
351
|
+
function transform(obj, fn) {
|
|
352
|
+
if (!isObject(obj)) return {};
|
|
353
|
+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [
|
|
354
|
+
k,
|
|
355
|
+
fn(v, k)
|
|
356
|
+
]));
|
|
357
|
+
}
|
|
358
|
+
__name(transform, "transform");
|
|
359
|
+
|
|
360
|
+
export { deepClone, deepFreeze, deepObjMerge, filterObject, flattenObject, getAllPaths, getValueByPath, invert, invertBy, isEqual, isObjEmpty, isObject, isPlainObject, mapObject, omit, pick, setValueByPath, transform };
|
package/package.json
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@catbee/utils",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
-
"main": "index.cjs",
|
|
9
|
-
"module": "index.mjs",
|
|
10
|
-
"types": "index.d.ts",
|
|
11
8
|
"author": "Hari G <harig@catbee.in>",
|
|
12
9
|
"maintainers": [
|
|
13
10
|
"Hari G <harig@catbee.in>",
|
|
@@ -23,8 +20,7 @@
|
|
|
23
20
|
},
|
|
24
21
|
"license": "MIT",
|
|
25
22
|
"dependencies": {
|
|
26
|
-
"pino": "^10.1.0"
|
|
27
|
-
"reflect-metadata": "^0.2.2"
|
|
23
|
+
"pino": "^10.1.0"
|
|
28
24
|
},
|
|
29
25
|
"optionalDependencies": {
|
|
30
26
|
"@scalar/express-api-reference": "^0.8.30",
|
|
@@ -36,7 +32,8 @@
|
|
|
36
32
|
"express-rate-limit": "^8.2.1",
|
|
37
33
|
"helmet": "^8.1.0",
|
|
38
34
|
"pino-pretty": "^13.1.3",
|
|
39
|
-
"prom-client": "^15.1.3"
|
|
35
|
+
"prom-client": "^15.1.3",
|
|
36
|
+
"reflect-metadata": "^0.2.2"
|
|
40
37
|
},
|
|
41
38
|
"peerDependencies": {
|
|
42
39
|
"express": "^5.1.0"
|
|
@@ -49,6 +46,9 @@
|
|
|
49
46
|
"engines": {
|
|
50
47
|
"node": ">=18.4"
|
|
51
48
|
},
|
|
49
|
+
"main": "index.cjs",
|
|
50
|
+
"module": "index.mjs",
|
|
51
|
+
"types": "index.d.ts",
|
|
52
52
|
"files": [
|
|
53
53
|
"**/*.mjs",
|
|
54
54
|
"**/*.cjs",
|
|
@@ -57,6 +57,24 @@
|
|
|
57
57
|
"LICENSE",
|
|
58
58
|
"README.md"
|
|
59
59
|
],
|
|
60
|
+
"keywords": [
|
|
61
|
+
"catbee",
|
|
62
|
+
"catbee-utils",
|
|
63
|
+
"utils",
|
|
64
|
+
"utilities",
|
|
65
|
+
"helper",
|
|
66
|
+
"helpers",
|
|
67
|
+
"tools",
|
|
68
|
+
"typescript",
|
|
69
|
+
"nodejs",
|
|
70
|
+
"node",
|
|
71
|
+
"esm",
|
|
72
|
+
"commonjs",
|
|
73
|
+
"express",
|
|
74
|
+
"expressjs",
|
|
75
|
+
"express-server",
|
|
76
|
+
"developer-tools"
|
|
77
|
+
],
|
|
60
78
|
"exports": {
|
|
61
79
|
".": {
|
|
62
80
|
"import": "./index.mjs",
|
|
@@ -98,15 +116,15 @@
|
|
|
98
116
|
"require": "./date/index.cjs",
|
|
99
117
|
"types": "./date/index.d.ts"
|
|
100
118
|
},
|
|
101
|
-
"./
|
|
102
|
-
"import": "./
|
|
103
|
-
"require": "./
|
|
104
|
-
"types": "./
|
|
119
|
+
"./decorator": {
|
|
120
|
+
"import": "./decorator/index.mjs",
|
|
121
|
+
"require": "./decorator/index.cjs",
|
|
122
|
+
"types": "./decorator/index.d.ts"
|
|
105
123
|
},
|
|
106
|
-
"./
|
|
107
|
-
"import": "./
|
|
108
|
-
"require": "./
|
|
109
|
-
"types": "./
|
|
124
|
+
"./directory": {
|
|
125
|
+
"import": "./directory/index.mjs",
|
|
126
|
+
"require": "./directory/index.cjs",
|
|
127
|
+
"types": "./directory/index.d.ts"
|
|
110
128
|
},
|
|
111
129
|
"./env": {
|
|
112
130
|
"import": "./env/index.mjs",
|
|
@@ -143,10 +161,10 @@
|
|
|
143
161
|
"require": "./middleware/index.cjs",
|
|
144
162
|
"types": "./middleware/index.d.ts"
|
|
145
163
|
},
|
|
146
|
-
"./
|
|
147
|
-
"import": "./
|
|
148
|
-
"require": "./
|
|
149
|
-
"types": "./
|
|
164
|
+
"./object": {
|
|
165
|
+
"import": "./object/index.mjs",
|
|
166
|
+
"require": "./object/index.cjs",
|
|
167
|
+
"types": "./object/index.d.ts"
|
|
150
168
|
},
|
|
151
169
|
"./performance": {
|
|
152
170
|
"import": "./performance/index.mjs",
|
|
@@ -193,10 +211,10 @@
|
|
|
193
211
|
"require": "./url/index.cjs",
|
|
194
212
|
"types": "./url/index.d.ts"
|
|
195
213
|
},
|
|
196
|
-
"./
|
|
197
|
-
"import": "./
|
|
198
|
-
"require": "./
|
|
199
|
-
"types": "./
|
|
214
|
+
"./validation": {
|
|
215
|
+
"import": "./validation/index.mjs",
|
|
216
|
+
"require": "./validation/index.cjs",
|
|
217
|
+
"types": "./validation/index.d.ts"
|
|
200
218
|
}
|
|
201
219
|
}
|
|
202
220
|
}
|
package/performance/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
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
|
|
@@ -193,15 +193,15 @@ function memoize(fn, options = {}) {
|
|
|
193
193
|
__name(memoize, "memoize");
|
|
194
194
|
function trackMemoryUsage(fn, options = {}) {
|
|
195
195
|
const { log = false, label = fn.name || "anonymous function" } = options;
|
|
196
|
-
if (
|
|
197
|
-
|
|
196
|
+
if (globalThis.gc) {
|
|
197
|
+
globalThis.gc();
|
|
198
198
|
}
|
|
199
199
|
const beforeMemory = process.memoryUsage();
|
|
200
200
|
const result = fn();
|
|
201
201
|
const afterMemory = process.memoryUsage();
|
|
202
202
|
const diff = {};
|
|
203
203
|
for (const key in afterMemory) {
|
|
204
|
-
if (Object.
|
|
204
|
+
if (Object.hasOwn(afterMemory, key)) {
|
|
205
205
|
diff[key] = afterMemory[key] - beforeMemory[key];
|
|
206
206
|
}
|
|
207
207
|
}
|
package/performance/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
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,141 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Options for timing function execution.
|
|
27
|
+
*/
|
|
28
|
+
interface TimingOptions {
|
|
29
|
+
/** Optional label for the timing (defaults to function name) */
|
|
30
|
+
label?: string;
|
|
31
|
+
/** Whether to log the timing (default: false) */
|
|
32
|
+
log?: boolean;
|
|
33
|
+
/** Log level to use if logging is enabled (default: 'debug') */
|
|
34
|
+
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Result of a timing operation.
|
|
38
|
+
*/
|
|
39
|
+
interface TimingResult {
|
|
40
|
+
/** Duration in milliseconds */
|
|
41
|
+
durationMs: number;
|
|
42
|
+
/** Duration in seconds */
|
|
43
|
+
durationSec: number;
|
|
44
|
+
/** Start timestamp */
|
|
45
|
+
startTime: number;
|
|
46
|
+
/** End timestamp */
|
|
47
|
+
endTime: number;
|
|
48
|
+
/** Label used for the timing */
|
|
49
|
+
label: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Measure the execution time of a synchronous function.
|
|
53
|
+
*
|
|
54
|
+
* @param fn - Function to measure
|
|
55
|
+
* @param options - Timing options
|
|
56
|
+
* @returns Result containing the return value and timing information
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const { result, timing } = timeSync(() => {
|
|
61
|
+
* // Some expensive operation
|
|
62
|
+
* return computeResult();
|
|
63
|
+
* }, { label: 'Computation', log: true });
|
|
64
|
+
*
|
|
65
|
+
* console.log(`Result: ${result}, took ${timing.durationMs}ms`);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function timeSync<T>(fn: () => T, options?: TimingOptions): {
|
|
69
|
+
result: T;
|
|
70
|
+
timing: TimingResult;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Measure the execution time of an asynchronous function.
|
|
74
|
+
*
|
|
75
|
+
* @param fn - Async function to measure
|
|
76
|
+
* @param options - Timing options
|
|
77
|
+
* @returns Promise resolving to result and timing information
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const { result, timing } = await timeAsync(async () => {
|
|
82
|
+
* // Some expensive async operation
|
|
83
|
+
* const data = await fetchData();
|
|
84
|
+
* return processData(data);
|
|
85
|
+
* }, { label: 'API Request', log: true });
|
|
86
|
+
*
|
|
87
|
+
* console.log(`Fetched ${result.length} items in ${timing.durationSec.toFixed(2)}s`);
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
declare function timeAsync<T>(fn: () => Promise<T>, options?: TimingOptions): Promise<{
|
|
91
|
+
result: T;
|
|
92
|
+
timing: TimingResult;
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Create a timing decorator for class methods.
|
|
96
|
+
*
|
|
97
|
+
* @param options - Timing options
|
|
98
|
+
* @returns Method decorator
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* class DataService {
|
|
103
|
+
* @timed({ log: true, logLevel: 'info' })
|
|
104
|
+
* async fetchData() {
|
|
105
|
+
* // ...implementation
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare function timed(options?: TimingOptions): (target: any, propertyKeyOrContext: string | symbol | any, descriptor?: PropertyDescriptor) => void;
|
|
111
|
+
/**
|
|
112
|
+
* Memoize function results with optional TTL and max cache size.
|
|
113
|
+
*
|
|
114
|
+
* @param fn - Function to memoize
|
|
115
|
+
* @param options - Memoization options
|
|
116
|
+
* @returns Memoized function
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* // Cache results for 30 seconds, with a maximum of 100 entries
|
|
121
|
+
* const cachedFetch = memoize(
|
|
122
|
+
* async (url) => {
|
|
123
|
+
* const response = await fetch(url);
|
|
124
|
+
* return response.json();
|
|
125
|
+
* },
|
|
126
|
+
* { ttl: 30000, maxSize: 100, cacheKey: (url) => url }
|
|
127
|
+
* );
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
declare function memoize<T, Args extends any[]>(fn: (...args: Args) => T, options?: {
|
|
131
|
+
/** Time-to-live in milliseconds (default: indefinite) */
|
|
132
|
+
ttl?: number;
|
|
133
|
+
/** Maximum cache size (default: unlimited) */
|
|
134
|
+
maxSize?: number;
|
|
135
|
+
/** Function to generate a cache key from arguments */
|
|
136
|
+
cacheKey?: (...args: Args) => string;
|
|
137
|
+
/** Auto-cleanup interval in milliseconds (default: disabled) */
|
|
138
|
+
autoCleanupMs?: number;
|
|
139
|
+
}): (...args: Args) => T;
|
|
140
|
+
/**
|
|
141
|
+
* Track memory usage for a function execution.
|
|
142
|
+
*
|
|
143
|
+
* @param fn - Function to track
|
|
144
|
+
* @param options - Memory tracking options
|
|
145
|
+
* @returns Result and memory usage information
|
|
146
|
+
*/
|
|
147
|
+
declare function trackMemoryUsage<T>(fn: () => T, options?: {
|
|
148
|
+
/** Whether to log the memory usage (default: false) */
|
|
149
|
+
log?: boolean;
|
|
150
|
+
/** Label for the memory tracking (default: function name) */
|
|
151
|
+
label?: string;
|
|
152
|
+
}): {
|
|
153
|
+
result: T;
|
|
154
|
+
memoryUsage: {
|
|
155
|
+
before: NodeJS.MemoryUsage;
|
|
156
|
+
after: NodeJS.MemoryUsage;
|
|
157
|
+
diff: Record<string, number>;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export { memoize, timeAsync, timeSync, timed, trackMemoryUsage };
|
|
162
|
+
export type { TimingOptions, TimingResult };
|
package/performance/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
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
|
|
@@ -191,15 +191,15 @@ function memoize(fn, options = {}) {
|
|
|
191
191
|
__name(memoize, "memoize");
|
|
192
192
|
function trackMemoryUsage(fn, options = {}) {
|
|
193
193
|
const { log = false, label = fn.name || "anonymous function" } = options;
|
|
194
|
-
if (
|
|
195
|
-
|
|
194
|
+
if (globalThis.gc) {
|
|
195
|
+
globalThis.gc();
|
|
196
196
|
}
|
|
197
197
|
const beforeMemory = process.memoryUsage();
|
|
198
198
|
const result = fn();
|
|
199
199
|
const afterMemory = process.memoryUsage();
|
|
200
200
|
const diff = {};
|
|
201
201
|
for (const key in afterMemory) {
|
|
202
|
-
if (Object.
|
|
202
|
+
if (Object.hasOwn(afterMemory, key)) {
|
|
203
203
|
diff[key] = afterMemory[key] - beforeMemory[key];
|
|
204
204
|
}
|
|
205
205
|
}
|