@pawells/typescript-common 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -2
- package/build/array/array-chunk.d.ts +10 -4
- package/build/array/array-chunk.d.ts.map +1 -1
- package/build/array/array-chunk.js +10 -4
- package/build/array/array-chunk.js.map +1 -1
- package/build/array/array-contains.d.ts +11 -3
- package/build/array/array-contains.d.ts.map +1 -1
- package/build/array/array-contains.js +11 -3
- package/build/array/array-contains.js.map +1 -1
- package/build/array/array-group-by.d.ts +14 -5
- package/build/array/array-group-by.d.ts.map +1 -1
- package/build/array/array-group-by.js +14 -5
- package/build/array/array-group-by.js.map +1 -1
- package/build/array/array-shuffle.d.ts +12 -3
- package/build/array/array-shuffle.d.ts.map +1 -1
- package/build/array/array-shuffle.js +12 -3
- package/build/array/array-shuffle.js.map +1 -1
- package/build/array/assert.d.ts +115 -0
- package/build/array/assert.d.ts.map +1 -0
- package/build/array/assert.js +182 -0
- package/build/array/assert.js.map +1 -0
- package/build/array/index.d.ts +1 -0
- package/build/array/index.d.ts.map +1 -1
- package/build/array/index.js +1 -0
- package/build/array/index.js.map +1 -1
- package/build/array/unique.d.ts +13 -3
- package/build/array/unique.d.ts.map +1 -1
- package/build/array/unique.js +13 -3
- package/build/array/unique.js.map +1 -1
- package/build/asserts/errors.d.ts +129 -0
- package/build/asserts/errors.d.ts.map +1 -0
- package/build/asserts/errors.js +149 -0
- package/build/asserts/errors.js.map +1 -0
- package/build/asserts/generic.d.ts +436 -0
- package/build/asserts/generic.d.ts.map +1 -0
- package/build/asserts/generic.js +543 -0
- package/build/asserts/generic.js.map +1 -0
- package/build/asserts/index.d.ts +41 -0
- package/build/asserts/index.d.ts.map +1 -0
- package/build/asserts/index.js +41 -0
- package/build/asserts/index.js.map +1 -0
- package/build/asserts/internal-utils.d.ts +53 -0
- package/build/asserts/internal-utils.d.ts.map +1 -0
- package/build/asserts/internal-utils.js +108 -0
- package/build/asserts/internal-utils.js.map +1 -0
- package/build/asserts/types.d.ts +180 -0
- package/build/asserts/types.d.ts.map +1 -0
- package/build/asserts/types.js +2 -0
- package/build/asserts/types.js.map +1 -0
- package/build/asserts/utils.d.ts +92 -0
- package/build/asserts/utils.d.ts.map +1 -0
- package/build/asserts/utils.js +103 -0
- package/build/asserts/utils.js.map +1 -0
- package/build/boolean/assert.d.ts +66 -0
- package/build/boolean/assert.d.ts.map +1 -0
- package/build/boolean/assert.js +76 -0
- package/build/boolean/assert.js.map +1 -0
- package/build/boolean/index.d.ts +9 -0
- package/build/boolean/index.d.ts.map +1 -0
- package/build/boolean/index.js +9 -0
- package/build/boolean/index.js.map +1 -0
- package/build/enum/enum-entries.d.ts +8 -3
- package/build/enum/enum-entries.d.ts.map +1 -1
- package/build/enum/enum-entries.js +8 -3
- package/build/enum/enum-entries.js.map +1 -1
- package/build/enum/enum-key-by-value.d.ts +10 -5
- package/build/enum/enum-key-by-value.d.ts.map +1 -1
- package/build/enum/enum-key-by-value.js +10 -5
- package/build/enum/enum-key-by-value.js.map +1 -1
- package/build/enum/enum-keys.d.ts +12 -3
- package/build/enum/enum-keys.d.ts.map +1 -1
- package/build/enum/enum-keys.js +12 -3
- package/build/enum/enum-keys.js.map +1 -1
- package/build/enum/enum-safe-value.d.ts +11 -6
- package/build/enum/enum-safe-value.d.ts.map +1 -1
- package/build/enum/enum-safe-value.js +11 -6
- package/build/enum/enum-safe-value.js.map +1 -1
- package/build/enum/validate-enum-value.d.ts +10 -5
- package/build/enum/validate-enum-value.d.ts.map +1 -1
- package/build/enum/validate-enum-value.js +10 -5
- package/build/enum/validate-enum-value.js.map +1 -1
- package/build/index.d.ts +5 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +12 -0
- package/build/index.js.map +1 -1
- package/build/number/assert.d.ts +154 -0
- package/build/number/assert.d.ts.map +1 -0
- package/build/number/assert.js +153 -0
- package/build/number/assert.js.map +1 -0
- package/build/number/index.d.ts +9 -0
- package/build/number/index.d.ts.map +1 -0
- package/build/number/index.js +9 -0
- package/build/number/index.js.map +1 -0
- package/build/object/assert.d.ts +138 -0
- package/build/object/assert.d.ts.map +1 -0
- package/build/object/assert.js +204 -0
- package/build/object/assert.js.map +1 -0
- package/build/object/clone.d.ts +17 -6
- package/build/object/clone.d.ts.map +1 -1
- package/build/object/clone.js +17 -6
- package/build/object/clone.js.map +1 -1
- package/build/object/filter-cached.d.ts +15 -3
- package/build/object/filter-cached.d.ts.map +1 -1
- package/build/object/filter-cached.js +15 -3
- package/build/object/filter-cached.js.map +1 -1
- package/build/object/has-circular-reference.d.ts +12 -3
- package/build/object/has-circular-reference.d.ts.map +1 -1
- package/build/object/has-circular-reference.js +12 -3
- package/build/object/has-circular-reference.js.map +1 -1
- package/build/object/index.d.ts +1 -0
- package/build/object/index.d.ts.map +1 -1
- package/build/object/index.js +1 -0
- package/build/object/index.js.map +1 -1
- package/build/object/key-value-pairs.d.ts +11 -3
- package/build/object/key-value-pairs.d.ts.map +1 -1
- package/build/object/key-value-pairs.js +11 -3
- package/build/object/key-value-pairs.js.map +1 -1
- package/build/object/map-cached.d.ts +14 -3
- package/build/object/map-cached.d.ts.map +1 -1
- package/build/object/map-cached.js +14 -3
- package/build/object/map-cached.js.map +1 -1
- package/build/object/merge.d.ts +18 -7
- package/build/object/merge.d.ts.map +1 -1
- package/build/object/merge.js +18 -7
- package/build/object/merge.js.map +1 -1
- package/build/object/omit.d.ts +13 -4
- package/build/object/omit.d.ts.map +1 -1
- package/build/object/omit.js +13 -4
- package/build/object/omit.js.map +1 -1
- package/build/string/assert.d.ts +100 -0
- package/build/string/assert.d.ts.map +1 -0
- package/build/string/assert.js +185 -0
- package/build/string/assert.js.map +1 -0
- package/build/string/formatting.d.ts +10 -3
- package/build/string/formatting.d.ts.map +1 -1
- package/build/string/formatting.js +10 -3
- package/build/string/formatting.js.map +1 -1
- package/build/string/index.d.ts +1 -0
- package/build/string/index.d.ts.map +1 -1
- package/build/string/index.js +1 -0
- package/build/string/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/number/assert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAG5D;;;;;;;;;;;;;GAaG;AACH,qBAAa,WAAY,SAAQ,KAAK;gBACzB,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,gBAAiB,SAAQ,WAAW;gBACpC,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,CAAC;AAEpE;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,IAAI,CAAC,iBAAiB,EAAE,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC;AAEnG;;;GAGG;AACH,MAAM,MAAM,uBAAuB,GAAG,IAAI,CAAC,iBAAiB,EAAE,QAAQ,GAAG,SAAS,CAAC,CAAC;AAEpF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,WAAW,iBAAiB;IACjC,4EAA4E;IAC5E,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,0EAA0E;IAC1E,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB,yDAAyD;IACzD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,qEAAqE;IACrE,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,sDAAsD;IACtD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,kEAAkE;IAClE,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,iDAAiD;IACjD,EAAE,CAAC,EAAE,MAAM,CAAC;CACZ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,iBAAsB,EAAE,SAAS,GAAE,gBAAqB,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAoDpI"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { SetExceptionClass, SetExceptionMessage, ThrowException } from '../asserts/utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a value is not a valid number or fails basic numeric validation.
|
|
4
|
+
*
|
|
5
|
+
* This error is used for fundamental number validation failures, such as when
|
|
6
|
+
* a value is not of type 'number', is NaN, or fails other basic numeric checks.
|
|
7
|
+
* It serves as the base class for more specific number-related errors.
|
|
8
|
+
*
|
|
9
|
+
* @extends Error
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* throw new NumberError('Value is not a valid number');
|
|
13
|
+
* throw new NumberError('Expected number but received string');
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export class NumberError extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message ?? 'Value is not a valid number');
|
|
19
|
+
this.name = 'NumberError';
|
|
20
|
+
Object.setPrototypeOf(this, NumberError.prototype);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Error thrown when a numeric value is outside the expected range or bounds.
|
|
25
|
+
*
|
|
26
|
+
* This specialized error extends NumberError and is used specifically for
|
|
27
|
+
* range validation failures, such as values being too large, too small,
|
|
28
|
+
* or not matching specific equality constraints. It provides more context
|
|
29
|
+
* for range-related validation failures.
|
|
30
|
+
*
|
|
31
|
+
* @extends NumberError
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* throw new NumberRangeError('Value 150 exceeds maximum of 100');
|
|
35
|
+
* throw new NumberRangeError('Value -5 is below minimum of 0');
|
|
36
|
+
* throw new NumberRangeError('Value must be exactly 42');
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export class NumberRangeError extends NumberError {
|
|
40
|
+
constructor(message) {
|
|
41
|
+
super(message ?? 'Value is not in the expected range');
|
|
42
|
+
this.name = 'NumberRangeError';
|
|
43
|
+
Object.setPrototypeOf(this, NumberRangeError.prototype);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Asserts that a numeric value meets specified constraints including type, range, and mathematical properties.
|
|
48
|
+
*
|
|
49
|
+
* This function validates that a number satisfies one or more conditions defined in the NumberArgs parameter.
|
|
50
|
+
* It supports various validation types including finite/infinite checks, integer validation, and range
|
|
51
|
+
* constraints with comparison operations. Multiple constraints are evaluated with AND logic - all specified
|
|
52
|
+
* conditions must be satisfied for the assertion to pass.
|
|
53
|
+
*
|
|
54
|
+
* The function is particularly useful for validating user inputs, configuration values, or any numeric
|
|
55
|
+
* data that must meet specific criteria. It provides clear error messages indicating which constraint
|
|
56
|
+
* was violated.
|
|
57
|
+
*
|
|
58
|
+
* @param value - The numeric value to validate against the specified constraints
|
|
59
|
+
* @param args - Object containing one or more validation rules (finite, integer, range constraints)
|
|
60
|
+
* @param exception - Optional configuration for custom error handling and messages
|
|
61
|
+
* @throws {NumberError} When value is not a number or is NaN
|
|
62
|
+
* @throws {NumberRangeError} When value fails any of the specified constraints
|
|
63
|
+
* @returns void - Function only returns if all validations pass
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* // Type validation
|
|
68
|
+
* AssertNumber(42, { finite: true }); // ✓ 42 is finite
|
|
69
|
+
* AssertNumber(5, { integer: true }); // ✓ 5 is an integer
|
|
70
|
+
* AssertNumber(3.14, { integer: true }); // ✗ Throws: must be integer
|
|
71
|
+
* AssertNumber(Infinity, { finite: true }); // ✗ Throws: must be finite
|
|
72
|
+
*
|
|
73
|
+
* // Range validation
|
|
74
|
+
* AssertNumber(50, { gte: 0 }); // ✓ 50 >= 0
|
|
75
|
+
* AssertNumber(10, { lt: 100 }); // ✓ 10 < 100
|
|
76
|
+
* AssertNumber(42, { eq: 42 }); // ✓ 42 === 42
|
|
77
|
+
*
|
|
78
|
+
* // Combined constraints (AND logic)
|
|
79
|
+
* AssertNumber(50, { finite: true, gte: 0, lte: 100 }); // ✓ finite and 0 <= 50 <= 100
|
|
80
|
+
* AssertNumber(10, { integer: true, gt: 0, lt: 20 }); // ✓ integer and 0 < 10 < 20
|
|
81
|
+
* AssertNumber(3.5, { integer: true, gte: 0 }); // ✗ Throws: must be integer
|
|
82
|
+
*
|
|
83
|
+
* // Validation failures
|
|
84
|
+
* AssertNumber(-1, { gte: 0 }); // ✗ Throws: must be >= 0
|
|
85
|
+
* AssertNumber(150, { lte: 100 }); // ✗ Throws: must be <= 100
|
|
86
|
+
* AssertNumber(41, { eq: 42 }); // ✗ Throws: must equal 42
|
|
87
|
+
* AssertNumber(50, { gt: 60 }); // ✗ Throws: must be > 60
|
|
88
|
+
*
|
|
89
|
+
* // Practical usage examples
|
|
90
|
+
* function validateAge(age: number) {
|
|
91
|
+
* AssertNumber(age, { finite: true, integer: true, gte: 0, lte: 150 }); // Valid human age
|
|
92
|
+
* }
|
|
93
|
+
*
|
|
94
|
+
* function validatePercentage(pct: number) {
|
|
95
|
+
* AssertNumber(pct, { finite: true, gte: 0, lte: 100 }); // 0-100%
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* function validateTemperature(temp: number) {
|
|
99
|
+
* AssertNumber(temp, { finite: true, gt: -273.15 }); // Above absolute zero
|
|
100
|
+
* }
|
|
101
|
+
*
|
|
102
|
+
* function validateArrayIndex(index: number) {
|
|
103
|
+
* AssertNumber(index, { integer: true, gte: 0 }); // Non-negative integer
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export function AssertNumber(value, args = {}, exception = {}) {
|
|
108
|
+
if (typeof value !== 'number' || Number.isNaN(value)) {
|
|
109
|
+
SetExceptionClass(exception, NumberError);
|
|
110
|
+
const actualType = Number.isNaN(value) ? 'NaN' : typeof value;
|
|
111
|
+
SetExceptionMessage(exception, `Expected number but received ${actualType}: ${JSON.stringify(value)}`);
|
|
112
|
+
ThrowException(exception);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
SetExceptionClass(exception, NumberRangeError);
|
|
116
|
+
// Validate finite constraint (if specified, value must be finite)
|
|
117
|
+
if (args.finite === true && !Number.isFinite(value)) {
|
|
118
|
+
SetExceptionMessage(exception, `Expected finite number but received ${value}`);
|
|
119
|
+
ThrowException(exception);
|
|
120
|
+
}
|
|
121
|
+
// Validate integer constraint (if specified, value must be an integer)
|
|
122
|
+
if (args.integer === true && !Number.isInteger(value)) {
|
|
123
|
+
SetExceptionMessage(exception, `Expected integer but received ${value}`);
|
|
124
|
+
ThrowException(exception);
|
|
125
|
+
}
|
|
126
|
+
// Validate equality constraint (if specified, value must exactly match)
|
|
127
|
+
if (args.eq !== undefined && value !== args.eq) {
|
|
128
|
+
SetExceptionMessage(exception, `Expected value to equal ${args.eq} but received ${value}`);
|
|
129
|
+
ThrowException(exception);
|
|
130
|
+
}
|
|
131
|
+
// Validate greater than constraint (exclusive - value must be strictly greater)
|
|
132
|
+
if (args.gt !== undefined && value <= args.gt) {
|
|
133
|
+
SetExceptionMessage(exception, `Expected value > ${args.gt} but received ${value}`);
|
|
134
|
+
ThrowException(exception);
|
|
135
|
+
}
|
|
136
|
+
// Validate greater than or equal constraint (inclusive - value can equal the bound)
|
|
137
|
+
if (args.gte !== undefined && value < args.gte) {
|
|
138
|
+
SetExceptionMessage(exception, `Expected value >= ${args.gte} but received ${value}`);
|
|
139
|
+
ThrowException(exception);
|
|
140
|
+
}
|
|
141
|
+
// Validate less than constraint (exclusive - value must be strictly less)
|
|
142
|
+
if (args.lt !== undefined && value >= args.lt) {
|
|
143
|
+
SetExceptionMessage(exception, `Expected value < ${args.lt} but received ${value}`);
|
|
144
|
+
ThrowException(exception);
|
|
145
|
+
}
|
|
146
|
+
// Validate less than or equal constraint (inclusive - value can equal the bound)
|
|
147
|
+
if (args.lte !== undefined && value > args.lte) {
|
|
148
|
+
SetExceptionMessage(exception, `Expected value <= ${args.lte} but received ${value}`);
|
|
149
|
+
ThrowException(exception);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../../src/number/assert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE7F;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IACrC,YAAY,OAAgB;QAC3B,KAAK,CAAC,OAAO,IAAI,6BAA6B,CAAC,CAAC;QAChD,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACD;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,gBAAiB,SAAQ,WAAW;IAChD,YAAY,OAAgB;QAC3B,KAAK,CAAC,OAAO,IAAI,oCAAoC,CAAC,CAAC;QACvD,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC;IACzD,CAAC;CACD;AA6DD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc,EAAE,OAA0B,EAAE,EAAE,YAA8B,EAAE;IAC1G,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QACtD,iBAAiB,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;QAE1C,MA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,CAAC;YACpF,cAAc,CAAC,SAAS,CAAC,CAAC;QAC3B,CAAC;QAED,oFAAoF;QACpF,IAAI,IAAI,CAAC,GAAG,KAAK,SAAS,IAAI,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YAChD,mBAAmB,CAAC,SAAS,EAAE,qBAAqB,IAAI,CAAC,GAAG,iBAAiB,KAAK,EAAE,CAAC,CAAC;YACtF,cAAc,CAAC,SAAS,CAAC,CAAC;QAC3B,CAAC;QAED,0EAA0E;QAC1E,IAAI,IAAI,CAAC,EAAE,KAAK,SAAS,IAAI,KAAK,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC;YAC/C,mBAAmB,CAAC,SAAS,EAAE,oBAAoB,IAAI,CAAC,EAAE,iBAAiB,KAAK,EAAE,CAAC,CAAC;YACpF,cAAc,CAAC,SAAS,CAAC,CAAC;QAC3B,CAAC;QAED,iFAAiF;QACjF,IAAI,IAAI,CAAC,GAAG,KAAK,SAAS,IAAI,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YAChD,mBAAmB,CAAC,SAAS,EAAE,qBAAqB,IAAI,CAAC,GAAG,iBAAiB,KAAK,EAAE,CAAC,CAAC;YACtF,cAAc,CAAC,SAAS,CAAC,CAAC;QAC3B,CAAC;IACF,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/number/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/number/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { IAssertException } from '../asserts/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a value is not a valid object or fails an object assertion.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* throw new ObjectError('Value is not a valid object');
|
|
7
|
+
*/
|
|
8
|
+
export declare class ObjectError extends Error {
|
|
9
|
+
constructor(message?: string);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when an object is missing a required property or a property fails an assertion.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* throw new PropertyError('Object is missing required property');
|
|
16
|
+
*/
|
|
17
|
+
export declare class ObjectPropertyError extends Error {
|
|
18
|
+
constructor(message?: string);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Asserts that a value is a plain object (not null, not an array, not a function).
|
|
22
|
+
*
|
|
23
|
+
* This method validates that the provided value is an object type, excluding null,
|
|
24
|
+
* arrays, and functions which are technically objects in JavaScript but not plain
|
|
25
|
+
* objects. After this assertion, the value is typed as Record<string, unknown>
|
|
26
|
+
* for safe property access.
|
|
27
|
+
*
|
|
28
|
+
* @template TError - Custom error type to throw on failure
|
|
29
|
+
* @param value - The value to validate as an object
|
|
30
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
31
|
+
* @throws {ObjectError} When value is not a plain object
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* AssertObject({ name: "John" }); // ✓ Valid (plain object)
|
|
36
|
+
* AssertObject({}); // ✓ Valid (empty object)
|
|
37
|
+
* AssertObject(new Date()); // ✓ Valid (object instance)
|
|
38
|
+
* AssertObject([1, 2, 3]); // ✗ Throws ObjectError (array)
|
|
39
|
+
* AssertObject(null); // ✗ Throws ObjectError (null)
|
|
40
|
+
* AssertObject("string"); // ✗ Throws ObjectError (primitive)
|
|
41
|
+
* AssertObject(() => {}); // ✗ Throws ObjectError (function)
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function AssertObject(value: unknown, exception?: IAssertException): asserts value is Record<string, unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* Asserts that an object has a specific property (inherited or own).
|
|
47
|
+
*
|
|
48
|
+
* This method validates that the specified property exists in the object,
|
|
49
|
+
* including properties from the prototype chain. Uses the 'in' operator
|
|
50
|
+
* for property detection. This is useful for checking if an object conforms
|
|
51
|
+
* to an expected interface or has required properties.
|
|
52
|
+
*
|
|
53
|
+
* @template T - The object type
|
|
54
|
+
* @template K - The property key type
|
|
55
|
+
* @template TError - Custom error type to throw on failure
|
|
56
|
+
* @param value - The object to check for the property
|
|
57
|
+
* @param property - The property key to check for
|
|
58
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
59
|
+
* @throws {PropertyError} When object does not have the specified property
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const obj = { name: "John", age: 30 };
|
|
64
|
+
* AssertHasProperty(obj, "name"); // ✓ Valid (own property)
|
|
65
|
+
* AssertHasProperty(obj, "toString"); // ✓ Valid (inherited property)
|
|
66
|
+
* AssertHasProperty(obj, "invalid"); // ✗ Throws PropertyError
|
|
67
|
+
*
|
|
68
|
+
* // Type narrowing with property presence
|
|
69
|
+
* function processUser(obj: unknown) {
|
|
70
|
+
* AssertObject(obj);
|
|
71
|
+
* AssertHasProperty(obj, "name");
|
|
72
|
+
* // obj["name"] is now accessible safely
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function AssertObjectHasProperty<T extends object, K extends PropertyKey>(value: T, property: K, exception?: IAssertException): void;
|
|
77
|
+
/**
|
|
78
|
+
* Asserts that an object has a specific own property (not inherited).
|
|
79
|
+
*
|
|
80
|
+
* This method validates that the specified property exists as an own property
|
|
81
|
+
* of the object, excluding properties from the prototype chain. Uses
|
|
82
|
+
* Object.prototype.hasOwnProperty.call() for reliable detection. This is useful
|
|
83
|
+
* when you need to ensure a property is directly defined on the object.
|
|
84
|
+
*
|
|
85
|
+
* @template T - The object type
|
|
86
|
+
* @template K - The property key type
|
|
87
|
+
* @template TError - Custom error type to throw on failure
|
|
88
|
+
* @param value - The object to check for the own property
|
|
89
|
+
* @param property - The property key to check for
|
|
90
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
91
|
+
* @throws {PropertyError} When object does not have the specified own property
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const obj = { name: "John" };
|
|
96
|
+
* AssertHasOwnProperty(obj, "name"); // ✓ Valid (own property)
|
|
97
|
+
* AssertHasOwnProperty(obj, "toString"); // ✗ Throws (inherited property)
|
|
98
|
+
* AssertHasOwnProperty(obj, "invalid"); // ✗ Throws (doesn't exist)
|
|
99
|
+
*
|
|
100
|
+
* // Checking for data vs inherited methods
|
|
101
|
+
* function validateUserData(obj: object) {
|
|
102
|
+
* AssertHasOwnProperty(obj, "id"); // Must be own property
|
|
103
|
+
* AssertHasOwnProperty(obj, "name"); // Must be own property
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function AssertObjectHasOwnProperty<T extends object, K extends PropertyKey>(value: T, property: K, exception?: IAssertException): void;
|
|
108
|
+
/**
|
|
109
|
+
* Asserts that a specific property of an object is not null or undefined.
|
|
110
|
+
*
|
|
111
|
+
* This method validates that the specified property exists and has a non-nullish value.
|
|
112
|
+
* It combines property existence checking with null/undefined validation. Useful for
|
|
113
|
+
* validating that required object properties have been properly initialized.
|
|
114
|
+
*
|
|
115
|
+
* @template T - The object type
|
|
116
|
+
* @template K - The property key type (must be a key of T)
|
|
117
|
+
* @template TError - Custom error type to throw on failure
|
|
118
|
+
* @param value - The object to check
|
|
119
|
+
* @param property - The property key to validate for non-null value
|
|
120
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
121
|
+
* @throws {PropertyError} When property is null, undefined, or doesn't exist
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const user = { id: 123, name: "John", email: null };
|
|
126
|
+
* AssertPropertyNotNull(user, "id"); // ✓ Valid (123 is not null/undefined)
|
|
127
|
+
* AssertPropertyNotNull(user, "name"); // ✓ Valid ("John" is not null/undefined)
|
|
128
|
+
* AssertPropertyNotNull(user, "email"); // ✗ Throws PropertyError (null)
|
|
129
|
+
*
|
|
130
|
+
* // Type narrowing for property values
|
|
131
|
+
* function processUser(user: { name?: string | null }) {
|
|
132
|
+
* AssertPropertyNotNull(user, "name");
|
|
133
|
+
* // user.name is now typed as string (null/undefined excluded)
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export declare function AssertObjectPropertyNotNull<T extends object, K extends keyof T>(value: T, property: K, exception?: IAssertException): void;
|
|
138
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/object/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAMvD;;;;;GAKG;AACH,qBAAa,WAAY,SAAQ,KAAK;gBACzB,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;;;GAKG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;gBACjC,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,GAAE,gBAAqB,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAyBvH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,WAAW,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,GAAE,gBAAqB,GAAG,IAAI,CAU9I;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,WAAW,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,GAAE,gBAAqB,GAAG,IAAI,CAUjJ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,2BAA2B,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,GAAE,gBAAqB,GAAG,IAAI,CAW9I"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { SetExceptionClass, SetExceptionMessage, ThrowException } from '../asserts/utils.js';
|
|
2
|
+
/** Maximum number of characters to include from a value in an error message. */
|
|
3
|
+
const MAX_VALUE_DISPLAY_LENGTH = 100;
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when a value is not a valid object or fails an object assertion.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* throw new ObjectError('Value is not a valid object');
|
|
9
|
+
*/
|
|
10
|
+
export class ObjectError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message ?? 'Object assertion failed');
|
|
13
|
+
this.name = 'ObjectError';
|
|
14
|
+
Object.setPrototypeOf(this, ObjectError.prototype);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Error thrown when an object is missing a required property or a property fails an assertion.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* throw new PropertyError('Object is missing required property');
|
|
22
|
+
*/
|
|
23
|
+
export class ObjectPropertyError extends Error {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message ?? 'Object Property Assertion Failed');
|
|
26
|
+
this.name = 'ObjectPropertyError';
|
|
27
|
+
Object.setPrototypeOf(this, ObjectPropertyError.prototype);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Asserts that a value is a plain object (not null, not an array, not a function).
|
|
32
|
+
*
|
|
33
|
+
* This method validates that the provided value is an object type, excluding null,
|
|
34
|
+
* arrays, and functions which are technically objects in JavaScript but not plain
|
|
35
|
+
* objects. After this assertion, the value is typed as Record<string, unknown>
|
|
36
|
+
* for safe property access.
|
|
37
|
+
*
|
|
38
|
+
* @template TError - Custom error type to throw on failure
|
|
39
|
+
* @param value - The value to validate as an object
|
|
40
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
41
|
+
* @throws {ObjectError} When value is not a plain object
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* AssertObject({ name: "John" }); // ✓ Valid (plain object)
|
|
46
|
+
* AssertObject({}); // ✓ Valid (empty object)
|
|
47
|
+
* AssertObject(new Date()); // ✓ Valid (object instance)
|
|
48
|
+
* AssertObject([1, 2, 3]); // ✗ Throws ObjectError (array)
|
|
49
|
+
* AssertObject(null); // ✗ Throws ObjectError (null)
|
|
50
|
+
* AssertObject("string"); // ✗ Throws ObjectError (primitive)
|
|
51
|
+
* AssertObject(() => {}); // ✗ Throws ObjectError (function)
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function AssertObject(value, exception = {}) {
|
|
55
|
+
SetExceptionClass(exception, ObjectError);
|
|
56
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value) || typeof value === 'function') {
|
|
57
|
+
const actualType = value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value;
|
|
58
|
+
let valueStr;
|
|
59
|
+
try {
|
|
60
|
+
if (typeof value === 'function') {
|
|
61
|
+
valueStr = '[Function]';
|
|
62
|
+
}
|
|
63
|
+
else if (value === null) {
|
|
64
|
+
valueStr = 'null';
|
|
65
|
+
}
|
|
66
|
+
else if (value === undefined) {
|
|
67
|
+
valueStr = 'undefined';
|
|
68
|
+
}
|
|
69
|
+
else if (typeof value === 'symbol') {
|
|
70
|
+
valueStr = `[Symbol: ${String(value)}]`;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
valueStr = JSON.stringify(value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
valueStr = String(value);
|
|
78
|
+
}
|
|
79
|
+
SetExceptionMessage(exception, `Expected object but received ${actualType}: ${valueStr.slice(0, MAX_VALUE_DISPLAY_LENGTH)}`);
|
|
80
|
+
ThrowException(exception);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Asserts that an object has a specific property (inherited or own).
|
|
85
|
+
*
|
|
86
|
+
* This method validates that the specified property exists in the object,
|
|
87
|
+
* including properties from the prototype chain. Uses the 'in' operator
|
|
88
|
+
* for property detection. This is useful for checking if an object conforms
|
|
89
|
+
* to an expected interface or has required properties.
|
|
90
|
+
*
|
|
91
|
+
* @template T - The object type
|
|
92
|
+
* @template K - The property key type
|
|
93
|
+
* @template TError - Custom error type to throw on failure
|
|
94
|
+
* @param value - The object to check for the property
|
|
95
|
+
* @param property - The property key to check for
|
|
96
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
97
|
+
* @throws {PropertyError} When object does not have the specified property
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const obj = { name: "John", age: 30 };
|
|
102
|
+
* AssertHasProperty(obj, "name"); // ✓ Valid (own property)
|
|
103
|
+
* AssertHasProperty(obj, "toString"); // ✓ Valid (inherited property)
|
|
104
|
+
* AssertHasProperty(obj, "invalid"); // ✗ Throws PropertyError
|
|
105
|
+
*
|
|
106
|
+
* // Type narrowing with property presence
|
|
107
|
+
* function processUser(obj: unknown) {
|
|
108
|
+
* AssertObject(obj);
|
|
109
|
+
* AssertHasProperty(obj, "name");
|
|
110
|
+
* // obj["name"] is now accessible safely
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export function AssertObjectHasProperty(value, property, exception = {}) {
|
|
115
|
+
// First check if value is an object, using ObjectError
|
|
116
|
+
AssertObject(value, { class: ObjectError });
|
|
117
|
+
// Then check for property existence, using the configured exception class or default
|
|
118
|
+
SetExceptionClass(exception, ObjectPropertyError);
|
|
119
|
+
if (!(property in value)) {
|
|
120
|
+
SetExceptionMessage(exception, `Expected object to have property '${String(property)}' but property was not found`);
|
|
121
|
+
ThrowException(exception);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Asserts that an object has a specific own property (not inherited).
|
|
126
|
+
*
|
|
127
|
+
* This method validates that the specified property exists as an own property
|
|
128
|
+
* of the object, excluding properties from the prototype chain. Uses
|
|
129
|
+
* Object.prototype.hasOwnProperty.call() for reliable detection. This is useful
|
|
130
|
+
* when you need to ensure a property is directly defined on the object.
|
|
131
|
+
*
|
|
132
|
+
* @template T - The object type
|
|
133
|
+
* @template K - The property key type
|
|
134
|
+
* @template TError - Custom error type to throw on failure
|
|
135
|
+
* @param value - The object to check for the own property
|
|
136
|
+
* @param property - The property key to check for
|
|
137
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
138
|
+
* @throws {PropertyError} When object does not have the specified own property
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const obj = { name: "John" };
|
|
143
|
+
* AssertHasOwnProperty(obj, "name"); // ✓ Valid (own property)
|
|
144
|
+
* AssertHasOwnProperty(obj, "toString"); // ✗ Throws (inherited property)
|
|
145
|
+
* AssertHasOwnProperty(obj, "invalid"); // ✗ Throws (doesn't exist)
|
|
146
|
+
*
|
|
147
|
+
* // Checking for data vs inherited methods
|
|
148
|
+
* function validateUserData(obj: object) {
|
|
149
|
+
* AssertHasOwnProperty(obj, "id"); // Must be own property
|
|
150
|
+
* AssertHasOwnProperty(obj, "name"); // Must be own property
|
|
151
|
+
* }
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export function AssertObjectHasOwnProperty(value, property, exception = {}) {
|
|
155
|
+
// First check if value is an object, using ObjectError
|
|
156
|
+
AssertObject(value, { class: ObjectError });
|
|
157
|
+
// Then check for own property existence, using the configured exception class or default
|
|
158
|
+
SetExceptionClass(exception, ObjectPropertyError);
|
|
159
|
+
if (!Object.prototype.hasOwnProperty.call(value, property)) {
|
|
160
|
+
SetExceptionMessage(exception, `Expected object to have own property '${String(property)}' but property was not found`);
|
|
161
|
+
ThrowException(exception);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Asserts that a specific property of an object is not null or undefined.
|
|
166
|
+
*
|
|
167
|
+
* This method validates that the specified property exists and has a non-nullish value.
|
|
168
|
+
* It combines property existence checking with null/undefined validation. Useful for
|
|
169
|
+
* validating that required object properties have been properly initialized.
|
|
170
|
+
*
|
|
171
|
+
* @template T - The object type
|
|
172
|
+
* @template K - The property key type (must be a key of T)
|
|
173
|
+
* @template TError - Custom error type to throw on failure
|
|
174
|
+
* @param value - The object to check
|
|
175
|
+
* @param property - The property key to validate for non-null value
|
|
176
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
177
|
+
* @throws {PropertyError} When property is null, undefined, or doesn't exist
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const user = { id: 123, name: "John", email: null };
|
|
182
|
+
* AssertPropertyNotNull(user, "id"); // ✓ Valid (123 is not null/undefined)
|
|
183
|
+
* AssertPropertyNotNull(user, "name"); // ✓ Valid ("John" is not null/undefined)
|
|
184
|
+
* AssertPropertyNotNull(user, "email"); // ✗ Throws PropertyError (null)
|
|
185
|
+
*
|
|
186
|
+
* // Type narrowing for property values
|
|
187
|
+
* function processUser(user: { name?: string | null }) {
|
|
188
|
+
* AssertPropertyNotNull(user, "name");
|
|
189
|
+
* // user.name is now typed as string (null/undefined excluded)
|
|
190
|
+
* }
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
export function AssertObjectPropertyNotNull(value, property, exception = {}) {
|
|
194
|
+
// First check if value is an object, using ObjectError
|
|
195
|
+
AssertObject(value, { class: ObjectError });
|
|
196
|
+
// Then check for property value, using the configured exception class or default
|
|
197
|
+
SetExceptionClass(exception, ObjectPropertyError);
|
|
198
|
+
if (value[property] === null || value[property] === undefined) {
|
|
199
|
+
const actualValue = value[property] === null ? 'null' : 'undefined';
|
|
200
|
+
SetExceptionMessage(exception, `Expected property '${String(property)}' to be non-null/non-undefined but received ${actualValue}`);
|
|
201
|
+
ThrowException(exception);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../../src/object/assert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE7F,gFAAgF;AAChF,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAErC;;;;;GAKG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IACrC,YAAY,OAAgB;QAC3B,KAAK,CAAC,OAAO,IAAI,yBAAyB,CAAC,CAAC;QAC5C,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACD;AAED;;;;;GAKG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC7C,YAAY,OAAgB;QAC3B,KAAK,CAAC,OAAO,IAAI,kCAAkC,CAAC,CAAC;QACrD,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAC5D,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc,EAAE,YAA8B,EAAE;IAC5E,iBAAiB,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;IAC1C,IAAI,OAA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AC,KAAK,SAAS,EAAE,CAAC;QAC/D,MAAM,WAAW,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC;QACpE,mBAAmB,CAAC,SAAS,EAAE,sBAAsB,MAAM,CAAC,QAAQ,CAAC,+CAA+C,WAAW,EAAE,CAAC,CAAC;QACnI,cAAc,CAAC,SAAS,CAAC,CAAC;IAC3B,CAAC;AACF,CAAC"}
|
package/build/object/clone.d.ts
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
import type { TObjectTransformer } from './types.js';
|
|
2
2
|
/**
|
|
3
|
-
* Creates a deep clone of an object with security protections
|
|
3
|
+
* Creates a deep clone of an object with security protections.
|
|
4
4
|
*
|
|
5
5
|
* **Security Features:**
|
|
6
|
-
* - Prevents infinite recursion with circular reference detection
|
|
7
|
-
* - Filters out dangerous properties that could cause prototype pollution
|
|
8
|
-
* - Safely handles complex object structures
|
|
6
|
+
* - Prevents infinite recursion with circular reference detection.
|
|
7
|
+
* - Filters out dangerous properties that could cause prototype pollution.
|
|
8
|
+
* - Safely handles complex object structures.
|
|
9
9
|
*
|
|
10
|
-
* @
|
|
10
|
+
* @template T - The type of the object to clone
|
|
11
|
+
* @param obj - Object to clone
|
|
12
|
+
* @param visitedInput - Internal WeakSet used to detect circular references (do not pass manually)
|
|
11
13
|
* @returns Deep clone of the object
|
|
12
|
-
* @throws {Error}
|
|
14
|
+
* @throws {Error} When `obj` contains circular references.
|
|
15
|
+
* @throws {Error} When `obj` is a function, symbol, `Map`, `Set`, `WeakMap`, or a class instance.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const original = { a: 1, b: { c: 2 } };
|
|
20
|
+
* const clone = ObjectClone(original);
|
|
21
|
+
* clone.b.c = 99;
|
|
22
|
+
* console.log(original.b.c); // 2 (original is not affected)
|
|
23
|
+
* ```
|
|
13
24
|
*/
|
|
14
25
|
export declare function ObjectClone<T>(obj: T, visitedInput?: WeakSet<object>): T;
|
|
15
26
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clone.d.ts","sourceRoot":"","sources":["../../src/object/clone.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD
|
|
1
|
+
{"version":3,"file":"clone.d.ts","sourceRoot":"","sources":["../../src/object/clone.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAiDxE;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,eAAe,CAAC,MAAM,SAAS,MAAM,EAAE,OAAO,SAAS,MAAM,EAC5E,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,kBAAkB,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9C,OAAO,CAMT"}
|
package/build/object/clone.js
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
import { filterDangerousKeys } from './security-utils.js';
|
|
2
2
|
/**
|
|
3
|
-
* Creates a deep clone of an object with security protections
|
|
3
|
+
* Creates a deep clone of an object with security protections.
|
|
4
4
|
*
|
|
5
5
|
* **Security Features:**
|
|
6
|
-
* - Prevents infinite recursion with circular reference detection
|
|
7
|
-
* - Filters out dangerous properties that could cause prototype pollution
|
|
8
|
-
* - Safely handles complex object structures
|
|
6
|
+
* - Prevents infinite recursion with circular reference detection.
|
|
7
|
+
* - Filters out dangerous properties that could cause prototype pollution.
|
|
8
|
+
* - Safely handles complex object structures.
|
|
9
9
|
*
|
|
10
|
-
* @
|
|
10
|
+
* @template T - The type of the object to clone
|
|
11
|
+
* @param obj - Object to clone
|
|
12
|
+
* @param visitedInput - Internal WeakSet used to detect circular references (do not pass manually)
|
|
11
13
|
* @returns Deep clone of the object
|
|
12
|
-
* @throws {Error}
|
|
14
|
+
* @throws {Error} When `obj` contains circular references.
|
|
15
|
+
* @throws {Error} When `obj` is a function, symbol, `Map`, `Set`, `WeakMap`, or a class instance.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const original = { a: 1, b: { c: 2 } };
|
|
20
|
+
* const clone = ObjectClone(original);
|
|
21
|
+
* clone.b.c = 99;
|
|
22
|
+
* console.log(original.b.c); // 2 (original is not affected)
|
|
23
|
+
* ```
|
|
13
24
|
*/
|
|
14
25
|
export function ObjectClone(obj, visitedInput) {
|
|
15
26
|
if (obj === null || obj === undefined) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clone.js","sourceRoot":"","sources":["../../src/object/clone.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAG1D
|
|
1
|
+
{"version":3,"file":"clone.js","sourceRoot":"","sources":["../../src/object/clone.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,WAAW,CAAI,GAAM,EAAE,YAA8B;IACpE,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACvC,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,uDAAuD;IACvD,MAAM,OAAO,GAAG,YAAY,IAAI,IAAI,OAAO,EAAE,CAAC;IAE9C,gDAAgD;IAChD,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QAC7C,+BAA+B;QAC/B,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,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