@catbee/utils 2.0.0-next.1 → 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.
Files changed (87) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +50 -36
  3. package/array/index.cjs +37 -5
  4. package/array/index.d.ts +59 -8
  5. package/array/index.mjs +32 -4
  6. package/async/index.cjs +24 -3
  7. package/async/index.d.ts +18 -2
  8. package/async/index.mjs +24 -4
  9. package/cache/index.cjs +1 -1
  10. package/cache/index.d.ts +1 -1
  11. package/cache/index.mjs +1 -1
  12. package/config/index.cjs +8 -8
  13. package/config/index.d.ts +1 -1
  14. package/config/index.mjs +4 -4
  15. package/context-store/index.cjs +2 -3
  16. package/context-store/index.d.ts +1 -1
  17. package/context-store/index.mjs +2 -3
  18. package/crypto/index.cjs +55 -5
  19. package/crypto/index.d.ts +63 -2
  20. package/crypto/index.mjs +52 -7
  21. package/date/index.cjs +630 -1
  22. package/date/index.d.ts +487 -2
  23. package/date/index.mjs +621 -2
  24. package/{decorators → decorator}/index.cjs +425 -304
  25. package/{decorators → decorator}/index.d.ts +3 -2
  26. package/{decorators → decorator}/index.mjs +425 -304
  27. package/{dir → directory}/index.cjs +1 -1
  28. package/{dir → directory}/index.d.ts +1 -1
  29. package/{dir → directory}/index.mjs +1 -1
  30. package/env/index.cjs +90 -42
  31. package/env/index.d.ts +12 -1
  32. package/env/index.mjs +90 -42
  33. package/exception/index.cjs +1 -1
  34. package/exception/index.d.ts +1 -1
  35. package/exception/index.mjs +1 -1
  36. package/fs/index.cjs +1 -1
  37. package/fs/index.d.ts +1 -1
  38. package/fs/index.mjs +1 -1
  39. package/http-status-codes/index.cjs +1 -1
  40. package/http-status-codes/index.d.ts +1 -1
  41. package/http-status-codes/index.mjs +1 -1
  42. package/id/index.cjs +1 -1
  43. package/id/index.d.ts +1 -1
  44. package/id/index.mjs +1 -1
  45. package/index.cjs +10 -10
  46. package/index.d.ts +4 -4
  47. package/index.mjs +4 -4
  48. package/logger/index.cjs +2 -4
  49. package/logger/index.d.ts +2 -2
  50. package/logger/index.mjs +2 -4
  51. package/middleware/index.cjs +1 -1
  52. package/middleware/index.d.ts +1 -1
  53. package/middleware/index.mjs +1 -1
  54. package/{obj → object}/index.cjs +182 -108
  55. package/{obj → object}/index.d.ts +38 -2
  56. package/{obj → object}/index.mjs +180 -109
  57. package/package.json +31 -13
  58. package/performance/index.cjs +2 -2
  59. package/performance/index.d.ts +1 -1
  60. package/performance/index.mjs +2 -2
  61. package/request/index.cjs +36 -24
  62. package/request/index.d.ts +1 -1
  63. package/request/index.mjs +36 -24
  64. package/response/index.cjs +1 -1
  65. package/response/index.d.ts +1 -1
  66. package/response/index.mjs +1 -1
  67. package/server/index.cjs +230 -131
  68. package/server/index.d.ts +81 -1
  69. package/server/index.mjs +226 -127
  70. package/stream/index.cjs +1 -1
  71. package/stream/index.d.ts +1 -1
  72. package/stream/index.mjs +1 -1
  73. package/string/index.cjs +34 -1
  74. package/string/index.d.ts +45 -2
  75. package/string/index.mjs +31 -2
  76. package/type/index.cjs +18 -1
  77. package/type/index.d.ts +38 -2
  78. package/type/index.mjs +16 -2
  79. package/types/index.cjs +1 -1
  80. package/types/index.d.ts +3 -3
  81. package/types/index.mjs +1 -1
  82. package/url/index.cjs +61 -1
  83. package/url/index.d.ts +59 -2
  84. package/url/index.mjs +57 -2
  85. package/validation/index.cjs +3 -3
  86. package/validation/index.d.ts +1 -1
  87. package/validation/index.mjs +3 -3
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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
@@ -25,7 +25,7 @@
25
25
  var __defProp = Object.defineProperty;
26
26
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
27
27
 
28
- // src/obj/obj.utils.ts
28
+ // src/object/object.utils.ts
29
29
  function isObject(value) {
30
30
  return value !== null && typeof value === "object" && !Array.isArray(value);
31
31
  }
@@ -47,41 +47,42 @@ function omit(obj, keys) {
47
47
  __name(omit, "omit");
48
48
  function deepObjMerge(target, ...sources) {
49
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)) {
50
+ for (const s of sources) {
51
+ target = mergeInto(target, s, seen);
52
+ }
53
+ return target;
54
+ }
55
+ __name(deepObjMerge, "deepObjMerge");
56
+ function mergeInto(t, s, seen) {
57
+ if (s === void 0) return t;
58
+ if (typeof s === "function" || typeof s === "symbol") return s;
59
+ if (Array.isArray(s)) {
60
+ const cloned = deepClone(s);
61
+ seen.set(s, cloned);
62
+ return cloned;
63
+ }
64
+ if (s && typeof s === "object") {
65
+ if (seen.has(s)) return seen.get(s);
66
+ if (s instanceof Date || s instanceof Map || s instanceof Set || s instanceof RegExp || s instanceof ArrayBuffer || ArrayBuffer.isView(s)) {
54
67
  const cloned = deepClone(s);
55
68
  seen.set(s, cloned);
56
69
  return cloned;
57
70
  }
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;
71
+ if (!t || typeof t !== "object") {
72
+ t = Object.create(Object.getPrototypeOf(s));
76
73
  }
77
- return s;
78
- }, "mergeInto");
79
- for (const s of sources) {
80
- target = mergeInto(target, s);
74
+ seen.set(s, t);
75
+ for (const key of Reflect.ownKeys(s)) {
76
+ if (key === "__proto__") continue;
77
+ const sv = s[key];
78
+ if (sv === void 0) continue;
79
+ t[key] = mergeInto(t[key], sv, seen);
80
+ }
81
+ return t;
81
82
  }
82
- return target;
83
+ return s;
83
84
  }
84
- __name(deepObjMerge, "deepObjMerge");
85
+ __name(mergeInto, "mergeInto");
85
86
  function isPlainObject(value) {
86
87
  if (typeof value !== "object" || value === null) return false;
87
88
  if (Array.isArray(value)) return false;
@@ -143,19 +144,59 @@ __name(setValueByPath, "setValueByPath");
143
144
  function isEqual(a, b) {
144
145
  if (a === b) return true;
145
146
  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]));
147
+ if (a instanceof Date && b instanceof Date) return equalDates(a, b);
148
+ if (a instanceof RegExp && b instanceof RegExp) return equalRegExp(a, b);
149
+ if (a instanceof Set && b instanceof Set) return equalSet(a, b);
150
+ if (a instanceof Map && b instanceof Map) return equalMap(a, b);
151
+ if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) return equalArrayBuffer(a, b);
152
+ if (Array.isArray(a) && Array.isArray(b)) return equalArray(a, b);
153
+ return equalPlainObjects(a, b);
157
154
  }
158
155
  __name(isEqual, "isEqual");
156
+ function equalDates(a, b) {
157
+ return a.getTime() === b.getTime();
158
+ }
159
+ __name(equalDates, "equalDates");
160
+ function equalRegExp(a, b) {
161
+ return a.source === b.source && a.flags === b.flags;
162
+ }
163
+ __name(equalRegExp, "equalRegExp");
164
+ function equalSet(a, b) {
165
+ if (a.size !== b.size) return false;
166
+ for (const v of a) if (!b.has(v)) return false;
167
+ return true;
168
+ }
169
+ __name(equalSet, "equalSet");
170
+ function equalMap(a, b) {
171
+ if (a.size !== b.size) return false;
172
+ for (const [k, v] of a) {
173
+ if (!b.has(k)) return false;
174
+ if (!isEqual(v, b.get(k))) return false;
175
+ }
176
+ return true;
177
+ }
178
+ __name(equalMap, "equalMap");
179
+ function equalArrayBuffer(a, b) {
180
+ return isEqual(new Uint8Array(a), new Uint8Array(b));
181
+ }
182
+ __name(equalArrayBuffer, "equalArrayBuffer");
183
+ function equalArray(a, b) {
184
+ if (a.length !== b.length) return false;
185
+ for (let i = 0; i < a.length; i++) if (!isEqual(a[i], b[i])) return false;
186
+ return true;
187
+ }
188
+ __name(equalArray, "equalArray");
189
+ function equalPlainObjects(a, b) {
190
+ const aKeys = Object.keys(a);
191
+ const bKeys = Object.keys(b);
192
+ if (aKeys.length !== bKeys.length) return false;
193
+ for (const key of aKeys) {
194
+ if (!Object.hasOwn(b, key)) return false;
195
+ if (!isEqual(a[key], b[key])) return false;
196
+ }
197
+ return true;
198
+ }
199
+ __name(equalPlainObjects, "equalPlainObjects");
159
200
  function filterObject(obj, predicate) {
160
201
  return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(value, key, obj)));
161
202
  }
@@ -168,72 +209,75 @@ function mapObject(obj, mapFn) {
168
209
  }
169
210
  __name(mapObject, "mapObject");
170
211
  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;
212
+ if (typeof value === "function" || typeof value === "symbol") return value;
213
+ if (value === null || typeof value !== "object") return value;
214
+ if (seen.has(value)) return seen.get(value);
215
+ if (Array.isArray(value)) return cloneArray(value, seen);
216
+ if (value instanceof Date) return cloneDate(value);
217
+ if (value instanceof Map) return cloneMap(value, seen);
218
+ if (value instanceof Set) return cloneSet(value, seen);
219
+ if (value instanceof RegExp) return cloneRegExp(value);
220
+ if (ArrayBuffer.isView(value)) return cloneArrayBufferView(value, seen);
221
+ if (value instanceof ArrayBuffer) return cloneArrayBuffer(value, seen);
222
+ return cloneObject(value, seen);
223
+ }
224
+ __name(_deepClone, "_deepClone");
225
+ function cloneArray(arr, seen) {
226
+ const result = [];
227
+ seen.set(arr, result);
228
+ for (const item of arr) result.push(_deepClone(item, seen));
229
+ return result;
230
+ }
231
+ __name(cloneArray, "cloneArray");
232
+ function cloneDate(d) {
233
+ const r = new Date(d.getTime());
234
+ return r;
235
+ }
236
+ __name(cloneDate, "cloneDate");
237
+ function cloneMap(m, seen) {
238
+ const result = /* @__PURE__ */ new Map();
239
+ seen.set(m, result);
240
+ for (const [k, v] of m) result.set(_deepClone(k, seen), _deepClone(v, seen));
241
+ return result;
242
+ }
243
+ __name(cloneMap, "cloneMap");
244
+ function cloneSet(s, seen) {
245
+ const result = /* @__PURE__ */ new Set();
246
+ seen.set(s, result);
247
+ for (const v of s) result.add(_deepClone(v, seen));
248
+ return result;
249
+ }
250
+ __name(cloneSet, "cloneSet");
251
+ function cloneRegExp(r) {
252
+ const result = new RegExp(r.source, r.flags);
253
+ return result;
254
+ }
255
+ __name(cloneRegExp, "cloneRegExp");
256
+ function cloneArrayBufferView(view, seen) {
257
+ if (view instanceof DataView) {
258
+ const buf2 = _deepClone(view.buffer, seen);
259
+ const clone2 = new DataView(buf2, view.byteOffset, view.byteLength);
260
+ seen.set(view, clone2);
261
+ return clone2;
232
262
  }
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);
263
+ const typed = view;
264
+ const buf = _deepClone(typed.buffer, seen);
265
+ const clone = new typed.constructor(buf, typed.byteOffset, typed.length);
266
+ seen.set(view, clone);
267
+ return clone;
268
+ }
269
+ __name(cloneArrayBufferView, "cloneArrayBufferView");
270
+ function cloneArrayBuffer(buf, seen) {
271
+ const result = buf.slice(0);
272
+ seen.set(buf, result);
273
+ return result;
274
+ }
275
+ __name(cloneArrayBuffer, "cloneArrayBuffer");
276
+ function cloneObject(obj, seen) {
277
+ const result = Object.create(Object.getPrototypeOf(obj));
278
+ seen.set(obj, result);
279
+ for (const key of Reflect.ownKeys(obj)) {
280
+ const desc = Object.getOwnPropertyDescriptor(obj, key);
237
281
  if (desc?.get || desc?.set) {
238
282
  Object.defineProperty(result, key, {
239
283
  get: desc.get,
@@ -243,7 +287,7 @@ function _deepClone(value, seen) {
243
287
  });
244
288
  } else {
245
289
  Object.defineProperty(result, key, {
246
- value: _deepClone(value[key], seen),
290
+ value: _deepClone(obj[key], seen),
247
291
  writable: desc?.writable ?? true,
248
292
  enumerable: desc?.enumerable ?? true,
249
293
  configurable: desc?.configurable ?? true
@@ -252,7 +296,7 @@ function _deepClone(value, seen) {
252
296
  }
253
297
  return result;
254
298
  }
255
- __name(_deepClone, "_deepClone");
299
+ __name(cloneObject, "cloneObject");
256
300
  function deepClone(value) {
257
301
  const seen = /* @__PURE__ */ new WeakMap();
258
302
  return _deepClone(value, seen);
@@ -285,5 +329,32 @@ function getAllPaths(obj, parentPath = "") {
285
329
  });
286
330
  }
287
331
  __name(getAllPaths, "getAllPaths");
332
+ function invert(obj) {
333
+ if (!isObject(obj)) return {};
334
+ return Object.fromEntries(Object.entries(obj).map(([k, v]) => [
335
+ String(v),
336
+ k
337
+ ]));
338
+ }
339
+ __name(invert, "invert");
340
+ function invertBy(obj, keyFn) {
341
+ if (!isObject(obj)) return {};
342
+ const result = {};
343
+ for (const [key, value] of Object.entries(obj)) {
344
+ const newKey = keyFn(value);
345
+ if (!result[newKey]) result[newKey] = [];
346
+ result[newKey].push(key);
347
+ }
348
+ return result;
349
+ }
350
+ __name(invertBy, "invertBy");
351
+ function transform(obj, fn) {
352
+ if (!isObject(obj)) return {};
353
+ return Object.fromEntries(Object.entries(obj).map(([k, v]) => [
354
+ k,
355
+ fn(v, k)
356
+ ]));
357
+ }
358
+ __name(transform, "transform");
288
359
 
289
- export { deepClone, deepFreeze, deepObjMerge, filterObject, flattenObject, getAllPaths, getValueByPath, isEqual, isObjEmpty, isObject, isPlainObject, mapObject, omit, pick, setValueByPath };
360
+ export { deepClone, deepFreeze, deepObjMerge, filterObject, flattenObject, getAllPaths, getValueByPath, invert, invertBy, isEqual, isObjEmpty, isObject, isPlainObject, mapObject, omit, pick, setValueByPath, transform };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@catbee/utils",
3
- "version": "2.0.0-next.1",
3
+ "version": "2.0.0",
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
5
  "publishConfig": {
6
6
  "access": "public"
@@ -57,6 +57,24 @@
57
57
  "LICENSE",
58
58
  "README.md"
59
59
  ],
60
+ "keywords": [
61
+ "catbee",
62
+ "catbee-utils",
63
+ "utils",
64
+ "utilities",
65
+ "helper",
66
+ "helpers",
67
+ "tools",
68
+ "typescript",
69
+ "nodejs",
70
+ "node",
71
+ "esm",
72
+ "commonjs",
73
+ "express",
74
+ "expressjs",
75
+ "express-server",
76
+ "developer-tools"
77
+ ],
60
78
  "exports": {
61
79
  ".": {
62
80
  "import": "./index.mjs",
@@ -98,15 +116,15 @@
98
116
  "require": "./date/index.cjs",
99
117
  "types": "./date/index.d.ts"
100
118
  },
101
- "./decorators": {
102
- "import": "./decorators/index.mjs",
103
- "require": "./decorators/index.cjs",
104
- "types": "./decorators/index.d.ts"
119
+ "./decorator": {
120
+ "import": "./decorator/index.mjs",
121
+ "require": "./decorator/index.cjs",
122
+ "types": "./decorator/index.d.ts"
105
123
  },
106
- "./dir": {
107
- "import": "./dir/index.mjs",
108
- "require": "./dir/index.cjs",
109
- "types": "./dir/index.d.ts"
124
+ "./directory": {
125
+ "import": "./directory/index.mjs",
126
+ "require": "./directory/index.cjs",
127
+ "types": "./directory/index.d.ts"
110
128
  },
111
129
  "./env": {
112
130
  "import": "./env/index.mjs",
@@ -143,10 +161,10 @@
143
161
  "require": "./middleware/index.cjs",
144
162
  "types": "./middleware/index.d.ts"
145
163
  },
146
- "./obj": {
147
- "import": "./obj/index.mjs",
148
- "require": "./obj/index.cjs",
149
- "types": "./obj/index.d.ts"
164
+ "./object": {
165
+ "import": "./object/index.mjs",
166
+ "require": "./object/index.cjs",
167
+ "types": "./object/index.d.ts"
150
168
  },
151
169
  "./performance": {
152
170
  "import": "./performance/index.mjs",
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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
@@ -201,7 +201,7 @@ function trackMemoryUsage(fn, options = {}) {
201
201
  const afterMemory = process.memoryUsage();
202
202
  const diff = {};
203
203
  for (const key in afterMemory) {
204
- if (Object.prototype.hasOwnProperty.call(afterMemory, key)) {
204
+ if (Object.hasOwn(afterMemory, key)) {
205
205
  diff[key] = afterMemory[key] - beforeMemory[key];
206
206
  }
207
207
  }
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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
@@ -199,7 +199,7 @@ function trackMemoryUsage(fn, options = {}) {
199
199
  const afterMemory = process.memoryUsage();
200
200
  const diff = {};
201
201
  for (const key in afterMemory) {
202
- if (Object.prototype.hasOwnProperty.call(afterMemory, key)) {
202
+ if (Object.hasOwn(afterMemory, key)) {
203
203
  diff[key] = afterMemory[key] - beforeMemory[key];
204
204
  }
205
205
  }
package/request/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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
@@ -143,38 +143,50 @@ function extractPaginationParams(query, defaultPage = 1, defaultLimit = 20, maxL
143
143
  };
144
144
  }
145
145
  __name(extractPaginationParams, "extractPaginationParams");
146
- function extractSortParams(query, allowedFields, defaultSort = {
146
+ var DEFAULT_SORT = {
147
147
  sortBy: "createdAt",
148
148
  sortOrder: "desc"
149
- }) {
150
- let sortBy = defaultSort.sortBy;
151
- let sortOrder = defaultSort.sortOrder;
152
- if (typeof query.sortBy === "string") {
153
- sortBy = query.sortBy;
154
- } else if (Array.isArray(query.sortBy)) {
155
- sortBy = query.sortBy[0];
156
- } else if (typeof query.sort === "string") {
157
- sortBy = query.sort.startsWith("-") ? query.sort.slice(1) : query.sort;
158
- sortOrder = query.sort.startsWith("-") ? "desc" : "asc";
159
- } else if (Array.isArray(query.sort)) {
160
- sortBy = query.sort[0].startsWith("-") ? query.sort[0].slice(1) : query.sort[0];
161
- sortOrder = query.sort[0].startsWith("-") ? "desc" : "asc";
162
- }
163
- if (typeof query.sortOrder === "string") {
164
- const order = query.sortOrder.toLowerCase();
165
- if (order === "asc" || order === "desc") {
166
- sortOrder = order;
167
- }
168
- }
169
- if (allowedFields.length && !allowedFields.includes(sortBy)) {
170
- sortBy = defaultSort.sortBy;
149
+ };
150
+ function extractSortParams(query, allowedFields, defaultSort = DEFAULT_SORT) {
151
+ const inferredSortBy = getSortByFromQuery(query, defaultSort);
152
+ let sortOrder = getSortOrderFromQuery(query, defaultSort);
153
+ const explicit = applyExplicitSortOrder(query.sortOrder);
154
+ if (explicit) {
155
+ sortOrder = explicit;
171
156
  }
157
+ const sortBy = validateSortField(inferredSortBy, allowedFields, defaultSort.sortBy);
172
158
  return {
173
159
  sortBy,
174
160
  sortOrder
175
161
  };
176
162
  }
177
163
  __name(extractSortParams, "extractSortParams");
164
+ function getSortByFromQuery(query, defaultSort) {
165
+ if (typeof query.sortBy === "string") return query.sortBy;
166
+ if (Array.isArray(query.sortBy)) return query.sortBy[0];
167
+ if (typeof query.sort === "string") return query.sort.startsWith("-") ? query.sort.slice(1) : query.sort;
168
+ if (Array.isArray(query.sort)) return query.sort[0].startsWith("-") ? query.sort[0].slice(1) : query.sort[0];
169
+ return defaultSort.sortBy;
170
+ }
171
+ __name(getSortByFromQuery, "getSortByFromQuery");
172
+ function getSortOrderFromQuery(query, defaultSort) {
173
+ if (typeof query.sort === "string") return query.sort.startsWith("-") ? "desc" : "asc";
174
+ if (Array.isArray(query.sort)) return query.sort[0].startsWith("-") ? "desc" : "asc";
175
+ return defaultSort.sortOrder;
176
+ }
177
+ __name(getSortOrderFromQuery, "getSortOrderFromQuery");
178
+ function applyExplicitSortOrder(sortOrderParam) {
179
+ if (typeof sortOrderParam !== "string") return null;
180
+ const order = sortOrderParam.toLowerCase();
181
+ if (order === "asc" || order === "desc") return order;
182
+ return null;
183
+ }
184
+ __name(applyExplicitSortOrder, "applyExplicitSortOrder");
185
+ function validateSortField(sortBy, allowedFields, defaultSortBy) {
186
+ if (allowedFields.length && !allowedFields.includes(sortBy)) return defaultSortBy;
187
+ return sortBy;
188
+ }
189
+ __name(validateSortField, "validateSortField");
178
190
  function extractFilterParams(query, allowedFilters) {
179
191
  const filters = {};
180
192
  for (const field of allowedFilters) {
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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
package/request/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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
@@ -141,38 +141,50 @@ function extractPaginationParams(query, defaultPage = 1, defaultLimit = 20, maxL
141
141
  };
142
142
  }
143
143
  __name(extractPaginationParams, "extractPaginationParams");
144
- function extractSortParams(query, allowedFields, defaultSort = {
144
+ var DEFAULT_SORT = {
145
145
  sortBy: "createdAt",
146
146
  sortOrder: "desc"
147
- }) {
148
- let sortBy = defaultSort.sortBy;
149
- let sortOrder = defaultSort.sortOrder;
150
- if (typeof query.sortBy === "string") {
151
- sortBy = query.sortBy;
152
- } else if (Array.isArray(query.sortBy)) {
153
- sortBy = query.sortBy[0];
154
- } else if (typeof query.sort === "string") {
155
- sortBy = query.sort.startsWith("-") ? query.sort.slice(1) : query.sort;
156
- sortOrder = query.sort.startsWith("-") ? "desc" : "asc";
157
- } else if (Array.isArray(query.sort)) {
158
- sortBy = query.sort[0].startsWith("-") ? query.sort[0].slice(1) : query.sort[0];
159
- sortOrder = query.sort[0].startsWith("-") ? "desc" : "asc";
160
- }
161
- if (typeof query.sortOrder === "string") {
162
- const order = query.sortOrder.toLowerCase();
163
- if (order === "asc" || order === "desc") {
164
- sortOrder = order;
165
- }
166
- }
167
- if (allowedFields.length && !allowedFields.includes(sortBy)) {
168
- sortBy = defaultSort.sortBy;
147
+ };
148
+ function extractSortParams(query, allowedFields, defaultSort = DEFAULT_SORT) {
149
+ const inferredSortBy = getSortByFromQuery(query, defaultSort);
150
+ let sortOrder = getSortOrderFromQuery(query, defaultSort);
151
+ const explicit = applyExplicitSortOrder(query.sortOrder);
152
+ if (explicit) {
153
+ sortOrder = explicit;
169
154
  }
155
+ const sortBy = validateSortField(inferredSortBy, allowedFields, defaultSort.sortBy);
170
156
  return {
171
157
  sortBy,
172
158
  sortOrder
173
159
  };
174
160
  }
175
161
  __name(extractSortParams, "extractSortParams");
162
+ function getSortByFromQuery(query, defaultSort) {
163
+ if (typeof query.sortBy === "string") return query.sortBy;
164
+ if (Array.isArray(query.sortBy)) return query.sortBy[0];
165
+ if (typeof query.sort === "string") return query.sort.startsWith("-") ? query.sort.slice(1) : query.sort;
166
+ if (Array.isArray(query.sort)) return query.sort[0].startsWith("-") ? query.sort[0].slice(1) : query.sort[0];
167
+ return defaultSort.sortBy;
168
+ }
169
+ __name(getSortByFromQuery, "getSortByFromQuery");
170
+ function getSortOrderFromQuery(query, defaultSort) {
171
+ if (typeof query.sort === "string") return query.sort.startsWith("-") ? "desc" : "asc";
172
+ if (Array.isArray(query.sort)) return query.sort[0].startsWith("-") ? "desc" : "asc";
173
+ return defaultSort.sortOrder;
174
+ }
175
+ __name(getSortOrderFromQuery, "getSortOrderFromQuery");
176
+ function applyExplicitSortOrder(sortOrderParam) {
177
+ if (typeof sortOrderParam !== "string") return null;
178
+ const order = sortOrderParam.toLowerCase();
179
+ if (order === "asc" || order === "desc") return order;
180
+ return null;
181
+ }
182
+ __name(applyExplicitSortOrder, "applyExplicitSortOrder");
183
+ function validateSortField(sortBy, allowedFields, defaultSortBy) {
184
+ if (allowedFields.length && !allowedFields.includes(sortBy)) return defaultSortBy;
185
+ return sortBy;
186
+ }
187
+ __name(validateSortField, "validateSortField");
176
188
  function extractFilterParams(query, allowedFilters) {
177
189
  const filters = {};
178
190
  for (const field of allowedFilters) {
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
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