@pawells/typescript-common 1.0.0 → 1.1.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/README.md +177 -2
- 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/asserts/errors.d.ts +45 -0
- package/build/asserts/errors.d.ts.map +1 -0
- package/build/asserts/errors.js +65 -0
- package/build/asserts/errors.js.map +1 -0
- package/build/asserts/generic.d.ts +432 -0
- package/build/asserts/generic.d.ts.map +1 -0
- package/build/asserts/generic.js +539 -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/object.d.ts +138 -0
- package/build/asserts/object.d.ts.map +1 -0
- package/build/asserts/object.js +204 -0
- package/build/asserts/object.js.map +1 -0
- package/build/asserts/string.d.ts +100 -0
- package/build/asserts/string.d.ts.map +1 -0
- package/build/asserts/string.js +185 -0
- package/build/asserts/string.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/index.d.ts +4 -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/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/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/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 +14 -2
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { SetExceptionClass, SetExceptionMessage, ThrowException } from '../asserts/utils.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 class BooleanError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message ?? 'Invalid Boolean');
|
|
11
|
+
this.name = 'BooleanError';
|
|
12
|
+
Object.setPrototypeOf(this, BooleanError.prototype);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Asserts that a value is a boolean primitive type.
|
|
17
|
+
*
|
|
18
|
+
* This function performs a strict type assertion that validates the provided value
|
|
19
|
+
* is of type 'boolean', ensuring it's either `true` or `false`. The assertion will
|
|
20
|
+
* reject truthy/falsy values that are not actual boolean primitives, making it
|
|
21
|
+
* ideal for type narrowing in TypeScript and runtime type validation.
|
|
22
|
+
*
|
|
23
|
+
* If the assertion fails, the function throws an exception and never returns.
|
|
24
|
+
* If the assertion passes, TypeScript will narrow the type to `boolean` for
|
|
25
|
+
* subsequent code execution.
|
|
26
|
+
*
|
|
27
|
+
* @param value - The value to validate and assert as a boolean primitive
|
|
28
|
+
* @param exception - Optional exception configuration for custom error handling.
|
|
29
|
+
* Can include custom error message, error type, or other metadata.
|
|
30
|
+
* @throws {Error} When value is not a boolean primitive. The specific error type
|
|
31
|
+
* depends on the exception configuration provided.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* Basic usage with valid boolean values:
|
|
35
|
+
* ```typescript
|
|
36
|
+
* AssertBoolean(true); // ✓ Passes - value is boolean true
|
|
37
|
+
* AssertBoolean(false); // ✓ Passes - value is boolean false
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* Assertion failures with non-boolean values:
|
|
42
|
+
* ```typescript
|
|
43
|
+
* AssertBoolean(1); // ✗ Throws - truthy number, not boolean
|
|
44
|
+
* AssertBoolean(0); // ✗ Throws - falsy number, not boolean
|
|
45
|
+
* AssertBoolean("true"); // ✗ Throws - string, not boolean
|
|
46
|
+
* AssertBoolean("false"); // ✗ Throws - string, not boolean
|
|
47
|
+
* AssertBoolean(null); // ✗ Throws - null, not boolean
|
|
48
|
+
* AssertBoolean(undefined); // ✗ Throws - undefined, not boolean
|
|
49
|
+
* AssertBoolean([]); // ✗ Throws - array, not boolean
|
|
50
|
+
* AssertBoolean({}); // ✗ Throws - object, not boolean
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* Using with custom exception handling:
|
|
55
|
+
* ```typescript
|
|
56
|
+
* import { AssertBoolean } from './boolean.js';
|
|
57
|
+
*
|
|
58
|
+
* // Custom error message
|
|
59
|
+
* AssertBoolean(value, { message: 'Expected a boolean value' });
|
|
60
|
+
*
|
|
61
|
+
* // Type narrowing after successful assertion
|
|
62
|
+
* function processValue(input: unknown) {
|
|
63
|
+
* AssertBoolean(input);
|
|
64
|
+
* // TypeScript now knows 'input' is boolean
|
|
65
|
+
* return input ? 'yes' : 'no';
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function AssertBoolean(value, exception = {}) {
|
|
70
|
+
SetExceptionClass(exception, BooleanError);
|
|
71
|
+
if (typeof value !== 'boolean') {
|
|
72
|
+
SetExceptionMessage(exception, `Expected boolean but received ${typeof value}: ${String(value)}`);
|
|
73
|
+
ThrowException(exception);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../../src/boolean/assert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE7F;;;;;GAKG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IACtC,YAAY,OAAgB;QAC3B,KAAK,CAAC,OAAO,IAAI,iBAAiB,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC;IACrD,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc,EAAE,YAA8B,EAAE;IAC7E,iBAAiB,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;IAC3C,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;QAChC,mBAAmB,CAAC,SAAS,EAAE,iCAAiC,OAAO,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAClG,cAAc,CAAC,SAAS,CAAC,CAAC;IAC3B,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/boolean/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/boolean/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,aAAa,CAAC"}
|
package/build/index.d.ts
CHANGED
|
@@ -19,6 +19,9 @@
|
|
|
19
19
|
*/
|
|
20
20
|
export * as ArrayUtils from './array/index.js';
|
|
21
21
|
export * as ObjectUtils from './object/index.js';
|
|
22
|
+
export * as AssertsUtils from './asserts/index.js';
|
|
23
|
+
export * as BooleanUtils from './boolean/index.js';
|
|
24
|
+
export * as NumberUtils from './number/index.js';
|
|
22
25
|
export * as StringUtils from './string/index.js';
|
|
23
26
|
export * as TimeUtils from './time/index.js';
|
|
24
27
|
export * as EnumUtils from './enum/index.js';
|
|
@@ -29,4 +32,5 @@ export { AssertObject, ObjectClone, TransformObject, ObjectEquals, ObjectFilter,
|
|
|
29
32
|
export { ElapsedTime, Stopwatch, } from './time/index.js';
|
|
30
33
|
export { EnumKeys, EnumValues, EnumEntries, ValidateEnumValue, EnumKeyByValue, EnumSafeValue, } from './enum/index.js';
|
|
31
34
|
export { Debounce, Throttle, Memoize, Once, Compose, Pipe, Sleep, } from './function/index.js';
|
|
35
|
+
export { AssertArray, AssertArray2D, AssertArrayAll, AssertArrayAny, AssertArrayNotEmpty, AssertBoolean, AssertEquals, AssertExtends, AssertFunction, AssertInstanceOf, AssertIsType, AssertNotEquals, AssertNotNull, AssertNull, AssertNumber, AssertObjectHasOwnProperty, AssertObjectHasProperty, AssertObjectPropertyNotNull, AssertPredicate, AssertString, AssertStringMatches, AssertStringNotEmpty, AssertSymbol, SetExceptionClass, SetExceptionMessage, ThrowException, AssertionError, BaseError, BufferOverflowError, InvalidArgumentError, NotFoundError, NotSupportedError, ValidationError, ArrayError, BooleanError, ExtendsError, FunctionError, InstanceOfError, NotNullError, NullError, NumberError, NumberRangeError, ObjectError, ObjectPropertyError, PredicateError, StringError, SymbolError, TypeGuardError, } from './asserts/index.js';
|
|
32
36
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAG/C,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAGjD,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAGjD,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAG7C,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAG7C,OAAO,KAAK,aAAa,MAAM,qBAAqB,CAAC;AAGrD,OAAO,EAEN,WAAW,EACX,aAAa,EACb,MAAM,EACN,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,UAAU,EACV,WAAW,EACX,YAAY,EACZ,WAAW,GACX,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAEN,SAAS,EACT,WAAW,EACX,UAAU,EACV,UAAU,EACV,oBAAoB,EACpB,YAAY,EACZ,UAAU,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,gBAAgB,GAChB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAEN,YAAY,EACZ,WAAW,EACX,eAAe,EACf,YAAY,EACZ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EACT,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,mBAAmB,EACnB,cAAc,EACd,WAAW,EACX,WAAW,EACX,cAAc,GACd,MAAM,oBAAoB,CAAC"}
|
package/build/index.js
CHANGED
|
@@ -21,6 +21,12 @@
|
|
|
21
21
|
export * as ArrayUtils from './array/index.js';
|
|
22
22
|
// Object utilities
|
|
23
23
|
export * as ObjectUtils from './object/index.js';
|
|
24
|
+
// Asserts utilities
|
|
25
|
+
export * as AssertsUtils from './asserts/index.js';
|
|
26
|
+
// Boolean utilities
|
|
27
|
+
export * as BooleanUtils from './boolean/index.js';
|
|
28
|
+
// Number utilities
|
|
29
|
+
export * as NumberUtils from './number/index.js';
|
|
24
30
|
// String utilities
|
|
25
31
|
export * as StringUtils from './string/index.js';
|
|
26
32
|
// Time utilities
|
|
@@ -48,4 +54,10 @@ EnumKeys, EnumValues, EnumEntries, ValidateEnumValue, EnumKeyByValue, EnumSafeVa
|
|
|
48
54
|
export {
|
|
49
55
|
// Function utilities
|
|
50
56
|
Debounce, Throttle, Memoize, Once, Compose, Pipe, Sleep, } from './function/index.js';
|
|
57
|
+
export {
|
|
58
|
+
// Asserts utilities
|
|
59
|
+
// (AssertObject is omitted here — it conflicts with ObjectUtils.AssertObject; use AssertsUtils.AssertObject instead)
|
|
60
|
+
AssertArray, AssertArray2D, AssertArrayAll, AssertArrayAny, AssertArrayNotEmpty, AssertBoolean, AssertEquals, AssertExtends, AssertFunction, AssertInstanceOf, AssertIsType, AssertNotEquals, AssertNotNull, AssertNull, AssertNumber, AssertObjectHasOwnProperty, AssertObjectHasProperty, AssertObjectPropertyNotNull, AssertPredicate, AssertString, AssertStringMatches, AssertStringNotEmpty, AssertSymbol, SetExceptionClass, SetExceptionMessage, ThrowException,
|
|
61
|
+
// Error classes
|
|
62
|
+
AssertionError, BaseError, BufferOverflowError, InvalidArgumentError, NotFoundError, NotSupportedError, ValidationError, ArrayError, BooleanError, ExtendsError, FunctionError, InstanceOfError, NotNullError, NullError, NumberError, NumberRangeError, ObjectError, ObjectPropertyError, PredicateError, StringError, SymbolError, TypeGuardError, } from './asserts/index.js';
|
|
51
63
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,kBAAkB;AAClB,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAE/C,mBAAmB;AACnB,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAEjD,mBAAmB;AACnB,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAEjD,iBAAiB;AACjB,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAE7C,iBAAiB;AACjB,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAE7C,qBAAqB;AACrB,OAAO,KAAK,aAAa,MAAM,qBAAqB,CAAC;AAErD,6CAA6C;AAC7C,OAAO;AACN,kBAAkB;AAClB,WAAW,EACX,aAAa,EACb,MAAM,EACN,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,UAAU,EACV,WAAW,EACX,YAAY,EACZ,WAAW,GACX,MAAM,kBAAkB,CAAC;AAE1B,OAAO;AACN,mBAAmB;AACnB,SAAS,EACT,WAAW,EACX,UAAU,EACV,UAAU,EACV,oBAAoB,EACpB,YAAY,EACZ,UAAU,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,gBAAgB,GAChB,MAAM,mBAAmB,CAAC;AAE3B,OAAO;AACN,mBAAmB;AACnB,YAAY,EACZ,WAAW,EACX,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,UAAU,EACV,WAAW,EACX,SAAS,EACT,UAAU,EACV,cAAc,EACd,uBAAuB,EACvB,qBAA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ACnB,cAAc;AACd,gBAAgB;AAChB,cAAc,EACd,SAAS,EACT,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,eAAe,EACf,YAAY,EACZ,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,mBAAmB,EACnB,cAAc,EACd,WAAW,EACX,WAAW,EACX,cAAc,GACd,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import type { IAssertException } from '../asserts/types.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 declare class NumberError extends Error {
|
|
17
|
+
constructor(message?: string);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Error thrown when a numeric value is outside the expected range or bounds.
|
|
21
|
+
*
|
|
22
|
+
* This specialized error extends NumberError and is used specifically for
|
|
23
|
+
* range validation failures, such as values being too large, too small,
|
|
24
|
+
* or not matching specific equality constraints. It provides more context
|
|
25
|
+
* for range-related validation failures.
|
|
26
|
+
*
|
|
27
|
+
* @extends NumberError
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* throw new NumberRangeError('Value 150 exceeds maximum of 100');
|
|
31
|
+
* throw new NumberRangeError('Value -5 is below minimum of 0');
|
|
32
|
+
* throw new NumberRangeError('Value must be exactly 42');
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class NumberRangeError extends NumberError {
|
|
36
|
+
constructor(message?: string);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Type alias for numeric comparison operators.
|
|
40
|
+
* Represents the available comparison operations for numeric validation.
|
|
41
|
+
*/
|
|
42
|
+
export type TNumericComparison = 'gt' | 'gte' | 'lt' | 'lte' | 'eq';
|
|
43
|
+
/**
|
|
44
|
+
* Type alias for numeric range constraint properties.
|
|
45
|
+
* Groups the range-based validation properties.
|
|
46
|
+
*/
|
|
47
|
+
export type TNumericRangeConstraints = Pick<IAssertNumberArgs, 'gt' | 'gte' | 'lt' | 'lte' | 'eq'>;
|
|
48
|
+
/**
|
|
49
|
+
* Type alias for numeric type constraint properties.
|
|
50
|
+
* Groups the type-based validation properties.
|
|
51
|
+
*/
|
|
52
|
+
export type TNumericTypeConstraints = Pick<IAssertNumberArgs, 'finite' | 'integer'>;
|
|
53
|
+
/**
|
|
54
|
+
* Configuration interface for numeric range validation constraints.
|
|
55
|
+
*
|
|
56
|
+
* This interface defines the various comparison operators that can be applied
|
|
57
|
+
* when validating numeric ranges. All properties are optional, allowing for
|
|
58
|
+
* flexible range definitions. Multiple constraints can be combined to create
|
|
59
|
+
* complex validation rules (e.g., value must be > 0 AND <= 100).
|
|
60
|
+
*
|
|
61
|
+
* @interface RangeArgs
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // Single constraint examples
|
|
65
|
+
* const minOnly: RangeArgs = { gte: 0 }; // >= 0
|
|
66
|
+
* const maxOnly: RangeArgs = { lt: 100 }; // < 100
|
|
67
|
+
* const exactValue: RangeArgs = { eq: 42 }; // === 42
|
|
68
|
+
*
|
|
69
|
+
* // Multiple constraint examples
|
|
70
|
+
* const range: RangeArgs = { gte: 0, lte: 100 }; // 0 <= value <= 100
|
|
71
|
+
* const exclusive: RangeArgs = { gt: 0, lt: 1 }; // 0 < value < 1
|
|
72
|
+
*
|
|
73
|
+
* // Used with AssertNumberRange
|
|
74
|
+
* AssertNumberRange(50, { gte: 0, lte: 100 }); // ✓ Valid
|
|
75
|
+
* AssertNumberRange(-1, { gte: 0, lte: 100 }); // ✗ Throws
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export interface IAssertNumberArgs {
|
|
79
|
+
finite?: boolean;
|
|
80
|
+
integer?: boolean;
|
|
81
|
+
/** Value must be greater than this number (exclusive) */
|
|
82
|
+
gt?: number;
|
|
83
|
+
/** Value must be greater than or equal to this number (inclusive) */
|
|
84
|
+
gte?: number;
|
|
85
|
+
/** Value must be less than this number (exclusive) */
|
|
86
|
+
lt?: number;
|
|
87
|
+
/** Value must be less than or equal to this number (inclusive) */
|
|
88
|
+
lte?: number;
|
|
89
|
+
/** Value must be exactly equal to this number */
|
|
90
|
+
eq?: number;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Asserts that a numeric value meets specified constraints including type, range, and mathematical properties.
|
|
94
|
+
*
|
|
95
|
+
* This function validates that a number satisfies one or more conditions defined in the NumberArgs parameter.
|
|
96
|
+
* It supports various validation types including finite/infinite checks, integer validation, and range
|
|
97
|
+
* constraints with comparison operations. Multiple constraints are evaluated with AND logic - all specified
|
|
98
|
+
* conditions must be satisfied for the assertion to pass.
|
|
99
|
+
*
|
|
100
|
+
* The function is particularly useful for validating user inputs, configuration values, or any numeric
|
|
101
|
+
* data that must meet specific criteria. It provides clear error messages indicating which constraint
|
|
102
|
+
* was violated.
|
|
103
|
+
*
|
|
104
|
+
* @param value - The numeric value to validate against the specified constraints
|
|
105
|
+
* @param args - Object containing one or more validation rules (finite, integer, range constraints)
|
|
106
|
+
* @param exception - Optional configuration for custom error handling and messages
|
|
107
|
+
* @throws {NumberError} When value is not a number or is NaN
|
|
108
|
+
* @throws {NumberRangeError} When value fails any of the specified constraints
|
|
109
|
+
* @returns void - Function only returns if all validations pass
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // Type validation
|
|
114
|
+
* AssertNumber(42, { finite: true }); // ✓ 42 is finite
|
|
115
|
+
* AssertNumber(5, { integer: true }); // ✓ 5 is an integer
|
|
116
|
+
* AssertNumber(3.14, { integer: true }); // ✗ Throws: must be integer
|
|
117
|
+
* AssertNumber(Infinity, { finite: true }); // ✗ Throws: must be finite
|
|
118
|
+
*
|
|
119
|
+
* // Range validation
|
|
120
|
+
* AssertNumber(50, { gte: 0 }); // ✓ 50 >= 0
|
|
121
|
+
* AssertNumber(10, { lt: 100 }); // ✓ 10 < 100
|
|
122
|
+
* AssertNumber(42, { eq: 42 }); // ✓ 42 === 42
|
|
123
|
+
*
|
|
124
|
+
* // Combined constraints (AND logic)
|
|
125
|
+
* AssertNumber(50, { finite: true, gte: 0, lte: 100 }); // ✓ finite and 0 <= 50 <= 100
|
|
126
|
+
* AssertNumber(10, { integer: true, gt: 0, lt: 20 }); // ✓ integer and 0 < 10 < 20
|
|
127
|
+
* AssertNumber(3.5, { integer: true, gte: 0 }); // ✗ Throws: must be integer
|
|
128
|
+
*
|
|
129
|
+
* // Validation failures
|
|
130
|
+
* AssertNumber(-1, { gte: 0 }); // ✗ Throws: must be >= 0
|
|
131
|
+
* AssertNumber(150, { lte: 100 }); // ✗ Throws: must be <= 100
|
|
132
|
+
* AssertNumber(41, { eq: 42 }); // ✗ Throws: must equal 42
|
|
133
|
+
* AssertNumber(50, { gt: 60 }); // ✗ Throws: must be > 60
|
|
134
|
+
*
|
|
135
|
+
* // Practical usage examples
|
|
136
|
+
* function validateAge(age: number) {
|
|
137
|
+
* AssertNumber(age, { finite: true, integer: true, gte: 0, lte: 150 }); // Valid human age
|
|
138
|
+
* }
|
|
139
|
+
*
|
|
140
|
+
* function validatePercentage(pct: number) {
|
|
141
|
+
* AssertNumber(pct, { finite: true, gte: 0, lte: 100 }); // 0-100%
|
|
142
|
+
* }
|
|
143
|
+
*
|
|
144
|
+
* function validateTemperature(temp: number) {
|
|
145
|
+
* AssertNumber(temp, { finite: true, gt: -273.15 }); // Above absolute zero
|
|
146
|
+
* }
|
|
147
|
+
*
|
|
148
|
+
* function validateArrayIndex(index: number) {
|
|
149
|
+
* AssertNumber(index, { integer: true, gte: 0 }); // Non-negative integer
|
|
150
|
+
* }
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export declare function AssertNumber(value: unknown, args?: IAssertNumberArgs, exception?: IAssertException): asserts value is number;
|
|
154
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,WAAW,iBAAiB;IACjC,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/number/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,aAAa,CAAC"}
|