@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.cjs
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
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
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var __defProp = Object.defineProperty;
|
|
28
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
|
+
|
|
30
|
+
// src/object/object.utils.ts
|
|
31
|
+
function isObject(value) {
|
|
32
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
33
|
+
}
|
|
34
|
+
__name(isObject, "isObject");
|
|
35
|
+
function isObjEmpty(obj) {
|
|
36
|
+
return isObject(obj) && Object.keys(obj).length === 0;
|
|
37
|
+
}
|
|
38
|
+
__name(isObjEmpty, "isObjEmpty");
|
|
39
|
+
function pick(obj, keys) {
|
|
40
|
+
return Object.fromEntries(keys.map((key) => [
|
|
41
|
+
key,
|
|
42
|
+
obj[key]
|
|
43
|
+
]));
|
|
44
|
+
}
|
|
45
|
+
__name(pick, "pick");
|
|
46
|
+
function omit(obj, keys) {
|
|
47
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key)));
|
|
48
|
+
}
|
|
49
|
+
__name(omit, "omit");
|
|
50
|
+
function deepObjMerge(target, ...sources) {
|
|
51
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
52
|
+
for (const s of sources) {
|
|
53
|
+
target = mergeInto(target, s, seen);
|
|
54
|
+
}
|
|
55
|
+
return target;
|
|
56
|
+
}
|
|
57
|
+
__name(deepObjMerge, "deepObjMerge");
|
|
58
|
+
function mergeInto(t, s, seen) {
|
|
59
|
+
if (s === void 0) return t;
|
|
60
|
+
if (typeof s === "function" || typeof s === "symbol") return s;
|
|
61
|
+
if (Array.isArray(s)) {
|
|
62
|
+
const cloned = deepClone(s);
|
|
63
|
+
seen.set(s, cloned);
|
|
64
|
+
return cloned;
|
|
65
|
+
}
|
|
66
|
+
if (s && typeof s === "object") {
|
|
67
|
+
if (seen.has(s)) return seen.get(s);
|
|
68
|
+
if (s instanceof Date || s instanceof Map || s instanceof Set || s instanceof RegExp || s instanceof ArrayBuffer || ArrayBuffer.isView(s)) {
|
|
69
|
+
const cloned = deepClone(s);
|
|
70
|
+
seen.set(s, cloned);
|
|
71
|
+
return cloned;
|
|
72
|
+
}
|
|
73
|
+
if (!t || typeof t !== "object") {
|
|
74
|
+
t = Object.create(Object.getPrototypeOf(s));
|
|
75
|
+
}
|
|
76
|
+
seen.set(s, t);
|
|
77
|
+
for (const key of Reflect.ownKeys(s)) {
|
|
78
|
+
if (key === "__proto__") continue;
|
|
79
|
+
const sv = s[key];
|
|
80
|
+
if (sv === void 0) continue;
|
|
81
|
+
t[key] = mergeInto(t[key], sv, seen);
|
|
82
|
+
}
|
|
83
|
+
return t;
|
|
84
|
+
}
|
|
85
|
+
return s;
|
|
86
|
+
}
|
|
87
|
+
__name(mergeInto, "mergeInto");
|
|
88
|
+
function isPlainObject(value) {
|
|
89
|
+
if (typeof value !== "object" || value === null) return false;
|
|
90
|
+
if (Array.isArray(value)) return false;
|
|
91
|
+
const proto = Object.getPrototypeOf(value);
|
|
92
|
+
return proto === Object.prototype || proto === null;
|
|
93
|
+
}
|
|
94
|
+
__name(isPlainObject, "isPlainObject");
|
|
95
|
+
function flattenObject(obj, prefix = "") {
|
|
96
|
+
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
97
|
+
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
98
|
+
if (key === "__proto__") return acc;
|
|
99
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
100
|
+
Object.assign(acc, flattenObject(value, newKey));
|
|
101
|
+
} else {
|
|
102
|
+
acc[newKey] = value;
|
|
103
|
+
}
|
|
104
|
+
return acc;
|
|
105
|
+
}, {});
|
|
106
|
+
}
|
|
107
|
+
__name(flattenObject, "flattenObject");
|
|
108
|
+
function getValueByPath(obj, path) {
|
|
109
|
+
if (!obj || typeof obj !== "object") return void 0;
|
|
110
|
+
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
111
|
+
return parts.reduce((acc, key) => acc?.[key], obj);
|
|
112
|
+
}
|
|
113
|
+
__name(getValueByPath, "getValueByPath");
|
|
114
|
+
function ensureNextContainer(curr, nextIsIndex) {
|
|
115
|
+
if (curr === void 0 || typeof curr !== "object" || curr === null) {
|
|
116
|
+
return nextIsIndex ? [] : /* @__PURE__ */ Object.create(null);
|
|
117
|
+
}
|
|
118
|
+
return curr;
|
|
119
|
+
}
|
|
120
|
+
__name(ensureNextContainer, "ensureNextContainer");
|
|
121
|
+
function setValueByPath(obj, path, value) {
|
|
122
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
123
|
+
const clone = deepClone(obj);
|
|
124
|
+
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
125
|
+
if (parts.length === 0) return clone;
|
|
126
|
+
const forbiddenKeys = /* @__PURE__ */ new Set([
|
|
127
|
+
"__proto__",
|
|
128
|
+
"prototype",
|
|
129
|
+
"constructor"
|
|
130
|
+
]);
|
|
131
|
+
let current = clone;
|
|
132
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
133
|
+
const key = parts[i];
|
|
134
|
+
if (forbiddenKeys.has(key)) return clone;
|
|
135
|
+
const nextKey = parts[i + 1];
|
|
136
|
+
const nextIsIndex = !Number.isNaN(Number(nextKey));
|
|
137
|
+
current[key] = ensureNextContainer(current[key], nextIsIndex);
|
|
138
|
+
current = current[key];
|
|
139
|
+
}
|
|
140
|
+
const finalKey = parts.at(-1);
|
|
141
|
+
if (forbiddenKeys.has(finalKey)) return clone;
|
|
142
|
+
current[finalKey] = value;
|
|
143
|
+
return clone;
|
|
144
|
+
}
|
|
145
|
+
__name(setValueByPath, "setValueByPath");
|
|
146
|
+
function isEqual(a, b) {
|
|
147
|
+
if (a === b) return true;
|
|
148
|
+
if (a == null || b == null || typeof a !== "object" || typeof b !== "object") return false;
|
|
149
|
+
if (a instanceof Date && b instanceof Date) return equalDates(a, b);
|
|
150
|
+
if (a instanceof RegExp && b instanceof RegExp) return equalRegExp(a, b);
|
|
151
|
+
if (a instanceof Set && b instanceof Set) return equalSet(a, b);
|
|
152
|
+
if (a instanceof Map && b instanceof Map) return equalMap(a, b);
|
|
153
|
+
if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) return equalArrayBuffer(a, b);
|
|
154
|
+
if (Array.isArray(a) && Array.isArray(b)) return equalArray(a, b);
|
|
155
|
+
return equalPlainObjects(a, b);
|
|
156
|
+
}
|
|
157
|
+
__name(isEqual, "isEqual");
|
|
158
|
+
function equalDates(a, b) {
|
|
159
|
+
return a.getTime() === b.getTime();
|
|
160
|
+
}
|
|
161
|
+
__name(equalDates, "equalDates");
|
|
162
|
+
function equalRegExp(a, b) {
|
|
163
|
+
return a.source === b.source && a.flags === b.flags;
|
|
164
|
+
}
|
|
165
|
+
__name(equalRegExp, "equalRegExp");
|
|
166
|
+
function equalSet(a, b) {
|
|
167
|
+
if (a.size !== b.size) return false;
|
|
168
|
+
for (const v of a) if (!b.has(v)) return false;
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
__name(equalSet, "equalSet");
|
|
172
|
+
function equalMap(a, b) {
|
|
173
|
+
if (a.size !== b.size) return false;
|
|
174
|
+
for (const [k, v] of a) {
|
|
175
|
+
if (!b.has(k)) return false;
|
|
176
|
+
if (!isEqual(v, b.get(k))) return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
__name(equalMap, "equalMap");
|
|
181
|
+
function equalArrayBuffer(a, b) {
|
|
182
|
+
return isEqual(new Uint8Array(a), new Uint8Array(b));
|
|
183
|
+
}
|
|
184
|
+
__name(equalArrayBuffer, "equalArrayBuffer");
|
|
185
|
+
function equalArray(a, b) {
|
|
186
|
+
if (a.length !== b.length) return false;
|
|
187
|
+
for (let i = 0; i < a.length; i++) if (!isEqual(a[i], b[i])) return false;
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
__name(equalArray, "equalArray");
|
|
191
|
+
function equalPlainObjects(a, b) {
|
|
192
|
+
const aKeys = Object.keys(a);
|
|
193
|
+
const bKeys = Object.keys(b);
|
|
194
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
195
|
+
for (const key of aKeys) {
|
|
196
|
+
if (!Object.hasOwn(b, key)) return false;
|
|
197
|
+
if (!isEqual(a[key], b[key])) return false;
|
|
198
|
+
}
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
__name(equalPlainObjects, "equalPlainObjects");
|
|
202
|
+
function filterObject(obj, predicate) {
|
|
203
|
+
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(value, key, obj)));
|
|
204
|
+
}
|
|
205
|
+
__name(filterObject, "filterObject");
|
|
206
|
+
function mapObject(obj, mapFn) {
|
|
207
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
208
|
+
key,
|
|
209
|
+
mapFn(value, key, obj)
|
|
210
|
+
]));
|
|
211
|
+
}
|
|
212
|
+
__name(mapObject, "mapObject");
|
|
213
|
+
function _deepClone(value, seen) {
|
|
214
|
+
if (typeof value === "function" || typeof value === "symbol") return value;
|
|
215
|
+
if (value === null || typeof value !== "object") return value;
|
|
216
|
+
if (seen.has(value)) return seen.get(value);
|
|
217
|
+
if (Array.isArray(value)) return cloneArray(value, seen);
|
|
218
|
+
if (value instanceof Date) return cloneDate(value);
|
|
219
|
+
if (value instanceof Map) return cloneMap(value, seen);
|
|
220
|
+
if (value instanceof Set) return cloneSet(value, seen);
|
|
221
|
+
if (value instanceof RegExp) return cloneRegExp(value);
|
|
222
|
+
if (ArrayBuffer.isView(value)) return cloneArrayBufferView(value, seen);
|
|
223
|
+
if (value instanceof ArrayBuffer) return cloneArrayBuffer(value, seen);
|
|
224
|
+
return cloneObject(value, seen);
|
|
225
|
+
}
|
|
226
|
+
__name(_deepClone, "_deepClone");
|
|
227
|
+
function cloneArray(arr, seen) {
|
|
228
|
+
const result = [];
|
|
229
|
+
seen.set(arr, result);
|
|
230
|
+
for (const item of arr) result.push(_deepClone(item, seen));
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
233
|
+
__name(cloneArray, "cloneArray");
|
|
234
|
+
function cloneDate(d) {
|
|
235
|
+
const r = new Date(d.getTime());
|
|
236
|
+
return r;
|
|
237
|
+
}
|
|
238
|
+
__name(cloneDate, "cloneDate");
|
|
239
|
+
function cloneMap(m, seen) {
|
|
240
|
+
const result = /* @__PURE__ */ new Map();
|
|
241
|
+
seen.set(m, result);
|
|
242
|
+
for (const [k, v] of m) result.set(_deepClone(k, seen), _deepClone(v, seen));
|
|
243
|
+
return result;
|
|
244
|
+
}
|
|
245
|
+
__name(cloneMap, "cloneMap");
|
|
246
|
+
function cloneSet(s, seen) {
|
|
247
|
+
const result = /* @__PURE__ */ new Set();
|
|
248
|
+
seen.set(s, result);
|
|
249
|
+
for (const v of s) result.add(_deepClone(v, seen));
|
|
250
|
+
return result;
|
|
251
|
+
}
|
|
252
|
+
__name(cloneSet, "cloneSet");
|
|
253
|
+
function cloneRegExp(r) {
|
|
254
|
+
const result = new RegExp(r.source, r.flags);
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
__name(cloneRegExp, "cloneRegExp");
|
|
258
|
+
function cloneArrayBufferView(view, seen) {
|
|
259
|
+
if (view instanceof DataView) {
|
|
260
|
+
const buf2 = _deepClone(view.buffer, seen);
|
|
261
|
+
const clone2 = new DataView(buf2, view.byteOffset, view.byteLength);
|
|
262
|
+
seen.set(view, clone2);
|
|
263
|
+
return clone2;
|
|
264
|
+
}
|
|
265
|
+
const typed = view;
|
|
266
|
+
const buf = _deepClone(typed.buffer, seen);
|
|
267
|
+
const clone = new typed.constructor(buf, typed.byteOffset, typed.length);
|
|
268
|
+
seen.set(view, clone);
|
|
269
|
+
return clone;
|
|
270
|
+
}
|
|
271
|
+
__name(cloneArrayBufferView, "cloneArrayBufferView");
|
|
272
|
+
function cloneArrayBuffer(buf, seen) {
|
|
273
|
+
const result = buf.slice(0);
|
|
274
|
+
seen.set(buf, result);
|
|
275
|
+
return result;
|
|
276
|
+
}
|
|
277
|
+
__name(cloneArrayBuffer, "cloneArrayBuffer");
|
|
278
|
+
function cloneObject(obj, seen) {
|
|
279
|
+
const result = Object.create(Object.getPrototypeOf(obj));
|
|
280
|
+
seen.set(obj, result);
|
|
281
|
+
for (const key of Reflect.ownKeys(obj)) {
|
|
282
|
+
const desc = Object.getOwnPropertyDescriptor(obj, key);
|
|
283
|
+
if (desc?.get || desc?.set) {
|
|
284
|
+
Object.defineProperty(result, key, {
|
|
285
|
+
get: desc.get,
|
|
286
|
+
set: desc.set,
|
|
287
|
+
enumerable: desc.enumerable,
|
|
288
|
+
configurable: desc.configurable
|
|
289
|
+
});
|
|
290
|
+
} else {
|
|
291
|
+
Object.defineProperty(result, key, {
|
|
292
|
+
value: _deepClone(obj[key], seen),
|
|
293
|
+
writable: desc?.writable ?? true,
|
|
294
|
+
enumerable: desc?.enumerable ?? true,
|
|
295
|
+
configurable: desc?.configurable ?? true
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
301
|
+
__name(cloneObject, "cloneObject");
|
|
302
|
+
function deepClone(value) {
|
|
303
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
304
|
+
return _deepClone(value, seen);
|
|
305
|
+
}
|
|
306
|
+
__name(deepClone, "deepClone");
|
|
307
|
+
function deepFreeze(obj) {
|
|
308
|
+
Object.freeze(obj);
|
|
309
|
+
for (const key of Reflect.ownKeys(obj)) {
|
|
310
|
+
const val = obj[key];
|
|
311
|
+
if (isObject(val) && !Object.isFrozen(val)) {
|
|
312
|
+
deepFreeze(val);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return obj;
|
|
316
|
+
}
|
|
317
|
+
__name(deepFreeze, "deepFreeze");
|
|
318
|
+
function getAllPaths(obj, parentPath = "") {
|
|
319
|
+
if (!isObject(obj)) return [];
|
|
320
|
+
return Object.entries(obj).flatMap(([key, value]) => {
|
|
321
|
+
const currentPath = parentPath ? `${parentPath}.${key}` : key;
|
|
322
|
+
if (isObject(value)) {
|
|
323
|
+
return [
|
|
324
|
+
currentPath,
|
|
325
|
+
...getAllPaths(value, currentPath)
|
|
326
|
+
];
|
|
327
|
+
}
|
|
328
|
+
return [
|
|
329
|
+
currentPath
|
|
330
|
+
];
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
__name(getAllPaths, "getAllPaths");
|
|
334
|
+
function invert(obj) {
|
|
335
|
+
if (!isObject(obj)) return {};
|
|
336
|
+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [
|
|
337
|
+
String(v),
|
|
338
|
+
k
|
|
339
|
+
]));
|
|
340
|
+
}
|
|
341
|
+
__name(invert, "invert");
|
|
342
|
+
function invertBy(obj, keyFn) {
|
|
343
|
+
if (!isObject(obj)) return {};
|
|
344
|
+
const result = {};
|
|
345
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
346
|
+
const newKey = keyFn(value);
|
|
347
|
+
if (!result[newKey]) result[newKey] = [];
|
|
348
|
+
result[newKey].push(key);
|
|
349
|
+
}
|
|
350
|
+
return result;
|
|
351
|
+
}
|
|
352
|
+
__name(invertBy, "invertBy");
|
|
353
|
+
function transform(obj, fn) {
|
|
354
|
+
if (!isObject(obj)) return {};
|
|
355
|
+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [
|
|
356
|
+
k,
|
|
357
|
+
fn(v, k)
|
|
358
|
+
]));
|
|
359
|
+
}
|
|
360
|
+
__name(transform, "transform");
|
|
361
|
+
|
|
362
|
+
exports.deepClone = deepClone;
|
|
363
|
+
exports.deepFreeze = deepFreeze;
|
|
364
|
+
exports.deepObjMerge = deepObjMerge;
|
|
365
|
+
exports.filterObject = filterObject;
|
|
366
|
+
exports.flattenObject = flattenObject;
|
|
367
|
+
exports.getAllPaths = getAllPaths;
|
|
368
|
+
exports.getValueByPath = getValueByPath;
|
|
369
|
+
exports.invert = invert;
|
|
370
|
+
exports.invertBy = invertBy;
|
|
371
|
+
exports.isEqual = isEqual;
|
|
372
|
+
exports.isObjEmpty = isObjEmpty;
|
|
373
|
+
exports.isObject = isObject;
|
|
374
|
+
exports.isPlainObject = isPlainObject;
|
|
375
|
+
exports.mapObject = mapObject;
|
|
376
|
+
exports.omit = omit;
|
|
377
|
+
exports.pick = pick;
|
|
378
|
+
exports.setValueByPath = setValueByPath;
|
|
379
|
+
exports.transform = transform;
|
|
@@ -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,13 +22,20 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Safely checks if a value is an object (not null, not array).
|
|
27
|
+
*
|
|
28
|
+
* @param {unknown} value - Value to check.
|
|
29
|
+
* @returns {boolean} True if value is a non-null, non-array object.
|
|
30
|
+
*/
|
|
31
|
+
declare function isObject(value: unknown): value is Record<string, any>;
|
|
25
32
|
/**
|
|
26
33
|
* Checks whether the object has no own enumerable properties.
|
|
27
34
|
*
|
|
28
35
|
* @param {Record<any, any>} obj - The object to check.
|
|
29
36
|
* @returns {boolean} True if the object is empty, false otherwise.
|
|
30
37
|
*/
|
|
31
|
-
|
|
38
|
+
declare function isObjEmpty(obj: Record<any, any>): boolean;
|
|
32
39
|
/**
|
|
33
40
|
* Returns a new object with only the specified keys picked.
|
|
34
41
|
*
|
|
@@ -38,7 +45,7 @@ export declare function isObjEmpty(obj: Record<any, any>): boolean;
|
|
|
38
45
|
* @param {K[]} keys - Keys to pick from the object.
|
|
39
46
|
* @returns {Pick<T, K>} New object with picked keys.
|
|
40
47
|
*/
|
|
41
|
-
|
|
48
|
+
declare function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
|
|
42
49
|
/**
|
|
43
50
|
* Returns a new object with the specified keys omitted.
|
|
44
51
|
*
|
|
@@ -48,32 +55,33 @@ export declare function pick<T extends object, K extends keyof T>(obj: T, keys:
|
|
|
48
55
|
* @param {K[]} keys - Keys to omit from the object.
|
|
49
56
|
* @returns {Omit<T, K>} New object without omitted keys.
|
|
50
57
|
*/
|
|
51
|
-
|
|
58
|
+
declare function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
|
|
52
59
|
/**
|
|
53
|
-
* Deeply merges multiple
|
|
60
|
+
* Deeply merges multiple sources into the target object (mutates and returns the target, not pure).
|
|
61
|
+
* Uses the project’s existing `deepClone` to handle all cloning and circular references.
|
|
54
62
|
*
|
|
55
63
|
* @template T
|
|
56
|
-
* @param {T} target - The object to merge into (will be mutated)
|
|
57
|
-
* @param {...any[]} sources - The objects to merge from
|
|
58
|
-
* @returns {T} The merged object (same as target)
|
|
64
|
+
* @param {T} target - The object to merge into (will be mutated)
|
|
65
|
+
* @param {...any[]} sources - The objects to merge from
|
|
66
|
+
* @returns {T} The merged object (same reference as target)
|
|
59
67
|
*/
|
|
60
|
-
|
|
68
|
+
declare function deepObjMerge<T extends object>(target: T, ...sources: any[]): T;
|
|
61
69
|
/**
|
|
62
70
|
* Checks if a value is a plain object (i.e., not an array, null, or a built-in object).
|
|
63
71
|
*
|
|
64
72
|
* @param value - The value to check.
|
|
65
73
|
* @returns True if the value is a plain object, false otherwise.
|
|
66
74
|
*/
|
|
67
|
-
|
|
75
|
+
declare function isPlainObject(value: any): value is Record<string, any>;
|
|
68
76
|
/**
|
|
69
|
-
* Flattens a nested object using dot notation for keys (e.g., `{a: {b: 1}}`
|
|
77
|
+
* Flattens a nested object using dot notation for keys (e.g., `{a: {b: 1}}` -> `{ "a.b": 1 }`).
|
|
70
78
|
*
|
|
71
79
|
* @template T
|
|
72
80
|
* @param {T} obj - The object to flatten.
|
|
73
81
|
* @param {string} [prefix=""] - Optional prefix for nested keys (used internally).
|
|
74
82
|
* @returns {Record<string, any>} A new object with flattened keys.
|
|
75
83
|
*/
|
|
76
|
-
|
|
84
|
+
declare function flattenObject<T extends Record<string, any>>(obj: T, prefix?: string): Record<string, any>;
|
|
77
85
|
/**
|
|
78
86
|
* Safely gets the value of a deeply nested key in an object using dot/bracket notation path.
|
|
79
87
|
*
|
|
@@ -82,7 +90,7 @@ export declare function flattenObject<T extends Record<string, any>>(obj: T, pre
|
|
|
82
90
|
* @param {string} path - String path using dot and/or bracket notation (e.g., 'user.friends[0].name').
|
|
83
91
|
* @returns {any} The value at the given path, or undefined if not found.
|
|
84
92
|
*/
|
|
85
|
-
|
|
93
|
+
declare function getValueByPath<T extends object>(obj: T, path: string): any;
|
|
86
94
|
/**
|
|
87
95
|
* Sets a value at a deeply nested key in an object using dot/bracket notation path.
|
|
88
96
|
* Secured against prototype pollution.
|
|
@@ -91,9 +99,9 @@ export declare function getValueByPath<T extends object>(obj: T, path: string):
|
|
|
91
99
|
* @param {T} obj - The object to modify.
|
|
92
100
|
* @param {string} path - Dot/bracket notation path (e.g., "user.friends[0].name").
|
|
93
101
|
* @param {any} value - Value to assign.
|
|
94
|
-
* @returns {T} The
|
|
102
|
+
* @returns {T} The new object with the value set (original object is not mutated).
|
|
95
103
|
*/
|
|
96
|
-
|
|
104
|
+
declare function setValueByPath<T extends object>(obj: T, path: string, value: any): T;
|
|
97
105
|
/**
|
|
98
106
|
* Performs a deep equality check between two objects.
|
|
99
107
|
*
|
|
@@ -101,7 +109,7 @@ export declare function setValueByPath<T extends object>(obj: T, path: string, v
|
|
|
101
109
|
* @param {any} b - Second value to compare.
|
|
102
110
|
* @returns {boolean} True if values are deeply equal.
|
|
103
111
|
*/
|
|
104
|
-
|
|
112
|
+
declare function isEqual(a: any, b: any): boolean;
|
|
105
113
|
/**
|
|
106
114
|
* Filters object properties based on a predicate function.
|
|
107
115
|
*
|
|
@@ -110,7 +118,7 @@ export declare function isEqual(a: any, b: any): boolean;
|
|
|
110
118
|
* @param {(value: any, key: string, obj: T) => boolean} predicate - Filter function.
|
|
111
119
|
* @returns {Partial<T>} New object with filtered properties.
|
|
112
120
|
*/
|
|
113
|
-
|
|
121
|
+
declare function filterObject<T extends object>(obj: T, predicate: (value: any, key: string, obj: T) => boolean): Partial<T>;
|
|
114
122
|
/**
|
|
115
123
|
* Maps object values to new values using a mapping function.
|
|
116
124
|
*
|
|
@@ -120,16 +128,17 @@ export declare function filterObject<T extends object>(obj: T, predicate: (value
|
|
|
120
128
|
* @param {(value: any, key: string, obj: T) => U} mapFn - Mapping function.
|
|
121
129
|
* @returns {Record<keyof T, U>} New object with mapped values.
|
|
122
130
|
*/
|
|
123
|
-
|
|
131
|
+
declare function mapObject<T extends object, U>(obj: T, mapFn: (value: any, key: string, obj: T) => U): Record<keyof T, U>;
|
|
124
132
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
133
|
+
* Deeply clones any value.
|
|
134
|
+
* Preserves functions and symbols by reference.
|
|
135
|
+
* Handles circular references using `WeakMap`.
|
|
127
136
|
*
|
|
128
|
-
* @template T
|
|
129
|
-
* @param
|
|
130
|
-
* @returns
|
|
137
|
+
* @template T The type of the value being cloned.
|
|
138
|
+
* @param value The value to deeply clone.
|
|
139
|
+
* @returns A fully deep-cloned copy of the input.
|
|
131
140
|
*/
|
|
132
|
-
|
|
141
|
+
declare function deepClone<T>(value: T): T;
|
|
133
142
|
/**
|
|
134
143
|
* Recursively freezes an object and all its properties.
|
|
135
144
|
* Makes an object immutable.
|
|
@@ -138,14 +147,7 @@ export declare function deepClone<T>(value: T): T;
|
|
|
138
147
|
* @param {T} obj - The object to freeze.
|
|
139
148
|
* @returns {Readonly<T>} The frozen object.
|
|
140
149
|
*/
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Safely checks if a value is an object (not null, not array).
|
|
144
|
-
*
|
|
145
|
-
* @param {unknown} value - Value to check.
|
|
146
|
-
* @returns {boolean} True if value is a non-null, non-array object.
|
|
147
|
-
*/
|
|
148
|
-
export declare function isObject(value: unknown): value is Record<string, any>;
|
|
150
|
+
declare function deepFreeze<T extends object>(obj: T): Readonly<T>;
|
|
149
151
|
/**
|
|
150
152
|
* Gets all paths in an object using dot notation.
|
|
151
153
|
*
|
|
@@ -153,4 +155,42 @@ export declare function isObject(value: unknown): value is Record<string, any>;
|
|
|
153
155
|
* @param {string} [parentPath=""] - Internal param for recursion.
|
|
154
156
|
* @returns {string[]} Array of all paths in dot notation.
|
|
155
157
|
*/
|
|
156
|
-
|
|
158
|
+
declare function getAllPaths(obj: Record<string, any>, parentPath?: string): string[];
|
|
159
|
+
/**
|
|
160
|
+
* Inverts an object's keys and values.
|
|
161
|
+
*
|
|
162
|
+
* @template T
|
|
163
|
+
* @param {T} obj - The object to invert.
|
|
164
|
+
* @returns {Record<string, string>} Inverted object.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* invert({ a: 'x', b: 'y' }); // { x: 'a', y: 'b' }
|
|
168
|
+
*/
|
|
169
|
+
declare function invert<T extends Record<string, any>>(obj: T): Record<string, string>;
|
|
170
|
+
/**
|
|
171
|
+
* Inverts an object using a function to generate keys from values.
|
|
172
|
+
*
|
|
173
|
+
* @template T
|
|
174
|
+
* @param {T} obj - The object to invert.
|
|
175
|
+
* @param {(value: any) => string} keyFn - Function to generate new keys.
|
|
176
|
+
* @returns {Record<string, string[]>} Inverted object with arrays of keys.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* invertBy({ a: 1, b: 2, c: 1 }, v => String(v)); // { '1': ['a', 'c'], '2': ['b'] }
|
|
180
|
+
*/
|
|
181
|
+
declare function invertBy<T extends Record<string, any>>(obj: T, keyFn: (value: any) => string): Record<string, string[]>;
|
|
182
|
+
/**
|
|
183
|
+
* Transforms object values using a mapping function.
|
|
184
|
+
*
|
|
185
|
+
* @template T
|
|
186
|
+
* @template U
|
|
187
|
+
* @param {T} obj - The source object.
|
|
188
|
+
* @param {(value: any, key: string) => U} fn - Transformation function.
|
|
189
|
+
* @returns {Record<string, U>} Object with transformed values.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* transform({ a: 1, b: 2 }, v => v * 2); // { a: 2, b: 4 }
|
|
193
|
+
*/
|
|
194
|
+
declare function transform<T extends Record<string, any>, U>(obj: T, fn: (value: any, key: string) => U): Record<string, U>;
|
|
195
|
+
|
|
196
|
+
export { deepClone, deepFreeze, deepObjMerge, filterObject, flattenObject, getAllPaths, getValueByPath, invert, invertBy, isEqual, isObjEmpty, isObject, isPlainObject, mapObject, omit, pick, setValueByPath, transform };
|