@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,428 +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.isObjEmpty = isObjEmpty;
|
|
27
|
-
exports.pick = pick;
|
|
28
|
-
exports.omit = omit;
|
|
29
|
-
exports.deepObjMerge = deepObjMerge;
|
|
30
|
-
exports.isPlainObject = isPlainObject;
|
|
31
|
-
exports.flattenObject = flattenObject;
|
|
32
|
-
exports.getValueByPath = getValueByPath;
|
|
33
|
-
exports.setValueByPath = setValueByPath;
|
|
34
|
-
exports.isEqual = isEqual;
|
|
35
|
-
exports.filterObject = filterObject;
|
|
36
|
-
exports.mapObject = mapObject;
|
|
37
|
-
exports.deepFreeze = deepFreeze;
|
|
38
|
-
exports.isObject = isObject;
|
|
39
|
-
exports.getAllPaths = getAllPaths;
|
|
40
|
-
/**
|
|
41
|
-
* Checks whether the object has no own enumerable properties.
|
|
42
|
-
*
|
|
43
|
-
* @param {Record<any, any>} obj - The object to check.
|
|
44
|
-
* @returns {boolean} True if the object is empty, false otherwise.
|
|
45
|
-
*/
|
|
46
|
-
function isObjEmpty(obj) {
|
|
47
|
-
return !!obj && typeof obj === 'object' && Object.keys(obj).length === 0;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Returns a new object with only the specified keys picked.
|
|
51
|
-
*
|
|
52
|
-
* @template T
|
|
53
|
-
* @template K
|
|
54
|
-
* @param {T} obj - The source object.
|
|
55
|
-
* @param {K[]} keys - Keys to pick from the object.
|
|
56
|
-
* @returns {Pick<T, K>} New object with picked keys.
|
|
57
|
-
*/
|
|
58
|
-
function pick(obj, keys) {
|
|
59
|
-
return Object.fromEntries(keys.map(key => [key, obj[key]]));
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Returns a new object with the specified keys omitted.
|
|
63
|
-
*
|
|
64
|
-
* @template T
|
|
65
|
-
* @template K
|
|
66
|
-
* @param {T} obj - The source object.
|
|
67
|
-
* @param {K[]} keys - Keys to omit from the object.
|
|
68
|
-
* @returns {Omit<T, K>} New object without omitted keys.
|
|
69
|
-
*/
|
|
70
|
-
function omit(obj, keys) {
|
|
71
|
-
return Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key)));
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Deeply merges multiple objects into the target object (mutates and returns the target object, not pure).
|
|
75
|
-
*
|
|
76
|
-
* @template T
|
|
77
|
-
* @param {T} target - The object to merge into (will be mutated).
|
|
78
|
-
* @param {...any[]} sources - The objects to merge from.
|
|
79
|
-
* @returns {T} The merged object (same as target).
|
|
80
|
-
*/
|
|
81
|
-
function deepObjMerge(target, ...sources) {
|
|
82
|
-
if (!sources.length)
|
|
83
|
-
return target;
|
|
84
|
-
const seen = new WeakMap();
|
|
85
|
-
seen.set(target, target);
|
|
86
|
-
for (const s of sources) {
|
|
87
|
-
if (s && typeof s === 'object') {
|
|
88
|
-
seen.set(s, target);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
function deepClone(value) {
|
|
92
|
-
if (value === null || typeof value !== 'object')
|
|
93
|
-
return value;
|
|
94
|
-
if (seen.has(value))
|
|
95
|
-
return seen.get(value);
|
|
96
|
-
// Date
|
|
97
|
-
if (value instanceof Date) {
|
|
98
|
-
return new Date(value.getTime());
|
|
99
|
-
}
|
|
100
|
-
// Array
|
|
101
|
-
if (Array.isArray(value)) {
|
|
102
|
-
const clone = [];
|
|
103
|
-
seen.set(value, clone);
|
|
104
|
-
value.forEach((item, i) => (clone[i] = deepClone(item)));
|
|
105
|
-
return clone;
|
|
106
|
-
}
|
|
107
|
-
// Set
|
|
108
|
-
if (value instanceof Set) {
|
|
109
|
-
const clone = new Set();
|
|
110
|
-
seen.set(value, clone);
|
|
111
|
-
value.forEach(item => clone.add(deepClone(item)));
|
|
112
|
-
return clone;
|
|
113
|
-
}
|
|
114
|
-
// Map
|
|
115
|
-
if (value instanceof Map) {
|
|
116
|
-
const clone = new Map();
|
|
117
|
-
seen.set(value, clone);
|
|
118
|
-
value.forEach((val, key) => clone.set(key, deepClone(val)));
|
|
119
|
-
return clone;
|
|
120
|
-
}
|
|
121
|
-
// RegExp
|
|
122
|
-
if (value instanceof RegExp) {
|
|
123
|
-
return new RegExp(value.source, value.flags);
|
|
124
|
-
}
|
|
125
|
-
// ArrayBuffer
|
|
126
|
-
if (value instanceof ArrayBuffer) {
|
|
127
|
-
return value.slice(0);
|
|
128
|
-
}
|
|
129
|
-
// TypedArrays
|
|
130
|
-
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
|
|
131
|
-
const typed = value; // cast for TS
|
|
132
|
-
const ctor = typed.constructor;
|
|
133
|
-
// List of standard TypedArray constructors
|
|
134
|
-
const typedArrayCtors = [
|
|
135
|
-
Int8Array,
|
|
136
|
-
Uint8Array,
|
|
137
|
-
Uint8ClampedArray,
|
|
138
|
-
Int16Array,
|
|
139
|
-
Uint16Array,
|
|
140
|
-
Int32Array,
|
|
141
|
-
Uint32Array,
|
|
142
|
-
Float32Array,
|
|
143
|
-
Float64Array,
|
|
144
|
-
BigInt64Array,
|
|
145
|
-
BigUint64Array
|
|
146
|
-
].filter(Boolean); // filter out undefined (for environments without BigInt)
|
|
147
|
-
const typedArrayCtorSet = new Set(typedArrayCtors);
|
|
148
|
-
if (typeof ctor === 'function' && typedArrayCtorSet.has(ctor)) {
|
|
149
|
-
// Defensive: ensure buffer is an ArrayBuffer and slice is available
|
|
150
|
-
const buffer = typed.buffer && typeof typed.buffer.slice === 'function' ? typed.buffer.slice(0) : typed.buffer;
|
|
151
|
-
const clone = new ctor(buffer, typed.byteOffset, typed.length);
|
|
152
|
-
seen.set(value, clone);
|
|
153
|
-
return clone;
|
|
154
|
-
}
|
|
155
|
-
else if (typeof typed.slice === 'function') {
|
|
156
|
-
// Fallback: try using slice if available (e.g., Node.js Buffer)
|
|
157
|
-
const clone = typed.slice();
|
|
158
|
-
seen.set(value, clone);
|
|
159
|
-
return clone;
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
// Could not safely clone, throw an error for better debugging
|
|
163
|
-
throw new Error('Cannot deep clone unknown TypedArray type: ' + (typed && typed.constructor && typed.constructor.name));
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
const clone = Object.create(Object.getPrototypeOf(value));
|
|
167
|
-
seen.set(value, clone);
|
|
168
|
-
Reflect.ownKeys(value).forEach(key => {
|
|
169
|
-
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
170
|
-
if (descriptor.get || descriptor.set) {
|
|
171
|
-
Object.defineProperty(clone, key, descriptor);
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
clone[key] = deepClone(value[key]);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
return clone;
|
|
178
|
-
}
|
|
179
|
-
function mergeProp(target, source, key) {
|
|
180
|
-
const targetValue = target[key];
|
|
181
|
-
const sourceValue = source[key];
|
|
182
|
-
// Ignore undefined in source
|
|
183
|
-
if (sourceValue === undefined)
|
|
184
|
-
return;
|
|
185
|
-
// Primitives → overwrite directly
|
|
186
|
-
if (sourceValue === null || typeof sourceValue !== 'object') {
|
|
187
|
-
target[key] = sourceValue;
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
// Arrays → replace (lodash behavior)
|
|
191
|
-
if (Array.isArray(sourceValue)) {
|
|
192
|
-
target[key] = deepClone(sourceValue);
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
// Special cloneable objects
|
|
196
|
-
if (sourceValue instanceof Date ||
|
|
197
|
-
sourceValue instanceof RegExp ||
|
|
198
|
-
sourceValue instanceof Set ||
|
|
199
|
-
sourceValue instanceof Map ||
|
|
200
|
-
sourceValue instanceof ArrayBuffer ||
|
|
201
|
-
(ArrayBuffer.isView(sourceValue) && !(sourceValue instanceof DataView))) {
|
|
202
|
-
target[key] = deepClone(sourceValue);
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
// Plain objects → recursive merge
|
|
206
|
-
if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
|
|
207
|
-
for (const k of Reflect.ownKeys(sourceValue)) {
|
|
208
|
-
mergeProp(targetValue, sourceValue, k);
|
|
209
|
-
}
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
// Default → overwrite with clone
|
|
213
|
-
target[key] = deepClone(sourceValue);
|
|
214
|
-
}
|
|
215
|
-
for (const source of sources) {
|
|
216
|
-
if (source == null)
|
|
217
|
-
continue;
|
|
218
|
-
for (const key of Reflect.ownKeys(source)) {
|
|
219
|
-
mergeProp(target, source, key);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return target;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Checks if a value is a plain object (i.e., not an array, null, or a built-in object).
|
|
226
|
-
*
|
|
227
|
-
* @param value - The value to check.
|
|
228
|
-
* @returns True if the value is a plain object, false otherwise.
|
|
229
|
-
*/
|
|
230
|
-
function isPlainObject(value) {
|
|
231
|
-
if (typeof value !== 'object' || value === null)
|
|
232
|
-
return false;
|
|
233
|
-
if (Array.isArray(value))
|
|
234
|
-
return false;
|
|
235
|
-
const proto = Object.getPrototypeOf(value);
|
|
236
|
-
return proto === Object.prototype || proto === null;
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Flattens a nested object using dot notation for keys (e.g., `{a: {b: 1}}` → `{ "a.b": 1 }`).
|
|
240
|
-
*
|
|
241
|
-
* @template T
|
|
242
|
-
* @param {T} obj - The object to flatten.
|
|
243
|
-
* @param {string} [prefix=""] - Optional prefix for nested keys (used internally).
|
|
244
|
-
* @returns {Record<string, any>} A new object with flattened keys.
|
|
245
|
-
*/
|
|
246
|
-
function flattenObject(obj, prefix = '') {
|
|
247
|
-
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
248
|
-
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
249
|
-
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
250
|
-
Object.assign(acc, flattenObject(value, newKey));
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
acc[newKey] = value;
|
|
254
|
-
}
|
|
255
|
-
return acc;
|
|
256
|
-
}, {});
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Safely gets the value of a deeply nested key in an object using dot/bracket notation path.
|
|
260
|
-
*
|
|
261
|
-
* @template T
|
|
262
|
-
* @param {T} obj - The object to extract from.
|
|
263
|
-
* @param {string} path - String path using dot and/or bracket notation (e.g., 'user.friends[0].name').
|
|
264
|
-
* @returns {any} The value at the given path, or undefined if not found.
|
|
265
|
-
*/
|
|
266
|
-
function getValueByPath(obj, path) {
|
|
267
|
-
if (!obj || typeof obj !== 'object')
|
|
268
|
-
return undefined;
|
|
269
|
-
// Convert path like "a.b[0].c" into ["a", "b", "0", "c"]
|
|
270
|
-
const parts = path
|
|
271
|
-
.replace(/\[(\d+)\]/g, '.$1') // convert [0] to .0
|
|
272
|
-
.split('.')
|
|
273
|
-
.filter(Boolean); // remove empty strings
|
|
274
|
-
return parts.reduce((acc, key) => acc?.[key], obj);
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Sets a value at a deeply nested key in an object using dot/bracket notation path.
|
|
278
|
-
* Creates intermediate objects and arrays as needed.
|
|
279
|
-
*
|
|
280
|
-
* @template T
|
|
281
|
-
* @param {T} obj - The object to modify.
|
|
282
|
-
* @param {string} path - String path using dot and/or bracket notation (e.g., 'user.friends[0].name').
|
|
283
|
-
* @param {any} value - The value to set at the path.
|
|
284
|
-
* @returns {T} The modified object.
|
|
285
|
-
*/
|
|
286
|
-
function setValueByPath(obj, path, value) {
|
|
287
|
-
if (!obj || typeof obj !== 'object')
|
|
288
|
-
return obj;
|
|
289
|
-
// Convert path like "a.b[0].c" into ["a", "b", "0", "c"]
|
|
290
|
-
const parts = path
|
|
291
|
-
.replace(/\[(\d+)\]/g, '.$1') // convert [0] to .0
|
|
292
|
-
.split('.')
|
|
293
|
-
.filter(Boolean); // remove empty strings
|
|
294
|
-
// Handle empty path
|
|
295
|
-
if (parts.length === 0)
|
|
296
|
-
return obj;
|
|
297
|
-
let current = obj;
|
|
298
|
-
// Navigate to the parent of the target property
|
|
299
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
300
|
-
const key = parts[i];
|
|
301
|
-
const isArrayIndex = !isNaN(Number(parts[i + 1]));
|
|
302
|
-
if (current[key] === undefined) {
|
|
303
|
-
// Create intermediate object or array based on next key type
|
|
304
|
-
current[key] = isArrayIndex ? [] : {};
|
|
305
|
-
}
|
|
306
|
-
current = current[key];
|
|
307
|
-
}
|
|
308
|
-
// Set the value at the final key
|
|
309
|
-
const finalKey = parts[parts.length - 1];
|
|
310
|
-
current[finalKey] = value;
|
|
311
|
-
return obj;
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Performs a deep equality check between two objects.
|
|
315
|
-
*
|
|
316
|
-
* @param {any} a - First value to compare.
|
|
317
|
-
* @param {any} b - Second value to compare.
|
|
318
|
-
* @returns {boolean} True if values are deeply equal.
|
|
319
|
-
*/
|
|
320
|
-
function isEqual(a, b) {
|
|
321
|
-
// Check if primitives or references to the same object
|
|
322
|
-
if (a === b)
|
|
323
|
-
return true;
|
|
324
|
-
// Check if either is null/undefined or not an object
|
|
325
|
-
if (a == null || b == null || typeof a !== 'object' || typeof b !== 'object') {
|
|
326
|
-
return false;
|
|
327
|
-
}
|
|
328
|
-
// Handle Date objects
|
|
329
|
-
if (a instanceof Date && b instanceof Date) {
|
|
330
|
-
return a.getTime() === b.getTime();
|
|
331
|
-
}
|
|
332
|
-
// If one is Date but the other isn't
|
|
333
|
-
if (a instanceof Date || b instanceof Date) {
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
|
-
// Handle arrays
|
|
337
|
-
if (Array.isArray(a) && Array.isArray(b)) {
|
|
338
|
-
if (a.length !== b.length)
|
|
339
|
-
return false;
|
|
340
|
-
// Check each element
|
|
341
|
-
for (let i = 0; i < a.length; i++) {
|
|
342
|
-
if (!isEqual(a[i], b[i]))
|
|
343
|
-
return false;
|
|
344
|
-
}
|
|
345
|
-
return true;
|
|
346
|
-
}
|
|
347
|
-
// If one is Array but the other isn't
|
|
348
|
-
if (Array.isArray(a) || Array.isArray(b)) {
|
|
349
|
-
return false;
|
|
350
|
-
}
|
|
351
|
-
// Compare object keys
|
|
352
|
-
const keysA = Object.keys(a);
|
|
353
|
-
const keysB = Object.keys(b);
|
|
354
|
-
if (keysA.length !== keysB.length)
|
|
355
|
-
return false;
|
|
356
|
-
// Check if every key in A exists in B and has the same value
|
|
357
|
-
return keysA.every(key => Object.prototype.hasOwnProperty.call(b, key) && isEqual(a[key], b[key]));
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
* Filters object properties based on a predicate function.
|
|
361
|
-
*
|
|
362
|
-
* @template T
|
|
363
|
-
* @param {T} obj - The source object.
|
|
364
|
-
* @param {(value: any, key: string, obj: T) => boolean} predicate - Filter function.
|
|
365
|
-
* @returns {Partial<T>} New object with filtered properties.
|
|
366
|
-
*/
|
|
367
|
-
function filterObject(obj, predicate) {
|
|
368
|
-
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(value, key, obj)));
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* Maps object values to new values using a mapping function.
|
|
372
|
-
*
|
|
373
|
-
* @template T
|
|
374
|
-
* @template U
|
|
375
|
-
* @param {T} obj - The source object.
|
|
376
|
-
* @param {(value: any, key: string, obj: T) => U} mapFn - Mapping function.
|
|
377
|
-
* @returns {Record<keyof T, U>} New object with mapped values.
|
|
378
|
-
*/
|
|
379
|
-
function mapObject(obj, mapFn) {
|
|
380
|
-
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, mapFn(value, key, obj)]));
|
|
381
|
-
}
|
|
382
|
-
/**
|
|
383
|
-
* Recursively freezes an object and all its properties.
|
|
384
|
-
* Makes an object immutable.
|
|
385
|
-
*
|
|
386
|
-
* @template T
|
|
387
|
-
* @param {T} obj - The object to freeze.
|
|
388
|
-
* @returns {Readonly<T>} The frozen object.
|
|
389
|
-
*/
|
|
390
|
-
function deepFreeze(obj) {
|
|
391
|
-
// Freeze primitive properties
|
|
392
|
-
Object.freeze(obj);
|
|
393
|
-
// Recursively freeze nested objects
|
|
394
|
-
for (const prop of Object.getOwnPropertyNames(obj)) {
|
|
395
|
-
const value = obj[prop];
|
|
396
|
-
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
397
|
-
deepFreeze(value);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
return obj;
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Safely checks if a value is an object (not null, not array).
|
|
404
|
-
*
|
|
405
|
-
* @param {unknown} value - Value to check.
|
|
406
|
-
* @returns {boolean} True if value is a non-null, non-array object.
|
|
407
|
-
*/
|
|
408
|
-
function isObject(value) {
|
|
409
|
-
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
410
|
-
}
|
|
411
|
-
/**
|
|
412
|
-
* Gets all paths in an object using dot notation.
|
|
413
|
-
*
|
|
414
|
-
* @param {Record<string, any>} obj - Object to analyze.
|
|
415
|
-
* @param {string} [parentPath=""] - Internal param for recursion.
|
|
416
|
-
* @returns {string[]} Array of all paths in dot notation.
|
|
417
|
-
*/
|
|
418
|
-
function getAllPaths(obj, parentPath = '') {
|
|
419
|
-
if (!isObject(obj))
|
|
420
|
-
return [];
|
|
421
|
-
return Object.entries(obj).flatMap(([key, value]) => {
|
|
422
|
-
const currentPath = parentPath ? `${parentPath}.${key}` : key;
|
|
423
|
-
if (isObject(value)) {
|
|
424
|
-
return [currentPath, ...getAllPaths(value, currentPath)];
|
|
425
|
-
}
|
|
426
|
-
return [currentPath];
|
|
427
|
-
});
|
|
428
|
-
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options for timing function execution.
|
|
3
|
-
*/
|
|
4
|
-
export interface TimingOptions {
|
|
5
|
-
/** Optional label for the timing (defaults to function name) */
|
|
6
|
-
label?: string;
|
|
7
|
-
/** Whether to log the timing (default: false) */
|
|
8
|
-
log?: boolean;
|
|
9
|
-
/** Log level to use if logging is enabled (default: 'debug') */
|
|
10
|
-
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Result of a timing operation.
|
|
14
|
-
*/
|
|
15
|
-
export interface TimingResult {
|
|
16
|
-
/** Duration in milliseconds */
|
|
17
|
-
durationMs: number;
|
|
18
|
-
/** Duration in seconds */
|
|
19
|
-
durationSec: number;
|
|
20
|
-
/** Start timestamp */
|
|
21
|
-
startTime: number;
|
|
22
|
-
/** End timestamp */
|
|
23
|
-
endTime: number;
|
|
24
|
-
/** Label used for the timing */
|
|
25
|
-
label: string;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Measure the execution time of a synchronous function.
|
|
29
|
-
*
|
|
30
|
-
* @param fn - Function to measure
|
|
31
|
-
* @param options - Timing options
|
|
32
|
-
* @returns Result containing the return value and timing information
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```typescript
|
|
36
|
-
* const { result, timing } = timeSync(() => {
|
|
37
|
-
* // Some expensive operation
|
|
38
|
-
* return computeResult();
|
|
39
|
-
* }, { label: 'Computation', log: true });
|
|
40
|
-
*
|
|
41
|
-
* console.log(`Result: ${result}, took ${timing.durationMs}ms`);
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
export declare function timeSync<T>(fn: () => T, options?: TimingOptions): {
|
|
45
|
-
result: T;
|
|
46
|
-
timing: TimingResult;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Measure the execution time of an asynchronous function.
|
|
50
|
-
*
|
|
51
|
-
* @param fn - Async function to measure
|
|
52
|
-
* @param options - Timing options
|
|
53
|
-
* @returns Promise resolving to result and timing information
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* const { result, timing } = await timeAsync(async () => {
|
|
58
|
-
* // Some expensive async operation
|
|
59
|
-
* const data = await fetchData();
|
|
60
|
-
* return processData(data);
|
|
61
|
-
* }, { label: 'API Request', log: true });
|
|
62
|
-
*
|
|
63
|
-
* console.log(`Fetched ${result.length} items in ${timing.durationSec.toFixed(2)}s`);
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
export declare function timeAsync<T>(fn: () => Promise<T>, options?: TimingOptions): Promise<{
|
|
67
|
-
result: T;
|
|
68
|
-
timing: TimingResult;
|
|
69
|
-
}>;
|
|
70
|
-
/**
|
|
71
|
-
* Create a timing decorator for class methods.
|
|
72
|
-
*
|
|
73
|
-
* @param options - Timing options
|
|
74
|
-
* @returns Method decorator
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```typescript
|
|
78
|
-
* class DataService {
|
|
79
|
-
* @timed({ log: true, logLevel: 'info' })
|
|
80
|
-
* async fetchData() {
|
|
81
|
-
* // ...implementation
|
|
82
|
-
* }
|
|
83
|
-
* }
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
export declare function timed(options?: TimingOptions): (target: any, propertyKeyOrContext: string | symbol | any, descriptor?: PropertyDescriptor) => void;
|
|
87
|
-
/**
|
|
88
|
-
* Memoize function results with optional TTL and max cache size.
|
|
89
|
-
*
|
|
90
|
-
* @param fn - Function to memoize
|
|
91
|
-
* @param options - Memoization options
|
|
92
|
-
* @returns Memoized function
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* ```typescript
|
|
96
|
-
* // Cache results for 30 seconds, with a maximum of 100 entries
|
|
97
|
-
* const cachedFetch = memoize(
|
|
98
|
-
* async (url) => {
|
|
99
|
-
* const response = await fetch(url);
|
|
100
|
-
* return response.json();
|
|
101
|
-
* },
|
|
102
|
-
* { ttl: 30000, maxSize: 100, cacheKey: (url) => url }
|
|
103
|
-
* );
|
|
104
|
-
* ```
|
|
105
|
-
*/
|
|
106
|
-
export declare function memoize<T, Args extends any[]>(fn: (...args: Args) => T, options?: {
|
|
107
|
-
/** Time-to-live in milliseconds (default: indefinite) */
|
|
108
|
-
ttl?: number;
|
|
109
|
-
/** Maximum cache size (default: unlimited) */
|
|
110
|
-
maxSize?: number;
|
|
111
|
-
/** Function to generate a cache key from arguments */
|
|
112
|
-
cacheKey?: (...args: Args) => string;
|
|
113
|
-
/** Auto-cleanup interval in milliseconds (default: disabled) */
|
|
114
|
-
autoCleanupMs?: number;
|
|
115
|
-
}): (...args: Args) => T;
|
|
116
|
-
/**
|
|
117
|
-
* Track memory usage for a function execution.
|
|
118
|
-
*
|
|
119
|
-
* @param fn - Function to track
|
|
120
|
-
* @param options - Memory tracking options
|
|
121
|
-
* @returns Result and memory usage information
|
|
122
|
-
*/
|
|
123
|
-
export declare function trackMemoryUsage<T>(fn: () => T, options?: {
|
|
124
|
-
/** Whether to log the memory usage (default: false) */
|
|
125
|
-
log?: boolean;
|
|
126
|
-
/** Label for the memory tracking (default: function name) */
|
|
127
|
-
label?: string;
|
|
128
|
-
}): {
|
|
129
|
-
result: T;
|
|
130
|
-
memoryUsage: {
|
|
131
|
-
before: NodeJS.MemoryUsage;
|
|
132
|
-
after: NodeJS.MemoryUsage;
|
|
133
|
-
diff: Record<string, number>;
|
|
134
|
-
};
|
|
135
|
-
};
|