@catbee/utils 1.0.5 → 2.0.0-next.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 +27 -27
- package/array/array.utils.d.ts +191 -0
- package/array/index.cjs +246 -0
- package/array/index.d.ts +25 -0
- package/array/index.mjs +228 -0
- package/async/async.utils.d.ts +296 -0
- package/async/index.cjs +428 -0
- package/async/index.d.ts +25 -0
- package/async/index.mjs +407 -0
- package/cache/cache.utils.d.ts +176 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +25 -0
- package/cache/index.mjs +290 -0
- package/config/config.d.ts +57 -0
- package/config/index.cjs +136 -0
- package/config/index.d.ts +26 -0
- package/config/index.mjs +131 -0
- package/context-store/context-store.utils.d.ts +212 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +25 -0
- package/context-store/index.mjs +261 -0
- package/crypto/crypto.utils.d.ts +183 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +25 -0
- package/crypto/index.mjs +166 -0
- package/date/date.utils.d.ts +190 -0
- package/date/index.cjs +295 -0
- package/date/index.d.ts +25 -0
- package/date/index.mjs +283 -0
- package/decorators/decorators.utils.d.ts +705 -0
- package/decorators/index.cjs +913 -0
- package/decorators/index.d.ts +25 -0
- package/decorators/index.mjs +872 -0
- package/dir/dir.utils.d.ts +216 -0
- package/dir/index.cjs +416 -0
- package/dir/index.d.ts +25 -0
- package/dir/index.mjs +389 -0
- package/env/env.utils.d.ts +400 -0
- package/env/index.cjs +761 -0
- package/env/index.d.ts +25 -0
- package/env/index.mjs +758 -0
- package/exception/exception.utils.d.ts +253 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +25 -0
- package/exception/index.mjs +338 -0
- package/fs/fs.utils.d.ts +196 -0
- package/fs/index.cjs +253 -0
- package/fs/index.d.ts +25 -0
- package/fs/index.mjs +228 -0
- package/http-status-codes/http-status-codes.d.ts +289 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +25 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/id.utils.d.ts +59 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +25 -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 +25 -0
- package/logger/index.mjs +313 -0
- package/logger/logger.utils.d.ts +210 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +25 -0
- package/middleware/index.mjs +170 -0
- package/middleware/middleware.utils.d.ts +123 -0
- package/obj/index.cjs +317 -0
- package/obj/index.d.ts +25 -0
- package/obj/index.mjs +301 -0
- package/obj/obj.utils.d.ts +156 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +25 -0
- package/performance/index.mjs +225 -0
- package/performance/performance.utils.d.ts +159 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +26 -0
- package/request/index.mjs +194 -0
- package/request/request.utils.d.ts +109 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +26 -0
- package/response/index.mjs +222 -0
- package/response/response.utils.d.ts +186 -0
- package/server/index.cjs +1627 -0
- package/server/index.d.ts +28 -0
- package/server/index.mjs +1617 -0
- package/server/server.builder.d.ts +531 -0
- package/server/server.d.ts +303 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +25 -0
- package/stream/index.mjs +144 -0
- package/stream/stream.utils.d.ts +111 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +25 -0
- package/string/index.mjs +95 -0
- package/string/string.utils.d.ts +124 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +25 -0
- package/type/index.mjs +119 -0
- package/type/type.utils.d.ts +129 -0
- package/types/api-response.d.ts +175 -0
- package/types/common.d.ts +148 -0
- package/types/config.d.ts +88 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +28 -0
- package/types/index.mjs +32 -0
- package/types/server.d.ts +291 -0
- package/url/index.cjs +201 -0
- package/url/index.d.ts +25 -0
- package/url/index.mjs +189 -0
- package/url/url.utils.d.ts +164 -0
- package/validate/index.cjs +212 -0
- package/validate/index.d.ts +25 -0
- package/validate/index.mjs +188 -0
- package/validate/validate.utils.d.ts +200 -0
- package/build/index.cjs +0 -7579
- package/build/index.d.ts +0 -5774
- package/build/index.mjs +0 -7274
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
26
|
+
export type Middleware = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Attaches a unique request ID to each request.
|
|
29
|
+
* Useful for request tracing and correlation between logs.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} [options] - Configuration options
|
|
32
|
+
* @param {string} [options.headerName='X-Request-ID'] - Header name for request ID
|
|
33
|
+
* @param {boolean} [options.exposeHeader=true] - Whether to expose the header in response
|
|
34
|
+
* @param {() => string} [options.generator] - Custom ID generator function
|
|
35
|
+
* @returns {Middleware} Express-compatible middleware
|
|
36
|
+
*/
|
|
37
|
+
export declare function requestId(options?: {
|
|
38
|
+
headerName?: string;
|
|
39
|
+
exposeHeader?: boolean;
|
|
40
|
+
generator?: () => string;
|
|
41
|
+
}): Middleware;
|
|
42
|
+
/**
|
|
43
|
+
* Measures request processing time and logs or adds it to response headers.
|
|
44
|
+
*
|
|
45
|
+
* @param {object} [options] - Configuration options
|
|
46
|
+
* @param {boolean} [options.addHeader=true] - Whether to add X-Response-Time header
|
|
47
|
+
* @param {boolean} [options.logOnComplete=false] - Whether to log timing info
|
|
48
|
+
* @returns {Middleware} Express-compatible middleware
|
|
49
|
+
*/
|
|
50
|
+
export declare function responseTime(options?: {
|
|
51
|
+
addHeader?: boolean;
|
|
52
|
+
logOnComplete?: boolean;
|
|
53
|
+
}): (req: Request, res: Response, next: NextFunction) => void;
|
|
54
|
+
/**
|
|
55
|
+
* Request timeout middleware.
|
|
56
|
+
* Aborts requests that take too long to process.
|
|
57
|
+
*
|
|
58
|
+
* @param {number} [timeoutMs=30000] - Timeout in milliseconds
|
|
59
|
+
* @returns {Middleware} Express-compatible middleware
|
|
60
|
+
*/
|
|
61
|
+
export declare function timeout(timeoutMs?: number): Middleware;
|
|
62
|
+
/**
|
|
63
|
+
* Creates an Express middleware that initializes a per-request context.
|
|
64
|
+
*
|
|
65
|
+
* @param {object} [options] - Optional configuration
|
|
66
|
+
* @param {string} [options.headerName='x-request-id'] - Header to look for request ID
|
|
67
|
+
* @param {boolean} [options.autoLog=true] - Whether to log automatically when context is initialized
|
|
68
|
+
* @returns {(req: Request, res: Response, next: NextFunction) => void} Express middleware function
|
|
69
|
+
*/
|
|
70
|
+
export declare function setupRequestContext(options?: {
|
|
71
|
+
headerName?: string;
|
|
72
|
+
autoLog?: boolean;
|
|
73
|
+
}): Middleware;
|
|
74
|
+
export interface ErrorHandlerOptions {
|
|
75
|
+
/** Whether to log errors (default: true) */
|
|
76
|
+
logErrors?: boolean;
|
|
77
|
+
/** Whether to include error details in non-production (default: false) */
|
|
78
|
+
includeDetails?: boolean;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Global error handling middleware with enhanced features.
|
|
82
|
+
*
|
|
83
|
+
* @param {ErrorHandlerOptions} options - Error handler options
|
|
84
|
+
* @param {boolean} [options.logErrors=true] - Whether to log errors
|
|
85
|
+
* @param {boolean} [options.includeDetails=false] - Whether to include error details in non-production
|
|
86
|
+
* @returns Error middleware
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* import express from 'express';
|
|
90
|
+
* import { errorHandler } from '@catbee/utils';
|
|
91
|
+
*
|
|
92
|
+
* const app = express();
|
|
93
|
+
*
|
|
94
|
+
* // Your routes
|
|
95
|
+
* app.get('/ping', (req, res) => {
|
|
96
|
+
* throw new Error('Something went wrong');
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* // Error handler (must be last middleware)
|
|
100
|
+
* app.use(errorHandler({ includeDetails: true }));
|
|
101
|
+
*
|
|
102
|
+
* app.listen(3000, () => {
|
|
103
|
+
* console.log('Server running on port 3000');
|
|
104
|
+
* });
|
|
105
|
+
*/
|
|
106
|
+
export declare function errorHandler(options?: ErrorHandlerOptions): (err: any, req: Request, res: Response, _next: NextFunction) => Response<any, Record<string, any>>;
|
|
107
|
+
/**
|
|
108
|
+
* Health check middleware for service status and custom checks.
|
|
109
|
+
*
|
|
110
|
+
* @param {object} [options] - Health check options
|
|
111
|
+
* @param {string} [options.path='/healthz'] - Health check endpoint path
|
|
112
|
+
* @param {Array<{name: string; check: () => Promise<boolean> | boolean}>} [options.checks] - Custom health checks
|
|
113
|
+
* @param {boolean} [options.detailed=true] - Show detailed check results
|
|
114
|
+
* @returns {Middleware} Express-compatible middleware
|
|
115
|
+
*/
|
|
116
|
+
export declare function healthCheck(options?: {
|
|
117
|
+
path?: string;
|
|
118
|
+
checks?: Array<{
|
|
119
|
+
name: string;
|
|
120
|
+
check: () => Promise<boolean> | boolean;
|
|
121
|
+
}>;
|
|
122
|
+
detailed?: boolean;
|
|
123
|
+
}): Middleware;
|
package/obj/index.cjs
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
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
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var __defProp = Object.defineProperty;
|
|
28
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
|
+
|
|
30
|
+
// src/obj/obj.utils.ts
|
|
31
|
+
function isObjEmpty(obj) {
|
|
32
|
+
return !!obj && typeof obj === "object" && Object.keys(obj).length === 0;
|
|
33
|
+
}
|
|
34
|
+
__name(isObjEmpty, "isObjEmpty");
|
|
35
|
+
function pick(obj, keys) {
|
|
36
|
+
return Object.fromEntries(keys.map((key) => [
|
|
37
|
+
key,
|
|
38
|
+
obj[key]
|
|
39
|
+
]));
|
|
40
|
+
}
|
|
41
|
+
__name(pick, "pick");
|
|
42
|
+
function omit(obj, keys) {
|
|
43
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key)));
|
|
44
|
+
}
|
|
45
|
+
__name(omit, "omit");
|
|
46
|
+
function deepObjMerge(target, ...sources) {
|
|
47
|
+
if (!sources.length) return target;
|
|
48
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
49
|
+
seen.set(target, target);
|
|
50
|
+
for (const s of sources) {
|
|
51
|
+
if (s && typeof s === "object") {
|
|
52
|
+
seen.set(s, target);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function deepClone2(value) {
|
|
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);
|
|
81
|
+
}
|
|
82
|
+
if (value instanceof ArrayBuffer) {
|
|
83
|
+
return value.slice(0);
|
|
84
|
+
}
|
|
85
|
+
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
|
|
86
|
+
const typed = value;
|
|
87
|
+
const ctor = typed.constructor;
|
|
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));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const clone = Object.create(Object.getPrototypeOf(value));
|
|
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]);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return clone;
|
|
126
|
+
}
|
|
127
|
+
__name(deepClone2, "deepClone");
|
|
128
|
+
function mergeProp(target2, source, key) {
|
|
129
|
+
const targetValue = target2[key];
|
|
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);
|
|
147
|
+
}
|
|
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);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return target;
|
|
160
|
+
}
|
|
161
|
+
__name(deepObjMerge, "deepObjMerge");
|
|
162
|
+
function isPlainObject(value) {
|
|
163
|
+
if (typeof value !== "object" || value === null) return false;
|
|
164
|
+
if (Array.isArray(value)) return false;
|
|
165
|
+
const proto = Object.getPrototypeOf(value);
|
|
166
|
+
return proto === Object.prototype || proto === null;
|
|
167
|
+
}
|
|
168
|
+
__name(isPlainObject, "isPlainObject");
|
|
169
|
+
function flattenObject(obj, prefix = "") {
|
|
170
|
+
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
171
|
+
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
172
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
173
|
+
Object.assign(acc, flattenObject(value, newKey));
|
|
174
|
+
} else {
|
|
175
|
+
acc[newKey] = value;
|
|
176
|
+
}
|
|
177
|
+
return acc;
|
|
178
|
+
}, {});
|
|
179
|
+
}
|
|
180
|
+
__name(flattenObject, "flattenObject");
|
|
181
|
+
function getValueByPath(obj, path) {
|
|
182
|
+
if (!obj || typeof obj !== "object") return void 0;
|
|
183
|
+
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
184
|
+
return parts.reduce((acc, key) => acc?.[key], obj);
|
|
185
|
+
}
|
|
186
|
+
__name(getValueByPath, "getValueByPath");
|
|
187
|
+
function setValueByPath(obj, path, value) {
|
|
188
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
189
|
+
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
190
|
+
if (parts.length === 0) return obj;
|
|
191
|
+
const forbiddenKeys = /* @__PURE__ */ new Set([
|
|
192
|
+
"__proto__",
|
|
193
|
+
"prototype",
|
|
194
|
+
"constructor"
|
|
195
|
+
]);
|
|
196
|
+
let current = obj;
|
|
197
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
198
|
+
const key = parts[i];
|
|
199
|
+
if (forbiddenKeys.has(key)) {
|
|
200
|
+
return obj;
|
|
201
|
+
}
|
|
202
|
+
const nextKey = parts[i + 1];
|
|
203
|
+
const isArrayIndex = !isNaN(Number(nextKey));
|
|
204
|
+
if (current[key] === void 0) {
|
|
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
|
+
}
|
|
209
|
+
current = current[key];
|
|
210
|
+
}
|
|
211
|
+
const finalKey = parts[parts.length - 1];
|
|
212
|
+
if (forbiddenKeys.has(finalKey)) {
|
|
213
|
+
return obj;
|
|
214
|
+
}
|
|
215
|
+
current[finalKey] = value;
|
|
216
|
+
return obj;
|
|
217
|
+
}
|
|
218
|
+
__name(setValueByPath, "setValueByPath");
|
|
219
|
+
function isEqual(a, b) {
|
|
220
|
+
if (a === b) return true;
|
|
221
|
+
if (a == null || b == null || typeof a !== "object" || typeof b !== "object") {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
if (a instanceof Date && b instanceof Date) {
|
|
225
|
+
return a.getTime() === b.getTime();
|
|
226
|
+
}
|
|
227
|
+
if (a instanceof Date || b instanceof Date) {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
231
|
+
if (a.length !== b.length) return false;
|
|
232
|
+
for (let i = 0; i < a.length; i++) {
|
|
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]));
|
|
244
|
+
}
|
|
245
|
+
__name(isEqual, "isEqual");
|
|
246
|
+
function filterObject(obj, predicate) {
|
|
247
|
+
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(value, key, obj)));
|
|
248
|
+
}
|
|
249
|
+
__name(filterObject, "filterObject");
|
|
250
|
+
function mapObject(obj, mapFn) {
|
|
251
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
252
|
+
key,
|
|
253
|
+
mapFn(value, key, obj)
|
|
254
|
+
]));
|
|
255
|
+
}
|
|
256
|
+
__name(mapObject, "mapObject");
|
|
257
|
+
function deepClone(value) {
|
|
258
|
+
try {
|
|
259
|
+
if (typeof globalThis.structuredClone === "function") {
|
|
260
|
+
return globalThis.structuredClone(value);
|
|
261
|
+
}
|
|
262
|
+
} catch (_) {
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
return JSON.parse(JSON.stringify(value));
|
|
266
|
+
} catch (_) {
|
|
267
|
+
return value;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
__name(deepClone, "deepClone");
|
|
271
|
+
function deepFreeze(obj) {
|
|
272
|
+
Object.freeze(obj);
|
|
273
|
+
for (const prop of Object.getOwnPropertyNames(obj)) {
|
|
274
|
+
const value = obj[prop];
|
|
275
|
+
if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
276
|
+
deepFreeze(value);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return obj;
|
|
280
|
+
}
|
|
281
|
+
__name(deepFreeze, "deepFreeze");
|
|
282
|
+
function isObject(value) {
|
|
283
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
284
|
+
}
|
|
285
|
+
__name(isObject, "isObject");
|
|
286
|
+
function getAllPaths(obj, parentPath = "") {
|
|
287
|
+
if (!isObject(obj)) return [];
|
|
288
|
+
return Object.entries(obj).flatMap(([key, value]) => {
|
|
289
|
+
const currentPath = parentPath ? `${parentPath}.${key}` : key;
|
|
290
|
+
if (isObject(value)) {
|
|
291
|
+
return [
|
|
292
|
+
currentPath,
|
|
293
|
+
...getAllPaths(value, currentPath)
|
|
294
|
+
];
|
|
295
|
+
}
|
|
296
|
+
return [
|
|
297
|
+
currentPath
|
|
298
|
+
];
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
__name(getAllPaths, "getAllPaths");
|
|
302
|
+
|
|
303
|
+
exports.deepClone = deepClone;
|
|
304
|
+
exports.deepFreeze = deepFreeze;
|
|
305
|
+
exports.deepObjMerge = deepObjMerge;
|
|
306
|
+
exports.filterObject = filterObject;
|
|
307
|
+
exports.flattenObject = flattenObject;
|
|
308
|
+
exports.getAllPaths = getAllPaths;
|
|
309
|
+
exports.getValueByPath = getValueByPath;
|
|
310
|
+
exports.isEqual = isEqual;
|
|
311
|
+
exports.isObjEmpty = isObjEmpty;
|
|
312
|
+
exports.isObject = isObject;
|
|
313
|
+
exports.isPlainObject = isPlainObject;
|
|
314
|
+
exports.mapObject = mapObject;
|
|
315
|
+
exports.omit = omit;
|
|
316
|
+
exports.pick = pick;
|
|
317
|
+
exports.setValueByPath = setValueByPath;
|
package/obj/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export * from './obj.utils';
|