@catbee/utils 1.1.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/LICENSE +1 -1
- package/README.md +79 -43
- package/array/index.cjs +355 -0
- package/array/index.d.ts +317 -0
- package/array/index.mjs +327 -0
- package/async/index.cjs +484 -0
- package/async/index.d.ts +299 -0
- package/async/index.mjs +463 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +179 -0
- package/cache/index.mjs +290 -0
- package/config/index.cjs +150 -0
- package/config/index.d.ts +88 -0
- package/config/index.mjs +143 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +216 -0
- package/context-store/index.mjs +261 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +187 -0
- package/crypto/index.mjs +166 -0
- package/date/index.cjs +340 -0
- package/date/index.d.ts +214 -0
- package/date/index.mjs +326 -0
- package/decorators/index.cjs +2051 -0
- package/decorators/index.d.ts +708 -0
- package/decorators/index.mjs +2010 -0
- package/dir/index.cjs +417 -0
- package/dir/index.d.ts +219 -0
- package/dir/index.mjs +390 -0
- package/env/index.cjs +745 -0
- package/env/index.d.ts +403 -0
- package/env/index.mjs +742 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +256 -0
- package/exception/index.mjs +338 -0
- package/fs/index.cjs +287 -0
- package/fs/index.d.ts +229 -0
- package/fs/index.mjs +258 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +291 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +61 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +213 -0
- package/logger/index.mjs +313 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +127 -0
- package/middleware/index.mjs +170 -0
- package/obj/index.cjs +305 -0
- package/obj/index.d.ts +160 -0
- package/obj/index.mjs +289 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +162 -0
- package/performance/index.mjs +225 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +265 -0
- package/request/index.mjs +194 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +342 -0
- package/response/index.mjs +222 -0
- package/server/index.cjs +1631 -0
- package/server/index.d.ts +809 -0
- package/server/index.mjs +1622 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +114 -0
- package/stream/index.mjs +144 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +126 -0
- package/string/index.mjs +95 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +131 -0
- package/type/index.mjs +119 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +798 -0
- package/types/index.mjs +32 -0
- package/url/index.cjs +199 -0
- package/url/index.d.ts +166 -0
- package/url/index.mjs +187 -0
- package/validation/index.cjs +259 -0
- package/validation/index.d.ts +209 -0
- package/validation/index.mjs +231 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
package/obj/index.d.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
/**
|
|
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 };
|
package/obj/index.mjs
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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/obj/obj.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
|
+
const mergeInto = /* @__PURE__ */ __name((t, s) => {
|
|
51
|
+
if (s === void 0) return t;
|
|
52
|
+
if (typeof s === "function" || typeof s === "symbol") return s;
|
|
53
|
+
if (Array.isArray(s)) {
|
|
54
|
+
const cloned = deepClone(s);
|
|
55
|
+
seen.set(s, cloned);
|
|
56
|
+
return cloned;
|
|
57
|
+
}
|
|
58
|
+
if (s && typeof s === "object") {
|
|
59
|
+
if (seen.has(s)) return seen.get(s);
|
|
60
|
+
if (s instanceof Date || s instanceof Map || s instanceof Set || s instanceof RegExp || s instanceof ArrayBuffer || ArrayBuffer.isView(s)) {
|
|
61
|
+
const cloned = deepClone(s);
|
|
62
|
+
seen.set(s, cloned);
|
|
63
|
+
return cloned;
|
|
64
|
+
}
|
|
65
|
+
if (!t || typeof t !== "object") {
|
|
66
|
+
t = Object.create(Object.getPrototypeOf(s));
|
|
67
|
+
}
|
|
68
|
+
seen.set(s, t);
|
|
69
|
+
for (const key of Reflect.ownKeys(s)) {
|
|
70
|
+
if (key === "__proto__") continue;
|
|
71
|
+
const sv = s[key];
|
|
72
|
+
if (sv === void 0) continue;
|
|
73
|
+
t[key] = mergeInto(t[key], sv);
|
|
74
|
+
}
|
|
75
|
+
return t;
|
|
76
|
+
}
|
|
77
|
+
return s;
|
|
78
|
+
}, "mergeInto");
|
|
79
|
+
for (const s of sources) {
|
|
80
|
+
target = mergeInto(target, s);
|
|
81
|
+
}
|
|
82
|
+
return target;
|
|
83
|
+
}
|
|
84
|
+
__name(deepObjMerge, "deepObjMerge");
|
|
85
|
+
function isPlainObject(value) {
|
|
86
|
+
if (typeof value !== "object" || value === null) return false;
|
|
87
|
+
if (Array.isArray(value)) return false;
|
|
88
|
+
const proto = Object.getPrototypeOf(value);
|
|
89
|
+
return proto === Object.prototype || proto === null;
|
|
90
|
+
}
|
|
91
|
+
__name(isPlainObject, "isPlainObject");
|
|
92
|
+
function flattenObject(obj, prefix = "") {
|
|
93
|
+
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
94
|
+
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
95
|
+
if (key === "__proto__") return acc;
|
|
96
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
97
|
+
Object.assign(acc, flattenObject(value, newKey));
|
|
98
|
+
} else {
|
|
99
|
+
acc[newKey] = value;
|
|
100
|
+
}
|
|
101
|
+
return acc;
|
|
102
|
+
}, {});
|
|
103
|
+
}
|
|
104
|
+
__name(flattenObject, "flattenObject");
|
|
105
|
+
function getValueByPath(obj, path) {
|
|
106
|
+
if (!obj || typeof obj !== "object") return void 0;
|
|
107
|
+
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
108
|
+
return parts.reduce((acc, key) => acc?.[key], obj);
|
|
109
|
+
}
|
|
110
|
+
__name(getValueByPath, "getValueByPath");
|
|
111
|
+
function ensureNextContainer(curr, nextIsIndex) {
|
|
112
|
+
if (curr === void 0 || typeof curr !== "object" || curr === null) {
|
|
113
|
+
return nextIsIndex ? [] : /* @__PURE__ */ Object.create(null);
|
|
114
|
+
}
|
|
115
|
+
return curr;
|
|
116
|
+
}
|
|
117
|
+
__name(ensureNextContainer, "ensureNextContainer");
|
|
118
|
+
function setValueByPath(obj, path, value) {
|
|
119
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
120
|
+
const clone = deepClone(obj);
|
|
121
|
+
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
122
|
+
if (parts.length === 0) return clone;
|
|
123
|
+
const forbiddenKeys = /* @__PURE__ */ new Set([
|
|
124
|
+
"__proto__",
|
|
125
|
+
"prototype",
|
|
126
|
+
"constructor"
|
|
127
|
+
]);
|
|
128
|
+
let current = clone;
|
|
129
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
130
|
+
const key = parts[i];
|
|
131
|
+
if (forbiddenKeys.has(key)) return clone;
|
|
132
|
+
const nextKey = parts[i + 1];
|
|
133
|
+
const nextIsIndex = !Number.isNaN(Number(nextKey));
|
|
134
|
+
current[key] = ensureNextContainer(current[key], nextIsIndex);
|
|
135
|
+
current = current[key];
|
|
136
|
+
}
|
|
137
|
+
const finalKey = parts.at(-1);
|
|
138
|
+
if (forbiddenKeys.has(finalKey)) return clone;
|
|
139
|
+
current[finalKey] = value;
|
|
140
|
+
return clone;
|
|
141
|
+
}
|
|
142
|
+
__name(setValueByPath, "setValueByPath");
|
|
143
|
+
function isEqual(a, b) {
|
|
144
|
+
if (a === b) return true;
|
|
145
|
+
if (a == null || b == null || typeof a !== "object" || typeof b !== "object") return false;
|
|
146
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
147
|
+
if (a instanceof RegExp && b instanceof RegExp) return a.source === b.source && a.flags === b.flags;
|
|
148
|
+
if (a instanceof Set && b instanceof Set) return a.size === b.size && [
|
|
149
|
+
...a
|
|
150
|
+
].every((v) => b.has(v));
|
|
151
|
+
if (a instanceof Map && b instanceof Map) return a.size === b.size && [
|
|
152
|
+
...a
|
|
153
|
+
].every(([k, v]) => isEqual(v, b.get(k)));
|
|
154
|
+
if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) return isEqual(new Uint8Array(a), new Uint8Array(b));
|
|
155
|
+
if (Array.isArray(a) && Array.isArray(b)) return a.length === b.length && a.every((v, i) => isEqual(v, b[i]));
|
|
156
|
+
return Object.keys(a).length === Object.keys(b).length && Object.keys(a).every((key) => isEqual(a[key], b[key]));
|
|
157
|
+
}
|
|
158
|
+
__name(isEqual, "isEqual");
|
|
159
|
+
function filterObject(obj, predicate) {
|
|
160
|
+
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(value, key, obj)));
|
|
161
|
+
}
|
|
162
|
+
__name(filterObject, "filterObject");
|
|
163
|
+
function mapObject(obj, mapFn) {
|
|
164
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
165
|
+
key,
|
|
166
|
+
mapFn(value, key, obj)
|
|
167
|
+
]));
|
|
168
|
+
}
|
|
169
|
+
__name(mapObject, "mapObject");
|
|
170
|
+
function _deepClone(value, seen) {
|
|
171
|
+
if (typeof value === "function" || typeof value === "symbol") {
|
|
172
|
+
return value;
|
|
173
|
+
}
|
|
174
|
+
if (value === null || typeof value !== "object") {
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
if (seen.has(value)) {
|
|
178
|
+
return seen.get(value);
|
|
179
|
+
}
|
|
180
|
+
let result;
|
|
181
|
+
if (Array.isArray(value)) {
|
|
182
|
+
result = [];
|
|
183
|
+
seen.set(value, result);
|
|
184
|
+
for (const item of value) {
|
|
185
|
+
result.push(_deepClone(item, seen));
|
|
186
|
+
}
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
if (value instanceof Date) {
|
|
190
|
+
result = new Date(value.getTime());
|
|
191
|
+
seen.set(value, result);
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
if (value instanceof Map) {
|
|
195
|
+
result = /* @__PURE__ */ new Map();
|
|
196
|
+
seen.set(value, result);
|
|
197
|
+
for (const [k, v] of value) {
|
|
198
|
+
result.set(_deepClone(k, seen), _deepClone(v, seen));
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
if (value instanceof Set) {
|
|
203
|
+
result = /* @__PURE__ */ new Set();
|
|
204
|
+
seen.set(value, result);
|
|
205
|
+
for (const v of value) {
|
|
206
|
+
result.add(_deepClone(v, seen));
|
|
207
|
+
}
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
if (value instanceof RegExp) {
|
|
211
|
+
result = new RegExp(value.source, value.flags);
|
|
212
|
+
seen.set(value, result);
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
if (ArrayBuffer.isView(value)) {
|
|
216
|
+
if (value instanceof DataView) {
|
|
217
|
+
const buf2 = _deepClone(value.buffer, seen);
|
|
218
|
+
const clone2 = new DataView(buf2, value.byteOffset, value.byteLength);
|
|
219
|
+
seen.set(value, clone2);
|
|
220
|
+
return clone2;
|
|
221
|
+
}
|
|
222
|
+
const typed = value;
|
|
223
|
+
const buf = _deepClone(typed.buffer, seen);
|
|
224
|
+
const clone = new typed.constructor(buf, typed.byteOffset, typed.length);
|
|
225
|
+
seen.set(value, clone);
|
|
226
|
+
return clone;
|
|
227
|
+
}
|
|
228
|
+
if (value instanceof ArrayBuffer) {
|
|
229
|
+
result = value.slice(0);
|
|
230
|
+
seen.set(value, result);
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
233
|
+
result = Object.create(Object.getPrototypeOf(value));
|
|
234
|
+
seen.set(value, result);
|
|
235
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
236
|
+
const desc = Object.getOwnPropertyDescriptor(value, key);
|
|
237
|
+
if (desc?.get || desc?.set) {
|
|
238
|
+
Object.defineProperty(result, key, {
|
|
239
|
+
get: desc.get,
|
|
240
|
+
set: desc.set,
|
|
241
|
+
enumerable: desc.enumerable,
|
|
242
|
+
configurable: desc.configurable
|
|
243
|
+
});
|
|
244
|
+
} else {
|
|
245
|
+
Object.defineProperty(result, key, {
|
|
246
|
+
value: _deepClone(value[key], seen),
|
|
247
|
+
writable: desc?.writable ?? true,
|
|
248
|
+
enumerable: desc?.enumerable ?? true,
|
|
249
|
+
configurable: desc?.configurable ?? true
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
__name(_deepClone, "_deepClone");
|
|
256
|
+
function deepClone(value) {
|
|
257
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
258
|
+
return _deepClone(value, seen);
|
|
259
|
+
}
|
|
260
|
+
__name(deepClone, "deepClone");
|
|
261
|
+
function deepFreeze(obj) {
|
|
262
|
+
Object.freeze(obj);
|
|
263
|
+
for (const key of Reflect.ownKeys(obj)) {
|
|
264
|
+
const val = obj[key];
|
|
265
|
+
if (isObject(val) && !Object.isFrozen(val)) {
|
|
266
|
+
deepFreeze(val);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return obj;
|
|
270
|
+
}
|
|
271
|
+
__name(deepFreeze, "deepFreeze");
|
|
272
|
+
function getAllPaths(obj, parentPath = "") {
|
|
273
|
+
if (!isObject(obj)) return [];
|
|
274
|
+
return Object.entries(obj).flatMap(([key, value]) => {
|
|
275
|
+
const currentPath = parentPath ? `${parentPath}.${key}` : key;
|
|
276
|
+
if (isObject(value)) {
|
|
277
|
+
return [
|
|
278
|
+
currentPath,
|
|
279
|
+
...getAllPaths(value, currentPath)
|
|
280
|
+
];
|
|
281
|
+
}
|
|
282
|
+
return [
|
|
283
|
+
currentPath
|
|
284
|
+
];
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
__name(getAllPaths, "getAllPaths");
|
|
288
|
+
|
|
289
|
+
export { deepClone, deepFreeze, deepObjMerge, filterObject, flattenObject, getAllPaths, getValueByPath, isEqual, isObjEmpty, isObject, isPlainObject, mapObject, omit, pick, setValueByPath };
|
package/package.json
CHANGED
|
@@ -1,50 +1,202 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@catbee/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-next.1",
|
|
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
|
-
"main": "build/index.cjs",
|
|
6
|
-
"module": "build/index.mjs",
|
|
7
|
-
"types": "build/index.d.ts",
|
|
8
5
|
"publishConfig": {
|
|
9
6
|
"access": "public"
|
|
10
7
|
},
|
|
11
|
-
"author":
|
|
12
|
-
|
|
13
|
-
"
|
|
8
|
+
"author": "Hari G <harig@catbee.in>",
|
|
9
|
+
"maintainers": [
|
|
10
|
+
"Hari G <harig@catbee.in>",
|
|
11
|
+
"Bavi R <bavithra@catbee.in>"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://catbee.in/docs/@catbee/utils/intro",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/catbee-technologies/catbee-utils.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/catbee-technologies/catbee-utils/issues"
|
|
14
20
|
},
|
|
15
21
|
"license": "MIT",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"pino": "^10.1.0"
|
|
24
|
+
},
|
|
16
25
|
"optionalDependencies": {
|
|
17
|
-
"@scalar/express-api-reference": "^0.8.
|
|
26
|
+
"@scalar/express-api-reference": "^0.8.30",
|
|
18
27
|
"abort-controller": "^3.0.0",
|
|
19
28
|
"compression": "^1.8.1",
|
|
20
29
|
"cookie-parser": "^1.4.7",
|
|
21
30
|
"cors": "^2.8.5",
|
|
31
|
+
"dotenv": "^17.2.3",
|
|
22
32
|
"express-rate-limit": "^8.2.1",
|
|
23
33
|
"helmet": "^8.1.0",
|
|
24
|
-
"pino": "^
|
|
25
|
-
"
|
|
26
|
-
"
|
|
34
|
+
"pino-pretty": "^13.1.3",
|
|
35
|
+
"prom-client": "^15.1.3",
|
|
36
|
+
"reflect-metadata": "^0.2.2"
|
|
27
37
|
},
|
|
28
38
|
"peerDependencies": {
|
|
29
|
-
"express": "^5.1.0"
|
|
30
|
-
"reflect-metadata": "^0.2.2"
|
|
39
|
+
"express": "^5.1.0"
|
|
31
40
|
},
|
|
32
41
|
"peerDependenciesMeta": {
|
|
33
42
|
"express": {
|
|
34
43
|
"optional": true
|
|
35
|
-
},
|
|
36
|
-
"reflect-metadata": {
|
|
37
|
-
"optional": true
|
|
38
44
|
}
|
|
39
45
|
},
|
|
40
46
|
"engines": {
|
|
41
47
|
"node": ">=18.4"
|
|
42
48
|
},
|
|
49
|
+
"main": "index.cjs",
|
|
50
|
+
"module": "index.mjs",
|
|
51
|
+
"types": "index.d.ts",
|
|
43
52
|
"files": [
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
53
|
+
"**/*.mjs",
|
|
54
|
+
"**/*.cjs",
|
|
55
|
+
"**/*.d.ts",
|
|
56
|
+
"**/*.d.mts",
|
|
47
57
|
"LICENSE",
|
|
48
58
|
"README.md"
|
|
49
|
-
]
|
|
59
|
+
],
|
|
60
|
+
"exports": {
|
|
61
|
+
".": {
|
|
62
|
+
"import": "./index.mjs",
|
|
63
|
+
"require": "./index.cjs",
|
|
64
|
+
"types": "./index.d.ts"
|
|
65
|
+
},
|
|
66
|
+
"./array": {
|
|
67
|
+
"import": "./array/index.mjs",
|
|
68
|
+
"require": "./array/index.cjs",
|
|
69
|
+
"types": "./array/index.d.ts"
|
|
70
|
+
},
|
|
71
|
+
"./async": {
|
|
72
|
+
"import": "./async/index.mjs",
|
|
73
|
+
"require": "./async/index.cjs",
|
|
74
|
+
"types": "./async/index.d.ts"
|
|
75
|
+
},
|
|
76
|
+
"./cache": {
|
|
77
|
+
"import": "./cache/index.mjs",
|
|
78
|
+
"require": "./cache/index.cjs",
|
|
79
|
+
"types": "./cache/index.d.ts"
|
|
80
|
+
},
|
|
81
|
+
"./config": {
|
|
82
|
+
"import": "./config/index.mjs",
|
|
83
|
+
"require": "./config/index.cjs",
|
|
84
|
+
"types": "./config/index.d.ts"
|
|
85
|
+
},
|
|
86
|
+
"./context-store": {
|
|
87
|
+
"import": "./context-store/index.mjs",
|
|
88
|
+
"require": "./context-store/index.cjs",
|
|
89
|
+
"types": "./context-store/index.d.ts"
|
|
90
|
+
},
|
|
91
|
+
"./crypto": {
|
|
92
|
+
"import": "./crypto/index.mjs",
|
|
93
|
+
"require": "./crypto/index.cjs",
|
|
94
|
+
"types": "./crypto/index.d.ts"
|
|
95
|
+
},
|
|
96
|
+
"./date": {
|
|
97
|
+
"import": "./date/index.mjs",
|
|
98
|
+
"require": "./date/index.cjs",
|
|
99
|
+
"types": "./date/index.d.ts"
|
|
100
|
+
},
|
|
101
|
+
"./decorators": {
|
|
102
|
+
"import": "./decorators/index.mjs",
|
|
103
|
+
"require": "./decorators/index.cjs",
|
|
104
|
+
"types": "./decorators/index.d.ts"
|
|
105
|
+
},
|
|
106
|
+
"./dir": {
|
|
107
|
+
"import": "./dir/index.mjs",
|
|
108
|
+
"require": "./dir/index.cjs",
|
|
109
|
+
"types": "./dir/index.d.ts"
|
|
110
|
+
},
|
|
111
|
+
"./env": {
|
|
112
|
+
"import": "./env/index.mjs",
|
|
113
|
+
"require": "./env/index.cjs",
|
|
114
|
+
"types": "./env/index.d.ts"
|
|
115
|
+
},
|
|
116
|
+
"./exception": {
|
|
117
|
+
"import": "./exception/index.mjs",
|
|
118
|
+
"require": "./exception/index.cjs",
|
|
119
|
+
"types": "./exception/index.d.ts"
|
|
120
|
+
},
|
|
121
|
+
"./fs": {
|
|
122
|
+
"import": "./fs/index.mjs",
|
|
123
|
+
"require": "./fs/index.cjs",
|
|
124
|
+
"types": "./fs/index.d.ts"
|
|
125
|
+
},
|
|
126
|
+
"./http-status-codes": {
|
|
127
|
+
"import": "./http-status-codes/index.mjs",
|
|
128
|
+
"require": "./http-status-codes/index.cjs",
|
|
129
|
+
"types": "./http-status-codes/index.d.ts"
|
|
130
|
+
},
|
|
131
|
+
"./id": {
|
|
132
|
+
"import": "./id/index.mjs",
|
|
133
|
+
"require": "./id/index.cjs",
|
|
134
|
+
"types": "./id/index.d.ts"
|
|
135
|
+
},
|
|
136
|
+
"./logger": {
|
|
137
|
+
"import": "./logger/index.mjs",
|
|
138
|
+
"require": "./logger/index.cjs",
|
|
139
|
+
"types": "./logger/index.d.ts"
|
|
140
|
+
},
|
|
141
|
+
"./middleware": {
|
|
142
|
+
"import": "./middleware/index.mjs",
|
|
143
|
+
"require": "./middleware/index.cjs",
|
|
144
|
+
"types": "./middleware/index.d.ts"
|
|
145
|
+
},
|
|
146
|
+
"./obj": {
|
|
147
|
+
"import": "./obj/index.mjs",
|
|
148
|
+
"require": "./obj/index.cjs",
|
|
149
|
+
"types": "./obj/index.d.ts"
|
|
150
|
+
},
|
|
151
|
+
"./performance": {
|
|
152
|
+
"import": "./performance/index.mjs",
|
|
153
|
+
"require": "./performance/index.cjs",
|
|
154
|
+
"types": "./performance/index.d.ts"
|
|
155
|
+
},
|
|
156
|
+
"./request": {
|
|
157
|
+
"import": "./request/index.mjs",
|
|
158
|
+
"require": "./request/index.cjs",
|
|
159
|
+
"types": "./request/index.d.ts"
|
|
160
|
+
},
|
|
161
|
+
"./response": {
|
|
162
|
+
"import": "./response/index.mjs",
|
|
163
|
+
"require": "./response/index.cjs",
|
|
164
|
+
"types": "./response/index.d.ts"
|
|
165
|
+
},
|
|
166
|
+
"./server": {
|
|
167
|
+
"import": "./server/index.mjs",
|
|
168
|
+
"require": "./server/index.cjs",
|
|
169
|
+
"types": "./server/index.d.ts"
|
|
170
|
+
},
|
|
171
|
+
"./stream": {
|
|
172
|
+
"import": "./stream/index.mjs",
|
|
173
|
+
"require": "./stream/index.cjs",
|
|
174
|
+
"types": "./stream/index.d.ts"
|
|
175
|
+
},
|
|
176
|
+
"./string": {
|
|
177
|
+
"import": "./string/index.mjs",
|
|
178
|
+
"require": "./string/index.cjs",
|
|
179
|
+
"types": "./string/index.d.ts"
|
|
180
|
+
},
|
|
181
|
+
"./type": {
|
|
182
|
+
"import": "./type/index.mjs",
|
|
183
|
+
"require": "./type/index.cjs",
|
|
184
|
+
"types": "./type/index.d.ts"
|
|
185
|
+
},
|
|
186
|
+
"./types": {
|
|
187
|
+
"import": "./types/index.mjs",
|
|
188
|
+
"require": "./types/index.cjs",
|
|
189
|
+
"types": "./types/index.d.ts"
|
|
190
|
+
},
|
|
191
|
+
"./url": {
|
|
192
|
+
"import": "./url/index.mjs",
|
|
193
|
+
"require": "./url/index.cjs",
|
|
194
|
+
"types": "./url/index.d.ts"
|
|
195
|
+
},
|
|
196
|
+
"./validation": {
|
|
197
|
+
"import": "./validation/index.mjs",
|
|
198
|
+
"require": "./validation/index.cjs",
|
|
199
|
+
"types": "./validation/index.d.ts"
|
|
200
|
+
}
|
|
201
|
+
}
|
|
50
202
|
}
|