@catbee/utils 2.0.0-next.0 → 2.0.0-next.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 +52 -16
- package/array/index.cjs +180 -71
- package/array/index.d.ts +293 -1
- package/array/index.mjs +171 -72
- package/async/index.cjs +92 -36
- package/async/index.d.ts +275 -1
- package/async/index.mjs +92 -36
- package/cache/index.cjs +1 -1
- package/cache/index.d.ts +155 -1
- package/cache/index.mjs +2 -2
- package/config/index.cjs +78 -64
- package/config/index.d.ts +64 -2
- package/config/index.mjs +76 -64
- package/context-store/index.d.ts +192 -1
- package/crypto/index.d.ts +163 -1
- package/date/index.cjs +46 -1
- package/date/index.d.ts +190 -1
- package/date/index.mjs +45 -2
- package/decorators/index.cjs +1156 -18
- package/decorators/index.d.ts +684 -1
- package/decorators/index.mjs +1156 -18
- package/dir/index.cjs +4 -3
- package/dir/index.d.ts +195 -1
- package/dir/index.mjs +4 -3
- package/env/index.cjs +10 -26
- package/env/index.d.ts +379 -1
- package/env/index.mjs +10 -26
- package/exception/index.d.ts +232 -1
- package/fs/index.cjs +70 -36
- package/fs/index.d.ts +205 -1
- package/fs/index.mjs +64 -34
- package/http-status-codes/index.d.ts +267 -1
- package/id/index.d.ts +37 -1
- package/index.cjs +3 -3
- package/index.d.ts +1 -1
- package/index.mjs +1 -1
- package/logger/index.cjs +11 -11
- package/logger/index.d.ts +189 -1
- package/logger/index.mjs +12 -12
- package/middleware/index.d.ts +103 -1
- package/obj/index.cjs +150 -162
- package/obj/index.d.ts +136 -1
- package/obj/index.mjs +150 -162
- package/package.json +11 -11
- package/performance/index.cjs +2 -2
- package/performance/index.d.ts +138 -1
- package/performance/index.mjs +2 -2
- package/request/index.cjs +1 -1
- package/request/index.d.ts +241 -2
- package/request/index.mjs +1 -1
- package/response/index.d.ts +318 -2
- package/server/index.cjs +27 -23
- package/server/index.d.ts +785 -4
- package/server/index.mjs +28 -23
- package/stream/index.d.ts +90 -1
- package/string/index.d.ts +102 -1
- package/type/index.cjs +1 -1
- package/type/index.d.ts +107 -1
- package/type/index.mjs +1 -1
- package/types/index.d.ts +774 -4
- package/url/index.cjs +2 -4
- package/url/index.d.ts +142 -1
- package/url/index.mjs +2 -4
- package/{validate → validation}/index.cjs +89 -42
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +32 -23
- package/{validate → validation}/index.mjs +85 -42
- 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/decorators.utils.d.ts +0 -705
- package/dir/dir.utils.d.ts +0 -216
- 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/obj.utils.d.ts +0 -156
- 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/obj/index.cjs
CHANGED
|
@@ -28,8 +28,12 @@ var __defProp = Object.defineProperty;
|
|
|
28
28
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
29
|
|
|
30
30
|
// src/obj/obj.utils.ts
|
|
31
|
+
function isObject(value) {
|
|
32
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
33
|
+
}
|
|
34
|
+
__name(isObject, "isObject");
|
|
31
35
|
function isObjEmpty(obj) {
|
|
32
|
-
return
|
|
36
|
+
return isObject(obj) && Object.keys(obj).length === 0;
|
|
33
37
|
}
|
|
34
38
|
__name(isObjEmpty, "isObjEmpty");
|
|
35
39
|
function pick(obj, keys) {
|
|
@@ -44,117 +48,38 @@ function omit(obj, keys) {
|
|
|
44
48
|
}
|
|
45
49
|
__name(omit, "omit");
|
|
46
50
|
function deepObjMerge(target, ...sources) {
|
|
47
|
-
if (!sources.length) return target;
|
|
48
51
|
const seen = /* @__PURE__ */ new WeakMap();
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (s
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (value === null || typeof value !== "object") return value;
|
|
57
|
-
if (seen.has(value)) return seen.get(value);
|
|
58
|
-
if (value instanceof Date) {
|
|
59
|
-
return new Date(value.getTime());
|
|
60
|
-
}
|
|
61
|
-
if (Array.isArray(value)) {
|
|
62
|
-
const clone2 = [];
|
|
63
|
-
seen.set(value, clone2);
|
|
64
|
-
value.forEach((item, i) => clone2[i] = deepClone2(item));
|
|
65
|
-
return clone2;
|
|
66
|
-
}
|
|
67
|
-
if (value instanceof Set) {
|
|
68
|
-
const clone2 = /* @__PURE__ */ new Set();
|
|
69
|
-
seen.set(value, clone2);
|
|
70
|
-
value.forEach((item) => clone2.add(deepClone2(item)));
|
|
71
|
-
return clone2;
|
|
72
|
-
}
|
|
73
|
-
if (value instanceof Map) {
|
|
74
|
-
const clone2 = /* @__PURE__ */ new Map();
|
|
75
|
-
seen.set(value, clone2);
|
|
76
|
-
value.forEach((val, key) => clone2.set(key, deepClone2(val)));
|
|
77
|
-
return clone2;
|
|
78
|
-
}
|
|
79
|
-
if (value instanceof RegExp) {
|
|
80
|
-
return new RegExp(value.source, value.flags);
|
|
52
|
+
const mergeInto = /* @__PURE__ */ __name((t, s) => {
|
|
53
|
+
if (s === void 0) return t;
|
|
54
|
+
if (typeof s === "function" || typeof s === "symbol") return s;
|
|
55
|
+
if (Array.isArray(s)) {
|
|
56
|
+
const cloned = deepClone(s);
|
|
57
|
+
seen.set(s, cloned);
|
|
58
|
+
return cloned;
|
|
81
59
|
}
|
|
82
|
-
if (
|
|
83
|
-
return
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const typedArrayCtors = [
|
|
89
|
-
Int8Array,
|
|
90
|
-
Uint8Array,
|
|
91
|
-
Uint8ClampedArray,
|
|
92
|
-
Int16Array,
|
|
93
|
-
Uint16Array,
|
|
94
|
-
Int32Array,
|
|
95
|
-
Uint32Array,
|
|
96
|
-
Float32Array,
|
|
97
|
-
Float64Array,
|
|
98
|
-
BigInt64Array,
|
|
99
|
-
BigUint64Array
|
|
100
|
-
].filter(Boolean);
|
|
101
|
-
const typedArrayCtorSet = new Set(typedArrayCtors);
|
|
102
|
-
if (typeof ctor === "function" && typedArrayCtorSet.has(ctor)) {
|
|
103
|
-
const buffer = typed.buffer && typeof typed.buffer.slice === "function" ? typed.buffer.slice(0) : typed.buffer;
|
|
104
|
-
const clone2 = new ctor(buffer, typed.byteOffset, typed.length);
|
|
105
|
-
seen.set(value, clone2);
|
|
106
|
-
return clone2;
|
|
107
|
-
} else if (typeof typed.slice === "function") {
|
|
108
|
-
const clone2 = typed.slice();
|
|
109
|
-
seen.set(value, clone2);
|
|
110
|
-
return clone2;
|
|
111
|
-
} else {
|
|
112
|
-
throw new Error("Cannot deep clone unknown TypedArray type: " + (typed && typed.constructor && typed.constructor.name));
|
|
60
|
+
if (s && typeof s === "object") {
|
|
61
|
+
if (seen.has(s)) return seen.get(s);
|
|
62
|
+
if (s instanceof Date || s instanceof Map || s instanceof Set || s instanceof RegExp || s instanceof ArrayBuffer || ArrayBuffer.isView(s)) {
|
|
63
|
+
const cloned = deepClone(s);
|
|
64
|
+
seen.set(s, cloned);
|
|
65
|
+
return cloned;
|
|
113
66
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
seen.set(value, clone);
|
|
117
|
-
Reflect.ownKeys(value).forEach((key) => {
|
|
118
|
-
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
119
|
-
if (descriptor.get || descriptor.set) {
|
|
120
|
-
Object.defineProperty(clone, key, descriptor);
|
|
121
|
-
} else {
|
|
122
|
-
clone[key] = deepClone2(value[key]);
|
|
67
|
+
if (!t || typeof t !== "object") {
|
|
68
|
+
t = Object.create(Object.getPrototypeOf(s));
|
|
123
69
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const sourceValue = source[key];
|
|
131
|
-
if (sourceValue === void 0) return;
|
|
132
|
-
if (sourceValue === null || typeof sourceValue !== "object") {
|
|
133
|
-
target2[key] = sourceValue;
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (Array.isArray(sourceValue)) {
|
|
137
|
-
target2[key] = deepClone2(sourceValue);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
if (sourceValue instanceof Date || sourceValue instanceof RegExp || sourceValue instanceof Set || sourceValue instanceof Map || sourceValue instanceof ArrayBuffer || ArrayBuffer.isView(sourceValue) && !(sourceValue instanceof DataView)) {
|
|
141
|
-
target2[key] = deepClone2(sourceValue);
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
|
|
145
|
-
for (const k of Reflect.ownKeys(sourceValue)) {
|
|
146
|
-
mergeProp(targetValue, sourceValue, k);
|
|
70
|
+
seen.set(s, t);
|
|
71
|
+
for (const key of Reflect.ownKeys(s)) {
|
|
72
|
+
if (key === "__proto__") continue;
|
|
73
|
+
const sv = s[key];
|
|
74
|
+
if (sv === void 0) continue;
|
|
75
|
+
t[key] = mergeInto(t[key], sv);
|
|
147
76
|
}
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
target2[key] = deepClone2(sourceValue);
|
|
151
|
-
}
|
|
152
|
-
__name(mergeProp, "mergeProp");
|
|
153
|
-
for (const source of sources) {
|
|
154
|
-
if (source == null) continue;
|
|
155
|
-
for (const key of Reflect.ownKeys(source)) {
|
|
156
|
-
mergeProp(target, source, key);
|
|
77
|
+
return t;
|
|
157
78
|
}
|
|
79
|
+
return s;
|
|
80
|
+
}, "mergeInto");
|
|
81
|
+
for (const s of sources) {
|
|
82
|
+
target = mergeInto(target, s);
|
|
158
83
|
}
|
|
159
84
|
return target;
|
|
160
85
|
}
|
|
@@ -169,6 +94,7 @@ __name(isPlainObject, "isPlainObject");
|
|
|
169
94
|
function flattenObject(obj, prefix = "") {
|
|
170
95
|
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
171
96
|
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
97
|
+
if (key === "__proto__") return acc;
|
|
172
98
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
173
99
|
Object.assign(acc, flattenObject(value, newKey));
|
|
174
100
|
} else {
|
|
@@ -184,63 +110,52 @@ function getValueByPath(obj, path) {
|
|
|
184
110
|
return parts.reduce((acc, key) => acc?.[key], obj);
|
|
185
111
|
}
|
|
186
112
|
__name(getValueByPath, "getValueByPath");
|
|
113
|
+
function ensureNextContainer(curr, nextIsIndex) {
|
|
114
|
+
if (curr === void 0 || typeof curr !== "object" || curr === null) {
|
|
115
|
+
return nextIsIndex ? [] : /* @__PURE__ */ Object.create(null);
|
|
116
|
+
}
|
|
117
|
+
return curr;
|
|
118
|
+
}
|
|
119
|
+
__name(ensureNextContainer, "ensureNextContainer");
|
|
187
120
|
function setValueByPath(obj, path, value) {
|
|
188
121
|
if (!obj || typeof obj !== "object") return obj;
|
|
122
|
+
const clone = deepClone(obj);
|
|
189
123
|
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
190
|
-
if (parts.length === 0) return
|
|
124
|
+
if (parts.length === 0) return clone;
|
|
191
125
|
const forbiddenKeys = /* @__PURE__ */ new Set([
|
|
192
126
|
"__proto__",
|
|
193
127
|
"prototype",
|
|
194
128
|
"constructor"
|
|
195
129
|
]);
|
|
196
|
-
let current =
|
|
130
|
+
let current = clone;
|
|
197
131
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
198
132
|
const key = parts[i];
|
|
199
|
-
if (forbiddenKeys.has(key))
|
|
200
|
-
return obj;
|
|
201
|
-
}
|
|
133
|
+
if (forbiddenKeys.has(key)) return clone;
|
|
202
134
|
const nextKey = parts[i + 1];
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
current[key] = isArrayIndex ? [] : /* @__PURE__ */ Object.create(null);
|
|
206
|
-
} else if (typeof current[key] !== "object" || current[key] === null) {
|
|
207
|
-
current[key] = isArrayIndex ? [] : /* @__PURE__ */ Object.create(null);
|
|
208
|
-
}
|
|
135
|
+
const nextIsIndex = !Number.isNaN(Number(nextKey));
|
|
136
|
+
current[key] = ensureNextContainer(current[key], nextIsIndex);
|
|
209
137
|
current = current[key];
|
|
210
138
|
}
|
|
211
|
-
const finalKey = parts
|
|
212
|
-
if (forbiddenKeys.has(finalKey))
|
|
213
|
-
return obj;
|
|
214
|
-
}
|
|
139
|
+
const finalKey = parts.at(-1);
|
|
140
|
+
if (forbiddenKeys.has(finalKey)) return clone;
|
|
215
141
|
current[finalKey] = value;
|
|
216
|
-
return
|
|
142
|
+
return clone;
|
|
217
143
|
}
|
|
218
144
|
__name(setValueByPath, "setValueByPath");
|
|
219
145
|
function isEqual(a, b) {
|
|
220
146
|
if (a === b) return true;
|
|
221
|
-
if (a == null || b == null || typeof a !== "object" || typeof b !== "object")
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (a instanceof
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (a instanceof
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (!isEqual(a[i], b[i])) return false;
|
|
234
|
-
}
|
|
235
|
-
return true;
|
|
236
|
-
}
|
|
237
|
-
if (Array.isArray(a) || Array.isArray(b)) {
|
|
238
|
-
return false;
|
|
239
|
-
}
|
|
240
|
-
const keysA = Object.keys(a);
|
|
241
|
-
const keysB = Object.keys(b);
|
|
242
|
-
if (keysA.length !== keysB.length) return false;
|
|
243
|
-
return keysA.every((key) => Object.prototype.hasOwnProperty.call(b, key) && isEqual(a[key], b[key]));
|
|
147
|
+
if (a == null || b == null || typeof a !== "object" || typeof b !== "object") return false;
|
|
148
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
149
|
+
if (a instanceof RegExp && b instanceof RegExp) return a.source === b.source && a.flags === b.flags;
|
|
150
|
+
if (a instanceof Set && b instanceof Set) return a.size === b.size && [
|
|
151
|
+
...a
|
|
152
|
+
].every((v) => b.has(v));
|
|
153
|
+
if (a instanceof Map && b instanceof Map) return a.size === b.size && [
|
|
154
|
+
...a
|
|
155
|
+
].every(([k, v]) => isEqual(v, b.get(k)));
|
|
156
|
+
if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) return isEqual(new Uint8Array(a), new Uint8Array(b));
|
|
157
|
+
if (Array.isArray(a) && Array.isArray(b)) return a.length === b.length && a.every((v, i) => isEqual(v, b[i]));
|
|
158
|
+
return Object.keys(a).length === Object.keys(b).length && Object.keys(a).every((key) => isEqual(a[key], b[key]));
|
|
244
159
|
}
|
|
245
160
|
__name(isEqual, "isEqual");
|
|
246
161
|
function filterObject(obj, predicate) {
|
|
@@ -254,35 +169,108 @@ function mapObject(obj, mapFn) {
|
|
|
254
169
|
]));
|
|
255
170
|
}
|
|
256
171
|
__name(mapObject, "mapObject");
|
|
257
|
-
function
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
return globalThis.structuredClone(value);
|
|
261
|
-
}
|
|
262
|
-
} catch (_) {
|
|
172
|
+
function _deepClone(value, seen) {
|
|
173
|
+
if (typeof value === "function" || typeof value === "symbol") {
|
|
174
|
+
return value;
|
|
263
175
|
}
|
|
264
|
-
|
|
265
|
-
return JSON.parse(JSON.stringify(value));
|
|
266
|
-
} catch (_) {
|
|
176
|
+
if (value === null || typeof value !== "object") {
|
|
267
177
|
return value;
|
|
268
178
|
}
|
|
179
|
+
if (seen.has(value)) {
|
|
180
|
+
return seen.get(value);
|
|
181
|
+
}
|
|
182
|
+
let result;
|
|
183
|
+
if (Array.isArray(value)) {
|
|
184
|
+
result = [];
|
|
185
|
+
seen.set(value, result);
|
|
186
|
+
for (const item of value) {
|
|
187
|
+
result.push(_deepClone(item, seen));
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
if (value instanceof Date) {
|
|
192
|
+
result = new Date(value.getTime());
|
|
193
|
+
seen.set(value, result);
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
if (value instanceof Map) {
|
|
197
|
+
result = /* @__PURE__ */ new Map();
|
|
198
|
+
seen.set(value, result);
|
|
199
|
+
for (const [k, v] of value) {
|
|
200
|
+
result.set(_deepClone(k, seen), _deepClone(v, seen));
|
|
201
|
+
}
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
if (value instanceof Set) {
|
|
205
|
+
result = /* @__PURE__ */ new Set();
|
|
206
|
+
seen.set(value, result);
|
|
207
|
+
for (const v of value) {
|
|
208
|
+
result.add(_deepClone(v, seen));
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
if (value instanceof RegExp) {
|
|
213
|
+
result = new RegExp(value.source, value.flags);
|
|
214
|
+
seen.set(value, result);
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
if (ArrayBuffer.isView(value)) {
|
|
218
|
+
if (value instanceof DataView) {
|
|
219
|
+
const buf2 = _deepClone(value.buffer, seen);
|
|
220
|
+
const clone2 = new DataView(buf2, value.byteOffset, value.byteLength);
|
|
221
|
+
seen.set(value, clone2);
|
|
222
|
+
return clone2;
|
|
223
|
+
}
|
|
224
|
+
const typed = value;
|
|
225
|
+
const buf = _deepClone(typed.buffer, seen);
|
|
226
|
+
const clone = new typed.constructor(buf, typed.byteOffset, typed.length);
|
|
227
|
+
seen.set(value, clone);
|
|
228
|
+
return clone;
|
|
229
|
+
}
|
|
230
|
+
if (value instanceof ArrayBuffer) {
|
|
231
|
+
result = value.slice(0);
|
|
232
|
+
seen.set(value, result);
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
result = Object.create(Object.getPrototypeOf(value));
|
|
236
|
+
seen.set(value, result);
|
|
237
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
238
|
+
const desc = Object.getOwnPropertyDescriptor(value, key);
|
|
239
|
+
if (desc?.get || desc?.set) {
|
|
240
|
+
Object.defineProperty(result, key, {
|
|
241
|
+
get: desc.get,
|
|
242
|
+
set: desc.set,
|
|
243
|
+
enumerable: desc.enumerable,
|
|
244
|
+
configurable: desc.configurable
|
|
245
|
+
});
|
|
246
|
+
} else {
|
|
247
|
+
Object.defineProperty(result, key, {
|
|
248
|
+
value: _deepClone(value[key], seen),
|
|
249
|
+
writable: desc?.writable ?? true,
|
|
250
|
+
enumerable: desc?.enumerable ?? true,
|
|
251
|
+
configurable: desc?.configurable ?? true
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
__name(_deepClone, "_deepClone");
|
|
258
|
+
function deepClone(value) {
|
|
259
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
260
|
+
return _deepClone(value, seen);
|
|
269
261
|
}
|
|
270
262
|
__name(deepClone, "deepClone");
|
|
271
263
|
function deepFreeze(obj) {
|
|
272
264
|
Object.freeze(obj);
|
|
273
|
-
for (const
|
|
274
|
-
const
|
|
275
|
-
if (
|
|
276
|
-
deepFreeze(
|
|
265
|
+
for (const key of Reflect.ownKeys(obj)) {
|
|
266
|
+
const val = obj[key];
|
|
267
|
+
if (isObject(val) && !Object.isFrozen(val)) {
|
|
268
|
+
deepFreeze(val);
|
|
277
269
|
}
|
|
278
270
|
}
|
|
279
271
|
return obj;
|
|
280
272
|
}
|
|
281
273
|
__name(deepFreeze, "deepFreeze");
|
|
282
|
-
function isObject(value) {
|
|
283
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
284
|
-
}
|
|
285
|
-
__name(isObject, "isObject");
|
|
286
274
|
function getAllPaths(obj, parentPath = "") {
|
|
287
275
|
if (!isObject(obj)) return [];
|
|
288
276
|
return Object.entries(obj).flatMap(([key, value]) => {
|
package/obj/index.d.ts
CHANGED
|
@@ -22,4 +22,139 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
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>;
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether the object has no own enumerable properties.
|
|
34
|
+
*
|
|
35
|
+
* @param {Record<any, any>} obj - The object to check.
|
|
36
|
+
* @returns {boolean} True if the object is empty, false otherwise.
|
|
37
|
+
*/
|
|
38
|
+
declare function isObjEmpty(obj: Record<any, any>): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Returns a new object with only the specified keys picked.
|
|
41
|
+
*
|
|
42
|
+
* @template T
|
|
43
|
+
* @template K
|
|
44
|
+
* @param {T} obj - The source object.
|
|
45
|
+
* @param {K[]} keys - Keys to pick from the object.
|
|
46
|
+
* @returns {Pick<T, K>} New object with picked keys.
|
|
47
|
+
*/
|
|
48
|
+
declare function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
|
|
49
|
+
/**
|
|
50
|
+
* Returns a new object with the specified keys omitted.
|
|
51
|
+
*
|
|
52
|
+
* @template T
|
|
53
|
+
* @template K
|
|
54
|
+
* @param {T} obj - The source object.
|
|
55
|
+
* @param {K[]} keys - Keys to omit from the object.
|
|
56
|
+
* @returns {Omit<T, K>} New object without omitted keys.
|
|
57
|
+
*/
|
|
58
|
+
declare function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
|
|
59
|
+
/**
|
|
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.
|
|
62
|
+
*
|
|
63
|
+
* @template T
|
|
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)
|
|
67
|
+
*/
|
|
68
|
+
declare function deepObjMerge<T extends object>(target: T, ...sources: any[]): T;
|
|
69
|
+
/**
|
|
70
|
+
* Checks if a value is a plain object (i.e., not an array, null, or a built-in object).
|
|
71
|
+
*
|
|
72
|
+
* @param value - The value to check.
|
|
73
|
+
* @returns True if the value is a plain object, false otherwise.
|
|
74
|
+
*/
|
|
75
|
+
declare function isPlainObject(value: any): value is Record<string, any>;
|
|
76
|
+
/**
|
|
77
|
+
* Flattens a nested object using dot notation for keys (e.g., `{a: {b: 1}}` -> `{ "a.b": 1 }`).
|
|
78
|
+
*
|
|
79
|
+
* @template T
|
|
80
|
+
* @param {T} obj - The object to flatten.
|
|
81
|
+
* @param {string} [prefix=""] - Optional prefix for nested keys (used internally).
|
|
82
|
+
* @returns {Record<string, any>} A new object with flattened keys.
|
|
83
|
+
*/
|
|
84
|
+
declare function flattenObject<T extends Record<string, any>>(obj: T, prefix?: string): Record<string, any>;
|
|
85
|
+
/**
|
|
86
|
+
* Safely gets the value of a deeply nested key in an object using dot/bracket notation path.
|
|
87
|
+
*
|
|
88
|
+
* @template T
|
|
89
|
+
* @param {T} obj - The object to extract from.
|
|
90
|
+
* @param {string} path - String path using dot and/or bracket notation (e.g., 'user.friends[0].name').
|
|
91
|
+
* @returns {any} The value at the given path, or undefined if not found.
|
|
92
|
+
*/
|
|
93
|
+
declare function getValueByPath<T extends object>(obj: T, path: string): any;
|
|
94
|
+
/**
|
|
95
|
+
* Sets a value at a deeply nested key in an object using dot/bracket notation path.
|
|
96
|
+
* Secured against prototype pollution.
|
|
97
|
+
*
|
|
98
|
+
* @template T
|
|
99
|
+
* @param {T} obj - The object to modify.
|
|
100
|
+
* @param {string} path - Dot/bracket notation path (e.g., "user.friends[0].name").
|
|
101
|
+
* @param {any} value - Value to assign.
|
|
102
|
+
* @returns {T} The new object with the value set (original object is not mutated).
|
|
103
|
+
*/
|
|
104
|
+
declare function setValueByPath<T extends object>(obj: T, path: string, value: any): T;
|
|
105
|
+
/**
|
|
106
|
+
* Performs a deep equality check between two objects.
|
|
107
|
+
*
|
|
108
|
+
* @param {any} a - First value to compare.
|
|
109
|
+
* @param {any} b - Second value to compare.
|
|
110
|
+
* @returns {boolean} True if values are deeply equal.
|
|
111
|
+
*/
|
|
112
|
+
declare function isEqual(a: any, b: any): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Filters object properties based on a predicate function.
|
|
115
|
+
*
|
|
116
|
+
* @template T
|
|
117
|
+
* @param {T} obj - The source object.
|
|
118
|
+
* @param {(value: any, key: string, obj: T) => boolean} predicate - Filter function.
|
|
119
|
+
* @returns {Partial<T>} New object with filtered properties.
|
|
120
|
+
*/
|
|
121
|
+
declare function filterObject<T extends object>(obj: T, predicate: (value: any, key: string, obj: T) => boolean): Partial<T>;
|
|
122
|
+
/**
|
|
123
|
+
* Maps object values to new values using a mapping function.
|
|
124
|
+
*
|
|
125
|
+
* @template T
|
|
126
|
+
* @template U
|
|
127
|
+
* @param {T} obj - The source object.
|
|
128
|
+
* @param {(value: any, key: string, obj: T) => U} mapFn - Mapping function.
|
|
129
|
+
* @returns {Record<keyof T, U>} New object with mapped values.
|
|
130
|
+
*/
|
|
131
|
+
declare function mapObject<T extends object, U>(obj: T, mapFn: (value: any, key: string, obj: T) => U): Record<keyof T, U>;
|
|
132
|
+
/**
|
|
133
|
+
* Deeply clones any value.
|
|
134
|
+
* Preserves functions and symbols by reference.
|
|
135
|
+
* Handles circular references using `WeakMap`.
|
|
136
|
+
*
|
|
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.
|
|
140
|
+
*/
|
|
141
|
+
declare function deepClone<T>(value: T): T;
|
|
142
|
+
/**
|
|
143
|
+
* Recursively freezes an object and all its properties.
|
|
144
|
+
* Makes an object immutable.
|
|
145
|
+
*
|
|
146
|
+
* @template T
|
|
147
|
+
* @param {T} obj - The object to freeze.
|
|
148
|
+
* @returns {Readonly<T>} The frozen object.
|
|
149
|
+
*/
|
|
150
|
+
declare function deepFreeze<T extends object>(obj: T): Readonly<T>;
|
|
151
|
+
/**
|
|
152
|
+
* Gets all paths in an object using dot notation.
|
|
153
|
+
*
|
|
154
|
+
* @param {Record<string, any>} obj - Object to analyze.
|
|
155
|
+
* @param {string} [parentPath=""] - Internal param for recursion.
|
|
156
|
+
* @returns {string[]} Array of all paths in dot notation.
|
|
157
|
+
*/
|
|
158
|
+
declare function getAllPaths(obj: Record<string, any>, parentPath?: string): string[];
|
|
159
|
+
|
|
160
|
+
export { deepClone, deepFreeze, deepObjMerge, filterObject, flattenObject, getAllPaths, getValueByPath, isEqual, isObjEmpty, isObject, isPlainObject, mapObject, omit, pick, setValueByPath };
|