@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,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Internal utilities for the asserts package.
|
|
3
|
+
* These utilities are copied from @jtv/common to make @jtv/asserts standalone.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* A constructor function type for creating objects of type O.
|
|
7
|
+
* @template O The object type to construct.
|
|
8
|
+
*/
|
|
9
|
+
export type TConstructableObject<O extends object = object> = new (...args: any[]) => O;
|
|
10
|
+
/**
|
|
11
|
+
* Performs a deep comparison between two values to determine if they are equivalent.
|
|
12
|
+
* Handles primitives, objects, arrays, dates, regular expressions, and special values like NaN.
|
|
13
|
+
*
|
|
14
|
+
* @param a - First value to compare
|
|
15
|
+
* @param b - Second value to compare
|
|
16
|
+
* @returns True if the values are equivalent, false otherwise
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // Primitive values
|
|
21
|
+
* ObjectEquals(42, 42); // true
|
|
22
|
+
* ObjectEquals('hello', 'hello'); // true
|
|
23
|
+
* ObjectEquals(true, false); // false
|
|
24
|
+
*
|
|
25
|
+
* // Objects
|
|
26
|
+
* ObjectEquals({ a: 1, b: 2 }, { a: 1, b: 2 }); // true
|
|
27
|
+
* ObjectEquals({ a: 1, b: 2 }, { a: 1, b: 3 }); // false
|
|
28
|
+
*
|
|
29
|
+
* // Nested objects
|
|
30
|
+
* const obj1 = { user: { name: 'John', age: 30 }, active: true };
|
|
31
|
+
* const obj2 = { user: { name: 'John', age: 30 }, active: true };
|
|
32
|
+
* ObjectEquals(obj1, obj2); // true
|
|
33
|
+
*
|
|
34
|
+
* // Arrays
|
|
35
|
+
* ObjectEquals([1, 2, 3], [1, 2, 3]); // true
|
|
36
|
+
* ObjectEquals([1, [2, 3]], [1, [2, 3]]); // true
|
|
37
|
+
*
|
|
38
|
+
* // Date objects
|
|
39
|
+
* const date1 = new Date('2023-01-01');
|
|
40
|
+
* const date2 = new Date('2023-01-01');
|
|
41
|
+
* ObjectEquals(date1, date2); // true
|
|
42
|
+
*
|
|
43
|
+
* // Regular expressions
|
|
44
|
+
* ObjectEquals(/abc/g, /abc/g); // true
|
|
45
|
+
* ObjectEquals(/abc/g, /abc/i); // false
|
|
46
|
+
*
|
|
47
|
+
* // Special cases
|
|
48
|
+
* ObjectEquals(NaN, NaN); // true (unlike === comparison)
|
|
49
|
+
* ObjectEquals(null, undefined); // false
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function ObjectEquals(a: any, b: any): boolean;
|
|
53
|
+
//# sourceMappingURL=internal-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-utils.d.ts","sourceRoot":"","sources":["../../src/asserts/internal-utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAExF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,GAAG,OAAO,CAgEpD"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Internal utilities for the asserts package.
|
|
3
|
+
* These utilities are copied from @jtv/common to make @jtv/asserts standalone.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Performs a deep comparison between two values to determine if they are equivalent.
|
|
7
|
+
* Handles primitives, objects, arrays, dates, regular expressions, and special values like NaN.
|
|
8
|
+
*
|
|
9
|
+
* @param a - First value to compare
|
|
10
|
+
* @param b - Second value to compare
|
|
11
|
+
* @returns True if the values are equivalent, false otherwise
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Primitive values
|
|
16
|
+
* ObjectEquals(42, 42); // true
|
|
17
|
+
* ObjectEquals('hello', 'hello'); // true
|
|
18
|
+
* ObjectEquals(true, false); // false
|
|
19
|
+
*
|
|
20
|
+
* // Objects
|
|
21
|
+
* ObjectEquals({ a: 1, b: 2 }, { a: 1, b: 2 }); // true
|
|
22
|
+
* ObjectEquals({ a: 1, b: 2 }, { a: 1, b: 3 }); // false
|
|
23
|
+
*
|
|
24
|
+
* // Nested objects
|
|
25
|
+
* const obj1 = { user: { name: 'John', age: 30 }, active: true };
|
|
26
|
+
* const obj2 = { user: { name: 'John', age: 30 }, active: true };
|
|
27
|
+
* ObjectEquals(obj1, obj2); // true
|
|
28
|
+
*
|
|
29
|
+
* // Arrays
|
|
30
|
+
* ObjectEquals([1, 2, 3], [1, 2, 3]); // true
|
|
31
|
+
* ObjectEquals([1, [2, 3]], [1, [2, 3]]); // true
|
|
32
|
+
*
|
|
33
|
+
* // Date objects
|
|
34
|
+
* const date1 = new Date('2023-01-01');
|
|
35
|
+
* const date2 = new Date('2023-01-01');
|
|
36
|
+
* ObjectEquals(date1, date2); // true
|
|
37
|
+
*
|
|
38
|
+
* // Regular expressions
|
|
39
|
+
* ObjectEquals(/abc/g, /abc/g); // true
|
|
40
|
+
* ObjectEquals(/abc/g, /abc/i); // false
|
|
41
|
+
*
|
|
42
|
+
* // Special cases
|
|
43
|
+
* ObjectEquals(NaN, NaN); // true (unlike === comparison)
|
|
44
|
+
* ObjectEquals(null, undefined); // false
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function ObjectEquals(a, b) {
|
|
48
|
+
// If the values are strictly equal, return true
|
|
49
|
+
if (a === b)
|
|
50
|
+
return true;
|
|
51
|
+
// Handle NaN special case - NaN should equal NaN for assertion purposes
|
|
52
|
+
if (typeof a === 'number' && typeof b === 'number' && isNaN(a) && isNaN(b)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
// If either value is null, undefined, or not an object, they can't be equivalent
|
|
56
|
+
if (a === null || b === null || a === undefined || b === undefined)
|
|
57
|
+
return false;
|
|
58
|
+
if (typeof a !== typeof b)
|
|
59
|
+
return false;
|
|
60
|
+
if (typeof a !== 'object')
|
|
61
|
+
return false;
|
|
62
|
+
// Handle Date objects
|
|
63
|
+
if (a instanceof Date && b instanceof Date) {
|
|
64
|
+
return a.getTime() === b.getTime();
|
|
65
|
+
}
|
|
66
|
+
// Handle RegExp objects
|
|
67
|
+
if (a instanceof RegExp && b instanceof RegExp) {
|
|
68
|
+
return a.toString() === b.toString();
|
|
69
|
+
}
|
|
70
|
+
// Handle arrays
|
|
71
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
72
|
+
if (a.length !== b.length)
|
|
73
|
+
return false;
|
|
74
|
+
// Compare each element in the arrays
|
|
75
|
+
for (let i = 0; i < a.length; i++) {
|
|
76
|
+
if (!ObjectEquals(a[i], b[i]))
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
// If one is array and the other isn't, they're not equal
|
|
82
|
+
if (Array.isArray(a) !== Array.isArray(b))
|
|
83
|
+
return false;
|
|
84
|
+
// Compare object properties
|
|
85
|
+
const keysA = Object.keys(a);
|
|
86
|
+
const keysB = Object.keys(b);
|
|
87
|
+
if (keysA.length !== keysB.length)
|
|
88
|
+
return false;
|
|
89
|
+
// Also compare symbol properties
|
|
90
|
+
const symbolsA = Object.getOwnPropertySymbols(a);
|
|
91
|
+
const symbolsB = Object.getOwnPropertySymbols(b);
|
|
92
|
+
if (symbolsA.length !== symbolsB.length)
|
|
93
|
+
return false;
|
|
94
|
+
for (const key of keysA) {
|
|
95
|
+
if (!Object.hasOwn(b, key))
|
|
96
|
+
return false;
|
|
97
|
+
if (!ObjectEquals(a[key], b[key]))
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
for (const symbol of symbolsA) {
|
|
101
|
+
if (!Object.getOwnPropertySymbols(b).includes(symbol))
|
|
102
|
+
return false;
|
|
103
|
+
if (!ObjectEquals(a[symbol], b[symbol]))
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=internal-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-utils.js","sourceRoot":"","sources":["../../src/asserts/internal-utils.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { TConstructableObject } from './internal-utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Type alias for constraint union patterns.
|
|
4
|
+
* Represents common constraint combinations used across assertion modules.
|
|
5
|
+
*/
|
|
6
|
+
export type TConstraintValue = string | number | boolean | null | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Type alias for comparison operators.
|
|
9
|
+
* Represents the various comparison operations available in constraint validation.
|
|
10
|
+
*/
|
|
11
|
+
export type TComparisonOperator = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte';
|
|
12
|
+
/**
|
|
13
|
+
* Type alias for validation result patterns.
|
|
14
|
+
* Represents the union of possible validation outcomes.
|
|
15
|
+
*/
|
|
16
|
+
export type TValidationResult = true | Error;
|
|
17
|
+
/**
|
|
18
|
+
* Configuration interface for assertion exception handling.
|
|
19
|
+
*
|
|
20
|
+
* This interface allows comprehensive customization of error behavior when assertions fail.
|
|
21
|
+
* It provides flexibility to override both the error class and message used when throwing
|
|
22
|
+
* assertion failures, enabling consistent error handling patterns across different assertion
|
|
23
|
+
* types. This design supports both simple message overrides and complex error type hierarchies.
|
|
24
|
+
*
|
|
25
|
+
* The exception configuration follows an opt-in pattern where all properties are optional,
|
|
26
|
+
* allowing users to customize only the aspects they need while falling back to sensible
|
|
27
|
+
* defaults for unconfigured options.
|
|
28
|
+
*
|
|
29
|
+
* @interface IAssertException
|
|
30
|
+
* @since 1.0.0
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* Basic usage with custom messages:
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Custom error class with custom message
|
|
36
|
+
* const config: IAssertException = {
|
|
37
|
+
* class: ValidationError,
|
|
38
|
+
* message: "Custom validation failed"
|
|
39
|
+
* };
|
|
40
|
+
*
|
|
41
|
+
* // Custom message only (uses default error class from assertion)
|
|
42
|
+
* const config2: IAssertException = {
|
|
43
|
+
* message: "Value must be positive"
|
|
44
|
+
* };
|
|
45
|
+
*
|
|
46
|
+
* // Custom error class only (uses default message from assertion)
|
|
47
|
+
* const config3: IAssertException = {
|
|
48
|
+
* class: TypeError
|
|
49
|
+
* };
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* Advanced usage in assertion functions:
|
|
54
|
+
* ```typescript
|
|
55
|
+
* import { AssertString } from './string.js';
|
|
56
|
+
*
|
|
57
|
+
* // Using with built-in assertions
|
|
58
|
+
* try {
|
|
59
|
+
* AssertString(123, {
|
|
60
|
+
* class: TypeError,
|
|
61
|
+
* message: "Username must be a string"
|
|
62
|
+
* });
|
|
63
|
+
* } catch (error) {
|
|
64
|
+
* console.log(error instanceof TypeError); // true
|
|
65
|
+
* console.log(error.message); // "Username must be a string"
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* // Reusable error configurations
|
|
69
|
+
* const validationConfig: IAssertException = {
|
|
70
|
+
* class: ValidationError,
|
|
71
|
+
* message: "Input validation failed"
|
|
72
|
+
* };
|
|
73
|
+
*
|
|
74
|
+
* AssertString(value, validationConfig);
|
|
75
|
+
* AssertNumber(otherValue, validationConfig);
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export interface IAssertException {
|
|
79
|
+
/**
|
|
80
|
+
* Custom error class constructor to use when the assertion fails.
|
|
81
|
+
*
|
|
82
|
+
* This property allows specifying a custom error class that will be instantiated
|
|
83
|
+
* and thrown when the assertion fails. The class must be constructable with a
|
|
84
|
+
* message parameter. If not provided, the assertion function will use its default
|
|
85
|
+
* error class (e.g., StringError for string assertions, NumberError for number assertions).
|
|
86
|
+
*
|
|
87
|
+
* @default - Uses the default error class specific to each assertion function
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* // Using built-in error types
|
|
92
|
+
* const config1: IAssertException = { class: TypeError };
|
|
93
|
+
* const config2: IAssertException = { class: RangeError };
|
|
94
|
+
*
|
|
95
|
+
* // Using custom error classes
|
|
96
|
+
* class ValidationError extends Error {
|
|
97
|
+
* constructor(message: string) {
|
|
98
|
+
* super(message);
|
|
99
|
+
* this.name = 'ValidationError';
|
|
100
|
+
* }
|
|
101
|
+
* }
|
|
102
|
+
*
|
|
103
|
+
* const config3: IAssertException = { class: ValidationError };
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
class?: TConstructableObject;
|
|
107
|
+
/**
|
|
108
|
+
* Custom error message to use when the assertion fails.
|
|
109
|
+
*
|
|
110
|
+
* This property allows specifying a custom error message that will be used
|
|
111
|
+
* when creating the error instance. If not provided, the assertion function
|
|
112
|
+
* will generate a default descriptive message based on the specific validation
|
|
113
|
+
* that failed and the values involved.
|
|
114
|
+
*
|
|
115
|
+
* @default - Uses auto-generated descriptive message based on the failed assertion
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Custom messages for different contexts
|
|
120
|
+
* const userValidation: IAssertException = {
|
|
121
|
+
* message: "User ID must be a positive integer"
|
|
122
|
+
* };
|
|
123
|
+
*
|
|
124
|
+
* const apiValidation: IAssertException = {
|
|
125
|
+
* message: "API response format is invalid"
|
|
126
|
+
* };
|
|
127
|
+
*
|
|
128
|
+
* // Context-specific error messages
|
|
129
|
+
* const configValidation: IAssertException = {
|
|
130
|
+
* class: ValidationError,
|
|
131
|
+
* message: "Invalid configuration: expected string value"
|
|
132
|
+
* };
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
message?: string;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Type guard predicate function
|
|
139
|
+
* @template T - The type being guarded
|
|
140
|
+
* @param value - The value to check
|
|
141
|
+
* @returns true if value is of type T, false otherwise
|
|
142
|
+
*/
|
|
143
|
+
export type TGuard<T> = (value: unknown) => value is T;
|
|
144
|
+
/**
|
|
145
|
+
* Validation predicate function
|
|
146
|
+
* @template T - The type of value being validated
|
|
147
|
+
* @param value - The value to validate
|
|
148
|
+
* @returns true if valid, false otherwise
|
|
149
|
+
*/
|
|
150
|
+
export type TValidationPredicate<T = unknown> = (value: T) => boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Array type guard predicate
|
|
153
|
+
* @template T - The element type
|
|
154
|
+
* @param value - The value to check
|
|
155
|
+
* @returns true if value is an array of type T[], false otherwise
|
|
156
|
+
*/
|
|
157
|
+
export type TArrayTypeGuard<T> = (value: unknown) => value is T[];
|
|
158
|
+
/**
|
|
159
|
+
* Object type guard predicate
|
|
160
|
+
* @template T - The object type
|
|
161
|
+
* @param value - The value to check
|
|
162
|
+
* @returns true if value is of object type T, false otherwise
|
|
163
|
+
*/
|
|
164
|
+
export type TObjectTypeGuard<T extends object> = (value: unknown) => value is T;
|
|
165
|
+
/**
|
|
166
|
+
* Nullable type guard predicate
|
|
167
|
+
* @template T - The non-null type
|
|
168
|
+
* @param value - The value to check
|
|
169
|
+
* @returns true if value is not null or undefined, false otherwise
|
|
170
|
+
*/
|
|
171
|
+
export type TNonNullableGuard<T> = (value: T | null | undefined) => value is T;
|
|
172
|
+
/**
|
|
173
|
+
* Custom assertion function type
|
|
174
|
+
* @template T - The type being asserted
|
|
175
|
+
* @param value - The value to assert
|
|
176
|
+
* @param message - Optional error message
|
|
177
|
+
* @throws IAssertException if assertion fails
|
|
178
|
+
*/
|
|
179
|
+
export type TAssertFunction<T = unknown> = (value: unknown, message?: string) => asserts value is T;
|
|
180
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/asserts/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAE3D;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAE5E;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,CAAC;AAE5E;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG,IAAI,GAAG,KAAK,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,MAAM,WAAW,gBAAgB;IAChC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,KAAK,CAAC,EAAE,oBAAoB,CAAC;IAE7B;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;GAKG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAAC;AAEvD;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC;AAEtE;;;;;GAKG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,EAAE,CAAC;AAElE;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAAC;AAEhF;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,KAAK,KAAK,IAAI,CAAC,CAAC;AAE/E;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/asserts/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { TConstructableObject } from './internal-utils.js';
|
|
2
|
+
import { IAssertException } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Throws an exception using the configured error class and message.
|
|
5
|
+
*
|
|
6
|
+
* This function is the central point for throwing assertion errors throughout the library.
|
|
7
|
+
* It respects the exception configuration provided, using either a custom error class
|
|
8
|
+
* or falling back to the standard Error class. The function ensures that all assertion
|
|
9
|
+
* failures are thrown consistently with appropriate error types and messages.
|
|
10
|
+
*
|
|
11
|
+
* @param exception - Exception configuration containing error class and/or message
|
|
12
|
+
* @throws {Error} The configured error class with the specified message, or generic Error if no class specified
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Throw with custom error class
|
|
17
|
+
* ThrowException({
|
|
18
|
+
* class: TypeError,
|
|
19
|
+
* message: "Expected string but got number"
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Throw with default Error class
|
|
23
|
+
* ThrowException({
|
|
24
|
+
* message: "Validation failed"
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Throw with minimal configuration
|
|
28
|
+
* ThrowException({}); // Throws generic "An assertion error occurred"
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function ThrowException(exception: IAssertException): void;
|
|
32
|
+
/**
|
|
33
|
+
* Sets the error class for an exception configuration if not already specified.
|
|
34
|
+
*
|
|
35
|
+
* This utility function is used to configure the default error class that should be
|
|
36
|
+
* thrown when an assertion fails. It respects existing configuration unless forced,
|
|
37
|
+
* allowing assertion functions to set sensible defaults while still permitting
|
|
38
|
+
* user customization.
|
|
39
|
+
*
|
|
40
|
+
* @param exception - Exception configuration object to modify
|
|
41
|
+
* @param errorClass - The error class constructor to set as default
|
|
42
|
+
* @param force - Whether to override existing error class configuration (default: false)
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const config: IAssertException = {};
|
|
47
|
+
*
|
|
48
|
+
* // Set default error class (won't override if already set)
|
|
49
|
+
* SetExceptionClass(config, TypeError);
|
|
50
|
+
* console.log(config.class === TypeError); // true
|
|
51
|
+
*
|
|
52
|
+
* // Won't override existing class unless forced
|
|
53
|
+
* SetExceptionClass(config, RangeError);
|
|
54
|
+
* console.log(config.class === TypeError); // still true
|
|
55
|
+
*
|
|
56
|
+
* // Force override existing class
|
|
57
|
+
* SetExceptionClass(config, RangeError, true);
|
|
58
|
+
* console.log(config.class === RangeError); // true
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function SetExceptionClass(exception: IAssertException, errorClass: TConstructableObject, force?: boolean): void;
|
|
62
|
+
/**
|
|
63
|
+
* Sets the error message for an exception configuration if not already specified.
|
|
64
|
+
*
|
|
65
|
+
* This utility function is used to configure the default error message that should be
|
|
66
|
+
* used when an assertion fails. It respects existing configuration unless forced,
|
|
67
|
+
* allowing assertion functions to set descriptive defaults while still permitting
|
|
68
|
+
* user customization of error messages.
|
|
69
|
+
*
|
|
70
|
+
* @param exception - Exception configuration object to modify
|
|
71
|
+
* @param message - The error message to set as default
|
|
72
|
+
* @param force - Whether to override existing error message configuration (default: false)
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const config: IAssertException = {};
|
|
77
|
+
*
|
|
78
|
+
* // Set default error message (won't override if already set)
|
|
79
|
+
* SetExceptionMessage(config, "Value must be positive");
|
|
80
|
+
* console.log(config.message); // "Value must be positive"
|
|
81
|
+
*
|
|
82
|
+
* // Won't override existing message unless forced
|
|
83
|
+
* SetExceptionMessage(config, "Different message");
|
|
84
|
+
* console.log(config.message); // still "Value must be positive"
|
|
85
|
+
*
|
|
86
|
+
* // Force override existing message
|
|
87
|
+
* SetExceptionMessage(config, "Forced message", true);
|
|
88
|
+
* console.log(config.message); // "Forced message"
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function SetExceptionMessage(exception: IAssertException, message: string, force?: boolean): void;
|
|
92
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/asserts/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,gBAAgB,GAAG,IAAI,CAKhE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,gBAAgB,EAAE,UAAU,EAAE,oBAAoB,EAAE,KAAK,GAAE,OAAe,GAAG,IAAI,CAG7H;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,OAAe,GAAG,IAAI,CAG9G"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws an exception using the configured error class and message.
|
|
3
|
+
*
|
|
4
|
+
* This function is the central point for throwing assertion errors throughout the library.
|
|
5
|
+
* It respects the exception configuration provided, using either a custom error class
|
|
6
|
+
* or falling back to the standard Error class. The function ensures that all assertion
|
|
7
|
+
* failures are thrown consistently with appropriate error types and messages.
|
|
8
|
+
*
|
|
9
|
+
* @param exception - Exception configuration containing error class and/or message
|
|
10
|
+
* @throws {Error} The configured error class with the specified message, or generic Error if no class specified
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Throw with custom error class
|
|
15
|
+
* ThrowException({
|
|
16
|
+
* class: TypeError,
|
|
17
|
+
* message: "Expected string but got number"
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // Throw with default Error class
|
|
21
|
+
* ThrowException({
|
|
22
|
+
* message: "Validation failed"
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Throw with minimal configuration
|
|
26
|
+
* ThrowException({}); // Throws generic "An assertion error occurred"
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function ThrowException(exception) {
|
|
30
|
+
if (!exception.class) {
|
|
31
|
+
throw new Error(exception.message ?? 'An assertion error occurred');
|
|
32
|
+
}
|
|
33
|
+
throw new exception.class(exception.message ?? 'An assertion error occurred');
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Sets the error class for an exception configuration if not already specified.
|
|
37
|
+
*
|
|
38
|
+
* This utility function is used to configure the default error class that should be
|
|
39
|
+
* thrown when an assertion fails. It respects existing configuration unless forced,
|
|
40
|
+
* allowing assertion functions to set sensible defaults while still permitting
|
|
41
|
+
* user customization.
|
|
42
|
+
*
|
|
43
|
+
* @param exception - Exception configuration object to modify
|
|
44
|
+
* @param errorClass - The error class constructor to set as default
|
|
45
|
+
* @param force - Whether to override existing error class configuration (default: false)
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const config: IAssertException = {};
|
|
50
|
+
*
|
|
51
|
+
* // Set default error class (won't override if already set)
|
|
52
|
+
* SetExceptionClass(config, TypeError);
|
|
53
|
+
* console.log(config.class === TypeError); // true
|
|
54
|
+
*
|
|
55
|
+
* // Won't override existing class unless forced
|
|
56
|
+
* SetExceptionClass(config, RangeError);
|
|
57
|
+
* console.log(config.class === TypeError); // still true
|
|
58
|
+
*
|
|
59
|
+
* // Force override existing class
|
|
60
|
+
* SetExceptionClass(config, RangeError, true);
|
|
61
|
+
* console.log(config.class === RangeError); // true
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export function SetExceptionClass(exception, errorClass, force = false) {
|
|
65
|
+
if (!force && exception.class !== undefined)
|
|
66
|
+
return;
|
|
67
|
+
exception.class = errorClass;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Sets the error message for an exception configuration if not already specified.
|
|
71
|
+
*
|
|
72
|
+
* This utility function is used to configure the default error message that should be
|
|
73
|
+
* used when an assertion fails. It respects existing configuration unless forced,
|
|
74
|
+
* allowing assertion functions to set descriptive defaults while still permitting
|
|
75
|
+
* user customization of error messages.
|
|
76
|
+
*
|
|
77
|
+
* @param exception - Exception configuration object to modify
|
|
78
|
+
* @param message - The error message to set as default
|
|
79
|
+
* @param force - Whether to override existing error message configuration (default: false)
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const config: IAssertException = {};
|
|
84
|
+
*
|
|
85
|
+
* // Set default error message (won't override if already set)
|
|
86
|
+
* SetExceptionMessage(config, "Value must be positive");
|
|
87
|
+
* console.log(config.message); // "Value must be positive"
|
|
88
|
+
*
|
|
89
|
+
* // Won't override existing message unless forced
|
|
90
|
+
* SetExceptionMessage(config, "Different message");
|
|
91
|
+
* console.log(config.message); // still "Value must be positive"
|
|
92
|
+
*
|
|
93
|
+
* // Force override existing message
|
|
94
|
+
* SetExceptionMessage(config, "Forced message", true);
|
|
95
|
+
* console.log(config.message); // "Forced message"
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export function SetExceptionMessage(exception, message, force = false) {
|
|
99
|
+
if (!force && exception.message !== undefined)
|
|
100
|
+
return;
|
|
101
|
+
exception.message = message;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/asserts/utils.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,cAAc,CAAC,SAA2B;IACzD,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,OAAO,IAAI,6BAA6B,CAAC,CAAC;IACrE,CAAC;IACD,MAAM,IAAI,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,IAAI,6BAA6B,CAAC,CAAC;AAC/E,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,iBAAiB,CAAC,SAA2B,EAAE,UAAgC,EAAE,QAAiB,KAAK;IACtH,IAAI,CAAC,KAAK,IAAI,SAAS,CAAC,KAAK,KAAK,SAAS;QAAE,OAAO;IACpD,SAAS,CAAC,KAAK,GAAG,UAAU,CAAC;AAC9B,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,mBAAmB,CAAC,SAA2B,EAAE,OAAe,EAAE,QAAiB,KAAK;IACvG,IAAI,CAAC,KAAK,IAAI,SAAS,CAAC,OAAO,KAAK,SAAS;QAAE,OAAO;IACtD,SAAS,CAAC,OAAO,GAAG,OAAO,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { IAssertException } from '../asserts/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a value is not a valid boolean or fails a boolean assertion.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* throw new BooleanError('Value is not a valid boolean');
|
|
7
|
+
*/
|
|
8
|
+
export declare class BooleanError extends Error {
|
|
9
|
+
constructor(message?: string);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Asserts that a value is a boolean primitive type.
|
|
13
|
+
*
|
|
14
|
+
* This function performs a strict type assertion that validates the provided value
|
|
15
|
+
* is of type 'boolean', ensuring it's either `true` or `false`. The assertion will
|
|
16
|
+
* reject truthy/falsy values that are not actual boolean primitives, making it
|
|
17
|
+
* ideal for type narrowing in TypeScript and runtime type validation.
|
|
18
|
+
*
|
|
19
|
+
* If the assertion fails, the function throws an exception and never returns.
|
|
20
|
+
* If the assertion passes, TypeScript will narrow the type to `boolean` for
|
|
21
|
+
* subsequent code execution.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The value to validate and assert as a boolean primitive
|
|
24
|
+
* @param exception - Optional exception configuration for custom error handling.
|
|
25
|
+
* Can include custom error message, error type, or other metadata.
|
|
26
|
+
* @throws {Error} When value is not a boolean primitive. The specific error type
|
|
27
|
+
* depends on the exception configuration provided.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* Basic usage with valid boolean values:
|
|
31
|
+
* ```typescript
|
|
32
|
+
* AssertBoolean(true); // ✓ Passes - value is boolean true
|
|
33
|
+
* AssertBoolean(false); // ✓ Passes - value is boolean false
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* Assertion failures with non-boolean values:
|
|
38
|
+
* ```typescript
|
|
39
|
+
* AssertBoolean(1); // ✗ Throws - truthy number, not boolean
|
|
40
|
+
* AssertBoolean(0); // ✗ Throws - falsy number, not boolean
|
|
41
|
+
* AssertBoolean("true"); // ✗ Throws - string, not boolean
|
|
42
|
+
* AssertBoolean("false"); // ✗ Throws - string, not boolean
|
|
43
|
+
* AssertBoolean(null); // ✗ Throws - null, not boolean
|
|
44
|
+
* AssertBoolean(undefined); // ✗ Throws - undefined, not boolean
|
|
45
|
+
* AssertBoolean([]); // ✗ Throws - array, not boolean
|
|
46
|
+
* AssertBoolean({}); // ✗ Throws - object, not boolean
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* Using with custom exception handling:
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { AssertBoolean } from './boolean.js';
|
|
53
|
+
*
|
|
54
|
+
* // Custom error message
|
|
55
|
+
* AssertBoolean(value, { message: 'Expected a boolean value' });
|
|
56
|
+
*
|
|
57
|
+
* // Type narrowing after successful assertion
|
|
58
|
+
* function processValue(input: unknown) {
|
|
59
|
+
* AssertBoolean(input);
|
|
60
|
+
* // TypeScript now knows 'input' is boolean
|
|
61
|
+
* return input ? 'yes' : 'no';
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function AssertBoolean(value: unknown, exception?: IAssertException): asserts value is boolean;
|
|
66
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/boolean/assert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAG5D;;;;;GAKG;AACH,qBAAa,YAAa,SAAQ,KAAK;gBAC1B,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,GAAE,gBAAqB,GAAG,OAAO,CAAC,KAAK,IAAI,OAAO,CAMxG"}
|