@pawells/typescript-common 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +148 -0
- package/build/array/array-chunk.d.ts +10 -0
- package/build/array/array-chunk.d.ts.map +1 -0
- package/build/array/array-chunk.js +19 -0
- package/build/array/array-chunk.js.map +1 -0
- package/build/array/array-compact.d.ts +18 -0
- package/build/array/array-compact.d.ts.map +1 -0
- package/build/array/array-compact.js +22 -0
- package/build/array/array-compact.js.map +1 -0
- package/build/array/array-contains.d.ts +9 -0
- package/build/array/array-contains.d.ts.map +1 -0
- package/build/array/array-contains.js +13 -0
- package/build/array/array-contains.js.map +1 -0
- package/build/array/array-count-by.d.ts +21 -0
- package/build/array/array-count-by.d.ts.map +1 -0
- package/build/array/array-count-by.js +29 -0
- package/build/array/array-count-by.js.map +1 -0
- package/build/array/array-difference.d.ts +25 -0
- package/build/array/array-difference.d.ts.map +1 -0
- package/build/array/array-difference.js +34 -0
- package/build/array/array-difference.js.map +1 -0
- package/build/array/array-element.d.ts +7 -0
- package/build/array/array-element.d.ts.map +1 -0
- package/build/array/array-element.js +2 -0
- package/build/array/array-element.js.map +1 -0
- package/build/array/array-filter.d.ts +37 -0
- package/build/array/array-filter.d.ts.map +1 -0
- package/build/array/array-filter.js +78 -0
- package/build/array/array-filter.js.map +1 -0
- package/build/array/array-flatten.d.ts +16 -0
- package/build/array/array-flatten.d.ts.map +1 -0
- package/build/array/array-flatten.js +20 -0
- package/build/array/array-flatten.js.map +1 -0
- package/build/array/array-group-by.d.ts +13 -0
- package/build/array/array-group-by.d.ts.map +1 -0
- package/build/array/array-group-by.js +24 -0
- package/build/array/array-group-by.js.map +1 -0
- package/build/array/array-intersection.d.ts +25 -0
- package/build/array/array-intersection.d.ts.map +1 -0
- package/build/array/array-intersection.js +39 -0
- package/build/array/array-intersection.js.map +1 -0
- package/build/array/array-partition.d.ts +19 -0
- package/build/array/array-partition.d.ts.map +1 -0
- package/build/array/array-partition.js +32 -0
- package/build/array/array-partition.js.map +1 -0
- package/build/array/array-range.d.ts +18 -0
- package/build/array/array-range.d.ts.map +1 -0
- package/build/array/array-range.js +30 -0
- package/build/array/array-range.js.map +1 -0
- package/build/array/array-sample.d.ts +29 -0
- package/build/array/array-sample.d.ts.map +1 -0
- package/build/array/array-sample.js +19 -0
- package/build/array/array-sample.js.map +1 -0
- package/build/array/array-shuffle.d.ts +8 -0
- package/build/array/array-shuffle.d.ts.map +1 -0
- package/build/array/array-shuffle.js +19 -0
- package/build/array/array-shuffle.js.map +1 -0
- package/build/array/array-sort-by.d.ts +20 -0
- package/build/array/array-sort-by.d.ts.map +1 -0
- package/build/array/array-sort-by.js +31 -0
- package/build/array/array-sort-by.js.map +1 -0
- package/build/array/array-zip.d.ts +22 -0
- package/build/array/array-zip.d.ts.map +1 -0
- package/build/array/array-zip.js +25 -0
- package/build/array/array-zip.js.map +1 -0
- package/build/array/array.test.d.ts +2 -0
- package/build/array/array.test.d.ts.map +1 -0
- package/build/array/array.test.js +347 -0
- package/build/array/array.test.js.map +1 -0
- package/build/array/index.d.ts +27 -0
- package/build/array/index.d.ts.map +1 -0
- package/build/array/index.js +25 -0
- package/build/array/index.js.map +1 -0
- package/build/array/types.d.ts +32 -0
- package/build/array/types.d.ts.map +1 -0
- package/build/array/types.js +2 -0
- package/build/array/types.js.map +1 -0
- package/build/array/unique.d.ts +8 -0
- package/build/array/unique.d.ts.map +1 -0
- package/build/array/unique.js +13 -0
- package/build/array/unique.js.map +1 -0
- package/build/enum/enum-entries.d.ts +11 -0
- package/build/enum/enum-entries.d.ts.map +1 -0
- package/build/enum/enum-entries.js +14 -0
- package/build/enum/enum-entries.js.map +1 -0
- package/build/enum/enum-key-by-value.d.ts +13 -0
- package/build/enum/enum-key-by-value.d.ts.map +1 -0
- package/build/enum/enum-key-by-value.js +21 -0
- package/build/enum/enum-key-by-value.js.map +1 -0
- package/build/enum/enum-keys.d.ts +10 -0
- package/build/enum/enum-keys.d.ts.map +1 -0
- package/build/enum/enum-keys.js +14 -0
- package/build/enum/enum-keys.js.map +1 -0
- package/build/enum/enum-safe-value.d.ts +14 -0
- package/build/enum/enum-safe-value.d.ts.map +1 -0
- package/build/enum/enum-safe-value.js +16 -0
- package/build/enum/enum-safe-value.js.map +1 -0
- package/build/enum/enum-values.d.ts +19 -0
- package/build/enum/enum-values.d.ts.map +1 -0
- package/build/enum/enum-values.js +25 -0
- package/build/enum/enum-values.js.map +1 -0
- package/build/enum/enum.test.d.ts +2 -0
- package/build/enum/enum.test.d.ts.map +1 -0
- package/build/enum/enum.test.js +122 -0
- package/build/enum/enum.test.js.map +1 -0
- package/build/enum/index.d.ts +17 -0
- package/build/enum/index.d.ts.map +1 -0
- package/build/enum/index.js +17 -0
- package/build/enum/index.js.map +1 -0
- package/build/enum/types.d.ts +9 -0
- package/build/enum/types.d.ts.map +1 -0
- package/build/enum/types.js +2 -0
- package/build/enum/types.js.map +1 -0
- package/build/enum/validate-enum-value.d.ts +13 -0
- package/build/enum/validate-enum-value.d.ts.map +1 -0
- package/build/enum/validate-enum-value.js +18 -0
- package/build/enum/validate-enum-value.js.map +1 -0
- package/build/function/compose.d.ts +37 -0
- package/build/function/compose.d.ts.map +1 -0
- package/build/function/compose.js +7 -0
- package/build/function/compose.js.map +1 -0
- package/build/function/debounce.d.ts +25 -0
- package/build/function/debounce.d.ts.map +1 -0
- package/build/function/debounce.js +37 -0
- package/build/function/debounce.js.map +1 -0
- package/build/function/function.test.d.ts +2 -0
- package/build/function/function.test.d.ts.map +1 -0
- package/build/function/function.test.js +158 -0
- package/build/function/function.test.js.map +1 -0
- package/build/function/index.d.ts +17 -0
- package/build/function/index.d.ts.map +1 -0
- package/build/function/index.js +16 -0
- package/build/function/index.js.map +1 -0
- package/build/function/memoize.d.ts +23 -0
- package/build/function/memoize.d.ts.map +1 -0
- package/build/function/memoize.js +34 -0
- package/build/function/memoize.js.map +1 -0
- package/build/function/once.d.ts +17 -0
- package/build/function/once.d.ts.map +1 -0
- package/build/function/once.js +27 -0
- package/build/function/once.js.map +1 -0
- package/build/function/sleep.d.ts +20 -0
- package/build/function/sleep.d.ts.map +1 -0
- package/build/function/sleep.js +22 -0
- package/build/function/sleep.js.map +1 -0
- package/build/function/throttle.d.ts +17 -0
- package/build/function/throttle.d.ts.map +1 -0
- package/build/function/throttle.js +37 -0
- package/build/function/throttle.js.map +1 -0
- package/build/function/types.d.ts +5 -0
- package/build/function/types.d.ts.map +1 -0
- package/build/function/types.js +2 -0
- package/build/function/types.js.map +1 -0
- package/build/index.d.ts +32 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +51 -0
- package/build/index.js.map +1 -0
- package/build/object/assert-object.d.ts +8 -0
- package/build/object/assert-object.d.ts.map +1 -0
- package/build/object/assert-object.js +10 -0
- package/build/object/assert-object.js.map +1 -0
- package/build/object/clone.d.ts +39 -0
- package/build/object/clone.d.ts.map +1 -0
- package/build/object/clone.js +85 -0
- package/build/object/clone.js.map +1 -0
- package/build/object/equals.d.ts +44 -0
- package/build/object/equals.d.ts.map +1 -0
- package/build/object/equals.js +104 -0
- package/build/object/equals.js.map +1 -0
- package/build/object/filter-cached.d.ts +9 -0
- package/build/object/filter-cached.d.ts.map +1 -0
- package/build/object/filter-cached.js +108 -0
- package/build/object/filter-cached.js.map +1 -0
- package/build/object/filter.d.ts +85 -0
- package/build/object/filter.d.ts.map +1 -0
- package/build/object/filter.js +248 -0
- package/build/object/filter.js.map +1 -0
- package/build/object/has-circular-reference.d.ts +8 -0
- package/build/object/has-circular-reference.d.ts.map +1 -0
- package/build/object/has-circular-reference.js +40 -0
- package/build/object/has-circular-reference.js.map +1 -0
- package/build/object/hash.d.ts +20 -0
- package/build/object/hash.d.ts.map +1 -0
- package/build/object/hash.js +39 -0
- package/build/object/hash.js.map +1 -0
- package/build/object/index.d.ts +32 -0
- package/build/object/index.d.ts.map +1 -0
- package/build/object/index.js +30 -0
- package/build/object/index.js.map +1 -0
- package/build/object/key-value-pairs.d.ts +21 -0
- package/build/object/key-value-pairs.d.ts.map +1 -0
- package/build/object/key-value-pairs.js +28 -0
- package/build/object/key-value-pairs.js.map +1 -0
- package/build/object/map-cached.d.ts +9 -0
- package/build/object/map-cached.d.ts.map +1 -0
- package/build/object/map-cached.js +97 -0
- package/build/object/map-cached.js.map +1 -0
- package/build/object/map.d.ts +29 -0
- package/build/object/map.d.ts.map +1 -0
- package/build/object/map.js +40 -0
- package/build/object/map.js.map +1 -0
- package/build/object/merge.d.ts +14 -0
- package/build/object/merge.d.ts.map +1 -0
- package/build/object/merge.js +41 -0
- package/build/object/merge.js.map +1 -0
- package/build/object/object-diff.d.ts +40 -0
- package/build/object/object-diff.d.ts.map +1 -0
- package/build/object/object-diff.js +45 -0
- package/build/object/object-diff.js.map +1 -0
- package/build/object/object-flatten.d.ts +22 -0
- package/build/object/object-flatten.d.ts.map +1 -0
- package/build/object/object-flatten.js +38 -0
- package/build/object/object-flatten.js.map +1 -0
- package/build/object/object-invert.d.ts +20 -0
- package/build/object/object-invert.d.ts.map +1 -0
- package/build/object/object-invert.js +26 -0
- package/build/object/object-invert.js.map +1 -0
- package/build/object/object.test.d.ts +2 -0
- package/build/object/object.test.d.ts.map +1 -0
- package/build/object/object.test.js +432 -0
- package/build/object/object.test.js.map +1 -0
- package/build/object/omit.d.ts +8 -0
- package/build/object/omit.d.ts.map +1 -0
- package/build/object/omit.js +17 -0
- package/build/object/omit.js.map +1 -0
- package/build/object/pick.d.ts +50 -0
- package/build/object/pick.d.ts.map +1 -0
- package/build/object/pick.js +60 -0
- package/build/object/pick.js.map +1 -0
- package/build/object/property-paths.d.ts +115 -0
- package/build/object/property-paths.d.ts.map +1 -0
- package/build/object/property-paths.js +170 -0
- package/build/object/property-paths.js.map +1 -0
- package/build/object/security-utils.d.ts +59 -0
- package/build/object/security-utils.d.ts.map +1 -0
- package/build/object/security-utils.js +165 -0
- package/build/object/security-utils.js.map +1 -0
- package/build/object/sort-keys.d.ts +26 -0
- package/build/object/sort-keys.d.ts.map +1 -0
- package/build/object/sort-keys.js +52 -0
- package/build/object/sort-keys.js.map +1 -0
- package/build/object/types.d.ts +152 -0
- package/build/object/types.d.ts.map +1 -0
- package/build/object/types.js +6 -0
- package/build/object/types.js.map +1 -0
- package/build/string/case-conversion.d.ts +50 -0
- package/build/string/case-conversion.d.ts.map +1 -0
- package/build/string/case-conversion.js +94 -0
- package/build/string/case-conversion.js.map +1 -0
- package/build/string/formatting.d.ts +108 -0
- package/build/string/formatting.d.ts.map +1 -0
- package/build/string/formatting.js +171 -0
- package/build/string/formatting.js.map +1 -0
- package/build/string/index.d.ts +15 -0
- package/build/string/index.d.ts.map +1 -0
- package/build/string/index.js +14 -0
- package/build/string/index.js.map +1 -0
- package/build/string/string.test.d.ts +2 -0
- package/build/string/string.test.d.ts.map +1 -0
- package/build/string/string.test.js +321 -0
- package/build/string/string.test.js.map +1 -0
- package/build/string/transformation.d.ts +18 -0
- package/build/string/transformation.d.ts.map +1 -0
- package/build/string/transformation.js +32 -0
- package/build/string/transformation.js.map +1 -0
- package/build/string/types.d.ts +44 -0
- package/build/string/types.d.ts.map +1 -0
- package/build/string/types.js +2 -0
- package/build/string/types.js.map +1 -0
- package/build/string/validation.d.ts +18 -0
- package/build/string/validation.d.ts.map +1 -0
- package/build/string/validation.js +26 -0
- package/build/string/validation.js.map +1 -0
- package/build/time/elapsed-time/constants.d.ts +10 -0
- package/build/time/elapsed-time/constants.d.ts.map +1 -0
- package/build/time/elapsed-time/constants.js +96 -0
- package/build/time/elapsed-time/constants.js.map +1 -0
- package/build/time/elapsed-time/elapsed-time.d.ts +412 -0
- package/build/time/elapsed-time/elapsed-time.d.ts.map +1 -0
- package/build/time/elapsed-time/elapsed-time.js +652 -0
- package/build/time/elapsed-time/elapsed-time.js.map +1 -0
- package/build/time/elapsed-time/types.d.ts +150 -0
- package/build/time/elapsed-time/types.d.ts.map +1 -0
- package/build/time/elapsed-time/types.js +2 -0
- package/build/time/elapsed-time/types.js.map +1 -0
- package/build/time/elapsed-time/utils.d.ts +18 -0
- package/build/time/elapsed-time/utils.d.ts.map +1 -0
- package/build/time/elapsed-time/utils.js +24 -0
- package/build/time/elapsed-time/utils.js.map +1 -0
- package/build/time/index.d.ts +18 -0
- package/build/time/index.d.ts.map +1 -0
- package/build/time/index.js +17 -0
- package/build/time/index.js.map +1 -0
- package/build/time/stopwatch/entry-types.d.ts +13 -0
- package/build/time/stopwatch/entry-types.d.ts.map +1 -0
- package/build/time/stopwatch/entry-types.js +2 -0
- package/build/time/stopwatch/entry-types.js.map +1 -0
- package/build/time/stopwatch/entry.d.ts +80 -0
- package/build/time/stopwatch/entry.d.ts.map +1 -0
- package/build/time/stopwatch/entry.js +105 -0
- package/build/time/stopwatch/entry.js.map +1 -0
- package/build/time/stopwatch/stopwatch.d.ts +232 -0
- package/build/time/stopwatch/stopwatch.d.ts.map +1 -0
- package/build/time/stopwatch/stopwatch.js +315 -0
- package/build/time/stopwatch/stopwatch.js.map +1 -0
- package/build/time/time.test.d.ts +2 -0
- package/build/time/time.test.d.ts.map +1 -0
- package/build/time/time.test.js +211 -0
- package/build/time/time.test.js.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a hash of an object using SHA-256.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The object to hash
|
|
5
|
+
* @param hashFunction - Optional custom hash function (default: SHA-256)
|
|
6
|
+
* @returns A hash string of the object
|
|
7
|
+
* @throws {Error} When the object contains circular references
|
|
8
|
+
* @throws {Error} When the object cannot be serialized (e.g., undefined, functions, symbols)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Default SHA-256 hash
|
|
13
|
+
* const hash = ObjectHash({ name: 'test', value: 42 });
|
|
14
|
+
*
|
|
15
|
+
* // Using custom hash function
|
|
16
|
+
* const customHash = ObjectHash({ name: 'test', value: 42 }, (data) => customHashFunction(data));
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function ObjectHash(obj: any, hashFunction?: (data: string) => string): string;
|
|
20
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/object/hash.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAqBpF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { ObjectHasCircularReference } from './has-circular-reference.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a hash of an object using SHA-256.
|
|
5
|
+
*
|
|
6
|
+
* @param obj - The object to hash
|
|
7
|
+
* @param hashFunction - Optional custom hash function (default: SHA-256)
|
|
8
|
+
* @returns A hash string of the object
|
|
9
|
+
* @throws {Error} When the object contains circular references
|
|
10
|
+
* @throws {Error} When the object cannot be serialized (e.g., undefined, functions, symbols)
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Default SHA-256 hash
|
|
15
|
+
* const hash = ObjectHash({ name: 'test', value: 42 });
|
|
16
|
+
*
|
|
17
|
+
* // Using custom hash function
|
|
18
|
+
* const customHash = ObjectHash({ name: 'test', value: 42 }, (data) => customHashFunction(data));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function ObjectHash(obj, hashFunction) {
|
|
22
|
+
// Check for circular references before attempting to stringify
|
|
23
|
+
if (ObjectHasCircularReference(obj)) {
|
|
24
|
+
throw new Error('Cannot hash object with circular references');
|
|
25
|
+
}
|
|
26
|
+
const jsonString = JSON.stringify(obj);
|
|
27
|
+
// Handle cases where JSON.stringify returns undefined
|
|
28
|
+
if (jsonString === undefined) {
|
|
29
|
+
throw new Error('Cannot hash object: JSON.stringify returned undefined');
|
|
30
|
+
}
|
|
31
|
+
// Use provided hash function or default to SHA-256
|
|
32
|
+
if (hashFunction) {
|
|
33
|
+
return hashFunction(jsonString);
|
|
34
|
+
}
|
|
35
|
+
// Default to SHA-256 base64
|
|
36
|
+
const hash = createHash('sha256').update(jsonString).digest('base64');
|
|
37
|
+
return hash;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=hash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/object/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AAEzE;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,UAAU,CAAC,GAAQ,EAAE,YAAuC;IAC3E,+DAA+D;IAC/D,IAAI,0BAA0B,CAAC,GAAG,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IAChE,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAEvC,sDAAsD;IACtD,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAC1E,CAAC;IAED,mDAAmD;IACnD,IAAI,YAAY,EAAE,CAAC;QAClB,OAAO,YAAY,CAAC,UAAU,CAAC,CAAC;IACjC,CAAC;IAED,4BAA4B;IAC5B,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACtE,OAAO,IAAI,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object utility functions and types.
|
|
3
|
+
*
|
|
4
|
+
* Provides helpers for cloning, comparing, filtering, mapping, merging,
|
|
5
|
+
* picking, omitting, hashing, and traversing plain objects. Security-hardened
|
|
6
|
+
* implementations guard against prototype-pollution and path-traversal attacks.
|
|
7
|
+
*
|
|
8
|
+
* @module object
|
|
9
|
+
*/
|
|
10
|
+
export { ObjectHasCircularReference } from './has-circular-reference.js';
|
|
11
|
+
export { ObjectClone, TransformObject } from './clone.js';
|
|
12
|
+
export { ObjectEquals } from './equals.js';
|
|
13
|
+
export { ObjectFilter, FilterObject } from './filter.js';
|
|
14
|
+
export { ObjectFilterCached } from './filter-cached.js';
|
|
15
|
+
export { MapObject } from './map.js';
|
|
16
|
+
export { MapObjectCached } from './map-cached.js';
|
|
17
|
+
export { ObjectHash } from './hash.js';
|
|
18
|
+
export { ObjectGetPropertyByPath, ObjectSetPropertyByPath } from './property-paths.js';
|
|
19
|
+
export { ObjectSortKeys } from './sort-keys.js';
|
|
20
|
+
export { ObjectFromKeyValuePairs, ObjectToKeyValuePairs } from './key-value-pairs.js';
|
|
21
|
+
export { ObjectPick } from './pick.js';
|
|
22
|
+
export { ObjectOmit } from './omit.js';
|
|
23
|
+
export { ObjectMerge } from './merge.js';
|
|
24
|
+
export { AssertObject } from './assert-object.js';
|
|
25
|
+
export { isPropertyKeySafe, isPropertyPathSafe, sanitizePropertyKey, filterDangerousKeys, createCircularReferenceDetector, isInputSafe } from './security-utils.js';
|
|
26
|
+
export { ObjectInvert } from './object-invert.js';
|
|
27
|
+
export { ObjectFlatten } from './object-flatten.js';
|
|
28
|
+
export { ObjectDiff } from './object-diff.js';
|
|
29
|
+
export type { IObjectDiffResult } from './object-diff.js';
|
|
30
|
+
export type { TConstructableObject, TObjectOmitExtraProperties, TCachedObjectFilterFunction, TCachedObjectMapperFunction, TObjectPredicate, TObjectTransformer, TObjectComparator, TObjectEqualityComparator, TPropertyMapper, TPropertyFilter } from './types.js';
|
|
31
|
+
export type { IObjectFilterOptions, ICachedObjectFilterOptions, ICachedObjectMapOptions } from './types.js';
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AACtF,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,+BAA+B,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACpK,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAG1D,YAAY,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,2BAA2B,EAAE,2BAA2B,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,yBAAyB,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAGnQ,YAAY,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object utility functions and types.
|
|
3
|
+
*
|
|
4
|
+
* Provides helpers for cloning, comparing, filtering, mapping, merging,
|
|
5
|
+
* picking, omitting, hashing, and traversing plain objects. Security-hardened
|
|
6
|
+
* implementations guard against prototype-pollution and path-traversal attacks.
|
|
7
|
+
*
|
|
8
|
+
* @module object
|
|
9
|
+
*/
|
|
10
|
+
// Functions
|
|
11
|
+
export { ObjectHasCircularReference } from './has-circular-reference.js';
|
|
12
|
+
export { ObjectClone, TransformObject } from './clone.js';
|
|
13
|
+
export { ObjectEquals } from './equals.js';
|
|
14
|
+
export { ObjectFilter, FilterObject } from './filter.js';
|
|
15
|
+
export { ObjectFilterCached } from './filter-cached.js';
|
|
16
|
+
export { MapObject } from './map.js';
|
|
17
|
+
export { MapObjectCached } from './map-cached.js';
|
|
18
|
+
export { ObjectHash } from './hash.js';
|
|
19
|
+
export { ObjectGetPropertyByPath, ObjectSetPropertyByPath } from './property-paths.js';
|
|
20
|
+
export { ObjectSortKeys } from './sort-keys.js';
|
|
21
|
+
export { ObjectFromKeyValuePairs, ObjectToKeyValuePairs } from './key-value-pairs.js';
|
|
22
|
+
export { ObjectPick } from './pick.js';
|
|
23
|
+
export { ObjectOmit } from './omit.js';
|
|
24
|
+
export { ObjectMerge } from './merge.js';
|
|
25
|
+
export { AssertObject } from './assert-object.js';
|
|
26
|
+
export { isPropertyKeySafe, isPropertyPathSafe, sanitizePropertyKey, filterDangerousKeys, createCircularReferenceDetector, isInputSafe } from './security-utils.js';
|
|
27
|
+
export { ObjectInvert } from './object-invert.js';
|
|
28
|
+
export { ObjectFlatten } from './object-flatten.js';
|
|
29
|
+
export { ObjectDiff } from './object-diff.js';
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,YAAY;AACZ,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AACtF,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,+BAA+B,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACpK,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an object from an array of key-value pairs
|
|
3
|
+
* @param entries Array of [key, value] tuples
|
|
4
|
+
* @returns Object with keys and values from the entries
|
|
5
|
+
*/
|
|
6
|
+
export declare function ObjectFromKeyValuePairs<T = unknown>(entries: [string, T][]): Record<string, T>;
|
|
7
|
+
/**
|
|
8
|
+
* Converts an object into an array of `[key, value]` tuples.
|
|
9
|
+
* The inverse of {@link ObjectFromKeyValuePairs}.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The value type
|
|
12
|
+
* @param obj - The object to convert
|
|
13
|
+
* @returns An array of `[key, value]` pairs
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* ObjectToKeyValuePairs({ a: 1, b: 2 }); // [['a', 1], ['b', 2]]
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function ObjectToKeyValuePairs<T = unknown>(obj: Record<string, T>): [string, T][];
|
|
21
|
+
//# sourceMappingURL=key-value-pairs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-value-pairs.d.ts","sourceRoot":"","sources":["../../src/object/key-value-pairs.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAK9F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,CAExF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an object from an array of key-value pairs
|
|
3
|
+
* @param entries Array of [key, value] tuples
|
|
4
|
+
* @returns Object with keys and values from the entries
|
|
5
|
+
*/
|
|
6
|
+
export function ObjectFromKeyValuePairs(entries) {
|
|
7
|
+
return entries.reduce((obj, [key, value]) => {
|
|
8
|
+
obj[key] = value;
|
|
9
|
+
return obj;
|
|
10
|
+
}, {});
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Converts an object into an array of `[key, value]` tuples.
|
|
14
|
+
* The inverse of {@link ObjectFromKeyValuePairs}.
|
|
15
|
+
*
|
|
16
|
+
* @template T - The value type
|
|
17
|
+
* @param obj - The object to convert
|
|
18
|
+
* @returns An array of `[key, value]` pairs
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* ObjectToKeyValuePairs({ a: 1, b: 2 }); // [['a', 1], ['b', 2]]
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function ObjectToKeyValuePairs(obj) {
|
|
26
|
+
return Object.entries(obj);
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=key-value-pairs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-value-pairs.js","sourceRoot":"","sources":["../../src/object/key-value-pairs.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAc,OAAsB;IAC1E,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;QAC3C,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACjB,OAAO,GAAG,CAAC;IACZ,CAAC,EAAE,EAAuB,CAAC,CAAC;AAC7B,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,qBAAqB,CAAc,GAAsB;IACxE,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAkB,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { TPropertyMapper, ICachedObjectMapOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a cached version of MapObject for improved performance when mapping the same objects repeatedly
|
|
4
|
+
*
|
|
5
|
+
* @param options Configuration options for the cache
|
|
6
|
+
* @returns A cached version of the map function
|
|
7
|
+
*/
|
|
8
|
+
export declare function MapObjectCached<T extends object>(options?: ICachedObjectMapOptions): (cursor: T, mapper: TPropertyMapper<T>, mapperKey?: string) => Promise<Record<keyof T, unknown>>;
|
|
9
|
+
//# sourceMappingURL=map-cached.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-cached.d.ts","sourceRoot":"","sources":["../../src/object/map-cached.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAO3E;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,EAAE,OAAO,GAAE,uBAA4B,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC,CAkGzL"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { MapObject } from './map.js';
|
|
2
|
+
import { ObjectHash } from './hash.js';
|
|
3
|
+
// Cache configuration constants
|
|
4
|
+
const DEFAULT_MAX_CACHE_SIZE = 1000;
|
|
5
|
+
const CACHE_EVICTION_PERCENTAGE = 0.2; // 20%
|
|
6
|
+
const INITIAL_CACHE_HASH_LENGTH = 16;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a cached version of MapObject for improved performance when mapping the same objects repeatedly
|
|
9
|
+
*
|
|
10
|
+
* @param options Configuration options for the cache
|
|
11
|
+
* @returns A cached version of the map function
|
|
12
|
+
*/
|
|
13
|
+
export function MapObjectCached(options = {}) {
|
|
14
|
+
const { maxCacheSize = DEFAULT_MAX_CACHE_SIZE } = options;
|
|
15
|
+
const cache = new Map();
|
|
16
|
+
let totalCachedEntries = 0;
|
|
17
|
+
/**
|
|
18
|
+
* Evicts approximately 20% of entries from the largest per-mapper cache bucket
|
|
19
|
+
* when the total number of cached results exceeds `maxCacheSize`.
|
|
20
|
+
*
|
|
21
|
+
* The eviction strategy targets the most-populated bucket to reclaim the most
|
|
22
|
+
* memory in a single pass while preserving results for other active mappers.
|
|
23
|
+
*/
|
|
24
|
+
const clearOldestEntries = () => {
|
|
25
|
+
if (cache.size === 0)
|
|
26
|
+
return;
|
|
27
|
+
// Find the cache with the most entries
|
|
28
|
+
let largestCache = null;
|
|
29
|
+
let largestCacheKey = '';
|
|
30
|
+
let largestSize = 0;
|
|
31
|
+
for (const [key, objectCache] of cache.entries()) {
|
|
32
|
+
if (objectCache.size > largestSize) {
|
|
33
|
+
largestCache = objectCache;
|
|
34
|
+
largestCacheKey = key;
|
|
35
|
+
largestSize = objectCache.size;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (largestCache) {
|
|
39
|
+
// Remove approximately 20% of entries from the largest cache
|
|
40
|
+
const entriesToRemove = Math.ceil(largestSize * CACHE_EVICTION_PERCENTAGE);
|
|
41
|
+
let removed = 0;
|
|
42
|
+
for (const key of largestCache.keys()) {
|
|
43
|
+
largestCache.delete(key);
|
|
44
|
+
removed++;
|
|
45
|
+
if (removed >= entriesToRemove)
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
// If the cache is now empty, remove it completely
|
|
49
|
+
if (largestCache.size === 0) {
|
|
50
|
+
cache.delete(largestCacheKey);
|
|
51
|
+
}
|
|
52
|
+
totalCachedEntries -= removed;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
return (cursor, mapper, mapperKey) => {
|
|
56
|
+
// Validate inputs
|
|
57
|
+
if (!cursor || typeof cursor !== 'object') {
|
|
58
|
+
return Promise.resolve({});
|
|
59
|
+
}
|
|
60
|
+
// Use the explicitly provided mapperKey when available.
|
|
61
|
+
// WARNING: If mapperKey is omitted and mapper is a closure, two closures with
|
|
62
|
+
// identical source code but different captured values will incorrectly share cache
|
|
63
|
+
// entries. Always provide an explicit mapperKey when using closure mappers.
|
|
64
|
+
const resolvedMapperKey = mapperKey ?? mapper.toString();
|
|
65
|
+
// Try to get the object cache for this mapper
|
|
66
|
+
let objectCache = cache.get(resolvedMapperKey);
|
|
67
|
+
if (!objectCache) {
|
|
68
|
+
objectCache = new Map();
|
|
69
|
+
cache.set(resolvedMapperKey, objectCache);
|
|
70
|
+
}
|
|
71
|
+
// Create optimized hash-based cache key for the object being mapped
|
|
72
|
+
let objectKey;
|
|
73
|
+
try {
|
|
74
|
+
objectKey = ObjectHash(cursor).substring(0, INITIAL_CACHE_HASH_LENGTH);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// If object has circular references, skip caching and compute directly
|
|
78
|
+
return Promise.resolve(MapObject(cursor, mapper));
|
|
79
|
+
}
|
|
80
|
+
// Check if we have a cached result
|
|
81
|
+
if (objectCache.has(objectKey)) {
|
|
82
|
+
const cachedResult = objectCache.get(objectKey);
|
|
83
|
+
return Promise.resolve(cachedResult ?? {});
|
|
84
|
+
}
|
|
85
|
+
// Calculate the result
|
|
86
|
+
const result = MapObject(cursor, mapper);
|
|
87
|
+
// Cache the result
|
|
88
|
+
objectCache.set(objectKey, result);
|
|
89
|
+
totalCachedEntries++;
|
|
90
|
+
// Ensure the cache size does not exceed the limit
|
|
91
|
+
if (totalCachedEntries > maxCacheSize) {
|
|
92
|
+
clearOldestEntries();
|
|
93
|
+
}
|
|
94
|
+
return Promise.resolve(result);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=map-cached.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map-cached.js","sourceRoot":"","sources":["../../src/object/map-cached.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAGvC,gCAAgC;AAChC,MAAM,sBAAsB,GAAG,IAAI,CAAC;AACpC,MAAM,yBAAyB,GAAG,GAAG,CAAC,CAAC,MAAM;AAC7C,MAAM,yBAAyB,GAAG,EAAE,CAAC;AAErC;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAmB,UAAmC,EAAE;IACtF,MAAM,EAAE,YAAY,GAAG,sBAAsB,EAAE,GAAG,OAAO,CAAC;IAE1D,MAAM,KAAK,GAAG,IAAI,GAAG,EAAiD,CAAC;IACvE,IAAI,kBAAkB,GAAG,CAAC,CAAC;IAE3B;;;;;;OAMG;IACH,MAAM,kBAAkB,GAAG,GAAS,EAAE;QACrC,IAAI,KAAK,CAAC,IAAI,KAAK,CAAC;YAAE,OAAO;QAE7B,uCAAuC;QACvC,IAAI,YAAY,GAAiD,IAAI,CAAC;QACtE,IAAI,eAAe,GAAG,EAAE,CAAC;QACzB,IAAI,WAAW,GAAG,CAAC,CAAC;QAEpB,KAAK,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;YAClD,IAAI,WAAW,CAAC,IAAI,GAAG,WAAW,EAAE,CAAC;gBACpC,YAAY,GAAG,WAAW,CAAC;gBAC3B,eAAe,GAAG,GAAG,CAAC;gBACtB,WAAW,GAAG,WAAW,CAAC,IAAI,CAAC;YAChC,CAAC;QACF,CAAC;QAED,IAAI,YAAY,EAAE,CAAC;YAClB,6DAA6D;YAC7D,MAAM,eAAe,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,GAAG,yBAAyB,CAAC,CAAC;YAC3E,IAAI,OAAO,GAAG,CAAC,CAAC;YAEhB,KAAK,MAAM,GAAG,IAAI,YAAY,CAAC,IAAI,EAAE,EAAE,CAAC;gBACvC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACzB,OAAO,EAAE,CAAC;gBACV,IAAI,OAAO,IAAI,eAAe;oBAAE,MAAM;YACvC,CAAC;YAED,kDAAkD;YAClD,IAAI,YAAY,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;gBAC7B,KAAK,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;YAC/B,CAAC;YAED,kBAAkB,IAAI,OAAO,CAAC;QAC/B,CAAC;IACF,CAAC,CAAC;IAEF,OAAO,CAAC,MAAS,EAAE,MAA0B,EAAE,SAAkB,EAAqC,EAAE;QACvG,kBAAkB;QAClB,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC3C,OAAO,OAAO,CAAC,OAAO,CAAC,EAA8B,CAAC,CAAC;QACxD,CAAC;QAED,wDAAwD;QACxD,8EAA8E;QAC9E,mFAAmF;QACnF,4EAA4E;QAC5E,MAAM,iBAAiB,GAAG,SAAS,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;QAEzD,8CAA8C;QAC9C,IAAI,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QAC/C,IAAI,CAAC,WAAW,EAAE,CAAC;YAClB,WAAW,GAAG,IAAI,GAAG,EAAoC,CAAC;YAC1D,KAAK,CAAC,GAAG,CAAC,iBAAiB,EAAE,WAAW,CAAC,CAAC;QAC3C,CAAC;QAED,oEAAoE;QACpE,IAAI,SAAiB,CAAC;QAEtB,IAAI,CAAC;YACJ,SAAS,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,yBAAyB,CAAC,CAAC;QACxE,CAAC;QAAC,MAAM,CAAC;YACR,uEAAuE;YACvE,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;QACnD,CAAC;QAED,mCAAmC;QACnC,IAAI,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;YAChC,MAAM,YAAY,GAAG,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAChD,OAAO,OAAO,CAAC,OAAO,CAAC,YAAY,IAAI,EAA8B,CAAC,CAAC;QACxE,CAAC;QAED,uBAAuB;QACvB,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAEzC,mBAAmB;QACnB,WAAW,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACnC,kBAAkB,EAAE,CAAC;QAErB,kDAAkD;QAClD,IAAI,kBAAkB,GAAG,YAAY,EAAE,CAAC;YACvC,kBAAkB,EAAE,CAAC;QACtB,CAAC;QAED,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { TPropertyMapper } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Maps an object's properties using a mapper function.
|
|
4
|
+
* Creates a new object where each property value is transformed by the mapper function.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the source object
|
|
7
|
+
* @param obj - The object to map
|
|
8
|
+
* @param mapper - A function that takes a property key and value, returning the new value
|
|
9
|
+
* @returns A new object with mapped property values
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
14
|
+
* const mapped = MapObject(obj, (key, value) => value * 2);
|
|
15
|
+
* // Result: { a: 2, b: 4, c: 6 }
|
|
16
|
+
*
|
|
17
|
+
* // Type-safe mapping with different value types
|
|
18
|
+
* interface User { id: number; name: string; age: number; }
|
|
19
|
+
* const user: User = { id: 1, name: 'John', age: 30 };
|
|
20
|
+
* const displayData = MapObject(user, (key, value) => {
|
|
21
|
+
* if (key === 'name') return value.toUpperCase();
|
|
22
|
+
* if (key === 'age') return `${value} years old`;
|
|
23
|
+
* return value;
|
|
24
|
+
* });
|
|
25
|
+
* // Result: { id: 1, name: 'JOHN', age: '30 years old' }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function MapObject<T extends object>(obj: T, mapper: TPropertyMapper<T>): Record<keyof T, unknown>;
|
|
29
|
+
//# sourceMappingURL=map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../../src/object/map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAexG"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps an object's properties using a mapper function.
|
|
3
|
+
* Creates a new object where each property value is transformed by the mapper function.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The type of the source object
|
|
6
|
+
* @param obj - The object to map
|
|
7
|
+
* @param mapper - A function that takes a property key and value, returning the new value
|
|
8
|
+
* @returns A new object with mapped property values
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
13
|
+
* const mapped = MapObject(obj, (key, value) => value * 2);
|
|
14
|
+
* // Result: { a: 2, b: 4, c: 6 }
|
|
15
|
+
*
|
|
16
|
+
* // Type-safe mapping with different value types
|
|
17
|
+
* interface User { id: number; name: string; age: number; }
|
|
18
|
+
* const user: User = { id: 1, name: 'John', age: 30 };
|
|
19
|
+
* const displayData = MapObject(user, (key, value) => {
|
|
20
|
+
* if (key === 'name') return value.toUpperCase();
|
|
21
|
+
* if (key === 'age') return `${value} years old`;
|
|
22
|
+
* return value;
|
|
23
|
+
* });
|
|
24
|
+
* // Result: { id: 1, name: 'JOHN', age: '30 years old' }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function MapObject(obj, mapper) {
|
|
28
|
+
if (!obj || typeof obj !== 'object') {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
const result = {};
|
|
32
|
+
for (const key in obj) {
|
|
33
|
+
if (Object.hasOwn(obj, key)) {
|
|
34
|
+
const value = obj[key];
|
|
35
|
+
result[key] = mapper(key, value);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=map.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"map.js","sourceRoot":"","sources":["../../src/object/map.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,SAAS,CAAmB,GAAM,EAAE,MAA0B;IAC7E,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QACrC,OAAO,EAA8B,CAAC;IACvC,CAAC;IAED,MAAM,MAAM,GAA6B,EAA8B,CAAC;IAExE,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACvB,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC;YAC7B,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;YACvB,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAc,EAAE,KAAK,CAAC,CAAC;QAC7C,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merges two objects deeply with security protections
|
|
3
|
+
*
|
|
4
|
+
* **Security Features:**
|
|
5
|
+
* - Prevents prototype pollution by filtering dangerous keys
|
|
6
|
+
* - Safely handles nested object merging
|
|
7
|
+
* - Protects against malicious source objects
|
|
8
|
+
*
|
|
9
|
+
* @param target Target object
|
|
10
|
+
* @param source Source object
|
|
11
|
+
* @returns Merged object
|
|
12
|
+
*/
|
|
13
|
+
export declare function ObjectMerge<T extends object = object>(target: T, source: Partial<T>): T;
|
|
14
|
+
//# sourceMappingURL=merge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.d.ts","sourceRoot":"","sources":["../../src/object/merge.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CA6BvF"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { filterDangerousKeys } from './security-utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Merges two objects deeply with security protections
|
|
4
|
+
*
|
|
5
|
+
* **Security Features:**
|
|
6
|
+
* - Prevents prototype pollution by filtering dangerous keys
|
|
7
|
+
* - Safely handles nested object merging
|
|
8
|
+
* - Protects against malicious source objects
|
|
9
|
+
*
|
|
10
|
+
* @param target Target object
|
|
11
|
+
* @param source Source object
|
|
12
|
+
* @returns Merged object
|
|
13
|
+
*/
|
|
14
|
+
export function ObjectMerge(target, source) {
|
|
15
|
+
if (!target || typeof target !== 'object')
|
|
16
|
+
return target;
|
|
17
|
+
if (!source || typeof source !== 'object')
|
|
18
|
+
return target;
|
|
19
|
+
// Security: Filter out dangerous keys from source object
|
|
20
|
+
const safeSource = filterDangerousKeys(source);
|
|
21
|
+
const output = { ...target };
|
|
22
|
+
Object.keys(safeSource).forEach((key) => {
|
|
23
|
+
// Additional safety check - only process own properties
|
|
24
|
+
if (!Object.prototype.hasOwnProperty.call(safeSource, key)) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const targetValue = output[key];
|
|
28
|
+
const sourceValue = safeSource[key];
|
|
29
|
+
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
|
|
30
|
+
output[key] = [...targetValue, ...sourceValue];
|
|
31
|
+
}
|
|
32
|
+
else if (targetValue && typeof targetValue === 'object' && sourceValue && typeof sourceValue === 'object') {
|
|
33
|
+
output[key] = ObjectMerge(targetValue, sourceValue);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
output[key] = sourceValue;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return output;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=merge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.js","sourceRoot":"","sources":["../../src/object/merge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAE1D;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAA4B,MAAS,EAAE,MAAkB;IACnF,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,MAAW,CAAC;IAC9D,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC;IAEzD,yDAAyD;IACzD,MAAM,UAAU,GAAG,mBAAmB,CAAC,MAA6B,CAAC,CAAC;IAEtE,MAAM,MAAM,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC;IAC7B,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACvC,wDAAwD;QACxD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,CAAC,EAAE,CAAC;YAC5D,OAAO;QACR,CAAC;QAED,MAAM,WAAW,GAAI,MAA8B,CAAC,GAAG,CAAC,CAAC;QACzD,MAAM,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;QAEpC,IAAI,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,CAAC;YAC7D,MAA8B,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,WAAW,EAAE,GAAG,WAAW,CAAC,CAAC;QACzE,CAAC;aAAM,IACN,WAAW,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,WAAW,IAAI,OAAO,WAAW,KAAK,QAAQ,EAC/F,CAAC;YACD,MAA8B,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;QAC9E,CAAC;aAAM,CAAC;YACN,MAA8B,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC;QACpD,CAAC;IACF,CAAC,CAAC,CAAC;IAEH,OAAO,MAAW,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Describes the differences between two objects at the top level. */
|
|
2
|
+
export interface IObjectDiffResult {
|
|
3
|
+
/** Keys present in `objB` but not in `objA`. */
|
|
4
|
+
added: Record<string, unknown>;
|
|
5
|
+
/** Keys present in `objA` but not in `objB`. */
|
|
6
|
+
removed: Record<string, unknown>;
|
|
7
|
+
/** Keys present in both objects whose values differ. */
|
|
8
|
+
changed: Record<string, {
|
|
9
|
+
from: unknown;
|
|
10
|
+
to: unknown;
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Computes a shallow diff between two objects.
|
|
15
|
+
*
|
|
16
|
+
* Returns three groups:
|
|
17
|
+
* - **added** — keys in `objB` that are absent from `objA`
|
|
18
|
+
* - **removed** — keys in `objA` that are absent from `objB`
|
|
19
|
+
* - **changed** — keys present in both whose values are not strictly equal
|
|
20
|
+
* (compared via `JSON.stringify` for deep value equality)
|
|
21
|
+
*
|
|
22
|
+
* @param objA - The baseline object ("before")
|
|
23
|
+
* @param objB - The comparison object ("after")
|
|
24
|
+
* @returns {@link IObjectDiffResult}
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* ObjectDiff(
|
|
29
|
+
* { a: 1, b: 2, c: 3 },
|
|
30
|
+
* { b: 99, c: 3, d: 4 },
|
|
31
|
+
* );
|
|
32
|
+
* // {
|
|
33
|
+
* // added: { d: 4 },
|
|
34
|
+
* // removed: { a: 1 },
|
|
35
|
+
* // changed: { b: { from: 2, to: 99 } },
|
|
36
|
+
* // }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function ObjectDiff(objA: Record<string, unknown>, objB: Record<string, unknown>): IObjectDiffResult;
|
|
40
|
+
//# sourceMappingURL=object-diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object-diff.d.ts","sourceRoot":"","sources":["../../src/object/object-diff.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,MAAM,WAAW,iBAAiB;IACjC,gDAAgD;IAChD,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,gDAAgD;IAChD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,wDAAwD;IACxD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,OAAO,CAAC;QAAC,EAAE,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACxD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,UAAU,CACzB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC3B,iBAAiB,CAkBnB"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computes a shallow diff between two objects.
|
|
3
|
+
*
|
|
4
|
+
* Returns three groups:
|
|
5
|
+
* - **added** — keys in `objB` that are absent from `objA`
|
|
6
|
+
* - **removed** — keys in `objA` that are absent from `objB`
|
|
7
|
+
* - **changed** — keys present in both whose values are not strictly equal
|
|
8
|
+
* (compared via `JSON.stringify` for deep value equality)
|
|
9
|
+
*
|
|
10
|
+
* @param objA - The baseline object ("before")
|
|
11
|
+
* @param objB - The comparison object ("after")
|
|
12
|
+
* @returns {@link IObjectDiffResult}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* ObjectDiff(
|
|
17
|
+
* { a: 1, b: 2, c: 3 },
|
|
18
|
+
* { b: 99, c: 3, d: 4 },
|
|
19
|
+
* );
|
|
20
|
+
* // {
|
|
21
|
+
* // added: { d: 4 },
|
|
22
|
+
* // removed: { a: 1 },
|
|
23
|
+
* // changed: { b: { from: 2, to: 99 } },
|
|
24
|
+
* // }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function ObjectDiff(objA, objB) {
|
|
28
|
+
const result = { added: {}, removed: {}, changed: {} };
|
|
29
|
+
const allKeys = new Set([...Object.keys(objA), ...Object.keys(objB)]);
|
|
30
|
+
for (const key of allKeys) {
|
|
31
|
+
const inA = Object.prototype.hasOwnProperty.call(objA, key);
|
|
32
|
+
const inB = Object.prototype.hasOwnProperty.call(objB, key);
|
|
33
|
+
if (inA && !inB) {
|
|
34
|
+
result.removed[key] = objA[key];
|
|
35
|
+
}
|
|
36
|
+
else if (!inA && inB) {
|
|
37
|
+
result.added[key] = objB[key];
|
|
38
|
+
}
|
|
39
|
+
else if (JSON.stringify(objA[key]) !== JSON.stringify(objB[key])) {
|
|
40
|
+
result.changed[key] = { from: objA[key], to: objB[key] };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=object-diff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object-diff.js","sourceRoot":"","sources":["../../src/object/object-diff.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,UAAU,CACzB,IAA6B,EAC7B,IAA6B;IAE7B,MAAM,MAAM,GAAsB,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;IAC1E,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEtE,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;QAC3B,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAC5D,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAE5D,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACjB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QACjC,CAAC;aAAM,IAAI,CAAC,GAAG,IAAI,GAAG,EAAE,CAAC;YACxB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QAC/B,CAAC;aAAM,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YACpE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1D,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flattens a nested object into a single-level object, joining nested keys
|
|
3
|
+
* with `separator` (default: `'.'`).
|
|
4
|
+
*
|
|
5
|
+
* Array values, `Date` instances, and `null` are treated as leaf values and
|
|
6
|
+
* are not traversed.
|
|
7
|
+
*
|
|
8
|
+
* @param obj - The object to flatten
|
|
9
|
+
* @param separator - The string used to join key segments (default: `'.'`)
|
|
10
|
+
* @returns A new flat object with all nested keys joined by the separator
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* ObjectFlatten({ a: { b: { c: 1 }, d: 2 } });
|
|
15
|
+
* // { 'a.b.c': 1, 'a.d': 2 }
|
|
16
|
+
*
|
|
17
|
+
* ObjectFlatten({ a: { b: 1 } }, '/');
|
|
18
|
+
* // { 'a/b': 1 }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function ObjectFlatten(obj: Record<string, unknown>, separator?: string, _prefix?: string): Record<string, unknown>;
|
|
22
|
+
//# sourceMappingURL=object-flatten.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object-flatten.d.ts","sourceRoot":"","sources":["../../src/object/object-flatten.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAC5B,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,SAAS,SAAM,EACf,OAAO,SAAK,GACV,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAoBzB"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flattens a nested object into a single-level object, joining nested keys
|
|
3
|
+
* with `separator` (default: `'.'`).
|
|
4
|
+
*
|
|
5
|
+
* Array values, `Date` instances, and `null` are treated as leaf values and
|
|
6
|
+
* are not traversed.
|
|
7
|
+
*
|
|
8
|
+
* @param obj - The object to flatten
|
|
9
|
+
* @param separator - The string used to join key segments (default: `'.'`)
|
|
10
|
+
* @returns A new flat object with all nested keys joined by the separator
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* ObjectFlatten({ a: { b: { c: 1 }, d: 2 } });
|
|
15
|
+
* // { 'a.b.c': 1, 'a.d': 2 }
|
|
16
|
+
*
|
|
17
|
+
* ObjectFlatten({ a: { b: 1 } }, '/');
|
|
18
|
+
* // { 'a/b': 1 }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function ObjectFlatten(obj, separator = '.', _prefix = '') {
|
|
22
|
+
const result = {};
|
|
23
|
+
for (const key of Object.keys(obj)) {
|
|
24
|
+
const value = obj[key];
|
|
25
|
+
const newKey = _prefix ? `${_prefix}${separator}${key}` : key;
|
|
26
|
+
if (value !== null &&
|
|
27
|
+
typeof value === 'object' &&
|
|
28
|
+
!Array.isArray(value) &&
|
|
29
|
+
!(value instanceof Date)) {
|
|
30
|
+
Object.assign(result, ObjectFlatten(value, separator, newKey));
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
result[newKey] = value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=object-flatten.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object-flatten.js","sourceRoot":"","sources":["../../src/object/object-flatten.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,aAAa,CAC5B,GAA4B,EAC5B,SAAS,GAAG,GAAG,EACf,OAAO,GAAG,EAAE;IAEZ,MAAM,MAAM,GAA4B,EAAE,CAAC;IAE3C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QACpC,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;QACvB,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,SAAS,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;QAE9D,IACC,KAAK,KAAK,IAAI;YACd,OAAO,KAAK,KAAK,QAAQ;YACzB,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YACrB,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,EACvB,CAAC;YACF,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,KAAgC,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;QAC3F,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;QACxB,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
|