@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.
Files changed (78) hide show
  1. package/README.md +177 -2
  2. package/build/array/assert.d.ts +115 -0
  3. package/build/array/assert.d.ts.map +1 -0
  4. package/build/array/assert.js +182 -0
  5. package/build/array/assert.js.map +1 -0
  6. package/build/array/index.d.ts +1 -0
  7. package/build/array/index.d.ts.map +1 -1
  8. package/build/array/index.js +1 -0
  9. package/build/array/index.js.map +1 -1
  10. package/build/asserts/errors.d.ts +45 -0
  11. package/build/asserts/errors.d.ts.map +1 -0
  12. package/build/asserts/errors.js +65 -0
  13. package/build/asserts/errors.js.map +1 -0
  14. package/build/asserts/generic.d.ts +432 -0
  15. package/build/asserts/generic.d.ts.map +1 -0
  16. package/build/asserts/generic.js +539 -0
  17. package/build/asserts/generic.js.map +1 -0
  18. package/build/asserts/index.d.ts +41 -0
  19. package/build/asserts/index.d.ts.map +1 -0
  20. package/build/asserts/index.js +41 -0
  21. package/build/asserts/index.js.map +1 -0
  22. package/build/asserts/internal-utils.d.ts +53 -0
  23. package/build/asserts/internal-utils.d.ts.map +1 -0
  24. package/build/asserts/internal-utils.js +108 -0
  25. package/build/asserts/internal-utils.js.map +1 -0
  26. package/build/asserts/object.d.ts +138 -0
  27. package/build/asserts/object.d.ts.map +1 -0
  28. package/build/asserts/object.js +204 -0
  29. package/build/asserts/object.js.map +1 -0
  30. package/build/asserts/string.d.ts +100 -0
  31. package/build/asserts/string.d.ts.map +1 -0
  32. package/build/asserts/string.js +185 -0
  33. package/build/asserts/string.js.map +1 -0
  34. package/build/asserts/types.d.ts +180 -0
  35. package/build/asserts/types.d.ts.map +1 -0
  36. package/build/asserts/types.js +2 -0
  37. package/build/asserts/types.js.map +1 -0
  38. package/build/asserts/utils.d.ts +92 -0
  39. package/build/asserts/utils.d.ts.map +1 -0
  40. package/build/asserts/utils.js +103 -0
  41. package/build/asserts/utils.js.map +1 -0
  42. package/build/boolean/assert.d.ts +66 -0
  43. package/build/boolean/assert.d.ts.map +1 -0
  44. package/build/boolean/assert.js +76 -0
  45. package/build/boolean/assert.js.map +1 -0
  46. package/build/boolean/index.d.ts +9 -0
  47. package/build/boolean/index.d.ts.map +1 -0
  48. package/build/boolean/index.js +9 -0
  49. package/build/boolean/index.js.map +1 -0
  50. package/build/index.d.ts +4 -0
  51. package/build/index.d.ts.map +1 -1
  52. package/build/index.js +12 -0
  53. package/build/index.js.map +1 -1
  54. package/build/number/assert.d.ts +154 -0
  55. package/build/number/assert.d.ts.map +1 -0
  56. package/build/number/assert.js +153 -0
  57. package/build/number/assert.js.map +1 -0
  58. package/build/number/index.d.ts +9 -0
  59. package/build/number/index.d.ts.map +1 -0
  60. package/build/number/index.js +9 -0
  61. package/build/number/index.js.map +1 -0
  62. package/build/object/assert.d.ts +138 -0
  63. package/build/object/assert.d.ts.map +1 -0
  64. package/build/object/assert.js +204 -0
  65. package/build/object/assert.js.map +1 -0
  66. package/build/object/index.d.ts +1 -0
  67. package/build/object/index.d.ts.map +1 -1
  68. package/build/object/index.js +1 -0
  69. package/build/object/index.js.map +1 -1
  70. package/build/string/assert.d.ts +100 -0
  71. package/build/string/assert.d.ts.map +1 -0
  72. package/build/string/assert.js +185 -0
  73. package/build/string/assert.js.map +1 -0
  74. package/build/string/index.d.ts +1 -0
  75. package/build/string/index.d.ts.map +1 -1
  76. package/build/string/index.js +1 -0
  77. package/build/string/index.js.map +1 -1
  78. package/package.json +14 -2
@@ -0,0 +1,185 @@
1
+ import { SetExceptionClass, SetExceptionMessage, ThrowException } from './utils.js';
2
+ /**
3
+ * Cache for compiled regex patterns to improve performance when the same patterns are used repeatedly.
4
+ * Maps regex source + flags to the compiled RegExp object for efficient reuse.
5
+ *
6
+ * @internal
7
+ */
8
+ const REGEX_PATTERN_CACHE = new Map();
9
+ /**
10
+ * Maximum number of cached regex patterns to prevent unbounded memory growth.
11
+ * When this limit is reached, the cache will be cleared to maintain memory efficiency.
12
+ *
13
+ * @internal
14
+ */
15
+ const MAX_CACHE_SIZE = 100;
16
+ /**
17
+ * Number of oldest cache entries to evict in one batch when the cache is full.
18
+ *
19
+ * @internal
20
+ */
21
+ const CACHE_EVICTION_BATCH_SIZE = 20;
22
+ /**
23
+ * Gets a cached regex pattern or creates and caches a new one.
24
+ * This optimization improves performance when the same regex patterns are used repeatedly.
25
+ *
26
+ * @param source - The regex pattern source string
27
+ * @param flags - The regex flags string
28
+ * @returns The compiled RegExp object
29
+ * @internal
30
+ */
31
+ function getCachedRegex(source, flags = '') {
32
+ const cacheKey = `${source}:::${flags}`;
33
+ let regex = REGEX_PATTERN_CACHE.get(cacheKey);
34
+ if (!regex) {
35
+ // Use LRU eviction: delete oldest entries when cache reaches limit
36
+ if (REGEX_PATTERN_CACHE.size >= MAX_CACHE_SIZE) {
37
+ const keysToDelete = Array.from(REGEX_PATTERN_CACHE.keys()).slice(0, CACHE_EVICTION_BATCH_SIZE);
38
+ keysToDelete.forEach(key => REGEX_PATTERN_CACHE.delete(key));
39
+ }
40
+ regex = new RegExp(source, flags);
41
+ REGEX_PATTERN_CACHE.set(cacheKey, regex);
42
+ }
43
+ return regex;
44
+ }
45
+ /**
46
+ * Error thrown when a value is not a valid string or fails a string assertion.
47
+ *
48
+ * @example
49
+ * throw new StringError('Value is not a valid string');
50
+ */
51
+ export class StringError extends Error {
52
+ constructor(message) {
53
+ super(message ?? 'String Assertion Failed');
54
+ this.name = 'StringError';
55
+ Object.setPrototypeOf(this, StringError.prototype);
56
+ }
57
+ }
58
+ /**
59
+ * Asserts that a value is a string primitive type.
60
+ *
61
+ * This method validates that the provided value is of type 'string'. It accepts
62
+ * any string including empty strings. This is a strict type check that will
63
+ * reject string objects created with new String().
64
+ *
65
+ * @template TError - Custom error type to throw on failure
66
+ * @param value - The value to validate as a string
67
+ * @param exception - Optional exception configuration for custom error handling
68
+ * @throws {StringError} When value is not a string primitive
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * AssertString("hello"); // ✓ Valid
73
+ * AssertString(""); // ✓ Valid (empty string is still a string)
74
+ * AssertString("123"); // ✓ Valid (numeric string)
75
+ * AssertString(123); // ✗ Throws StringError (number)
76
+ * AssertString(null); // ✗ Throws StringError
77
+ * AssertString(new String("hello")); // ✗ Throws StringError (String object)
78
+ * ```
79
+ */
80
+ export function AssertString(value, exception = {}) {
81
+ SetExceptionClass(exception, StringError);
82
+ if (typeof value !== 'string') {
83
+ SetExceptionMessage(exception, `Expected string but received ${typeof value}: ${String(value)}`);
84
+ ThrowException(exception);
85
+ }
86
+ }
87
+ /**
88
+ * Asserts that a value is a non-empty string (after trimming whitespace).
89
+ *
90
+ * This method validates that the provided value is a string and contains at least
91
+ * one non-whitespace character after trimming. This is useful for validating user
92
+ * inputs, form fields, and API parameters where empty or whitespace-only strings
93
+ * are not acceptable.
94
+ *
95
+ * @template TError - Custom error type to throw on failure
96
+ * @param value - The value to validate as a non-empty string
97
+ * @param exception - Optional exception configuration for custom error handling
98
+ * @throws {StringError} When value is not a string or is empty/whitespace-only
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * AssertStringNotEmpty("hello"); // ✓ Valid
103
+ * AssertStringNotEmpty(" a "); // ✓ Valid (has non-whitespace content)
104
+ * AssertStringNotEmpty("x"); // ✓ Valid (single character)
105
+ * AssertStringNotEmpty(""); // ✗ Throws StringError (empty)
106
+ * AssertStringNotEmpty(" "); // ✗ Throws StringError (whitespace only)
107
+ * AssertStringNotEmpty("\t\n "); // ✗ Throws StringError (whitespace only)
108
+ * AssertStringNotEmpty(123); // ✗ Throws StringError (not a string)
109
+ * ```
110
+ */
111
+ export function AssertStringNotEmpty(value, exception = {}) {
112
+ SetExceptionClass(exception, StringError);
113
+ if (typeof value !== 'string') {
114
+ SetExceptionMessage(exception, `Expected non-empty string but received ${typeof value}: ${String(value)}`);
115
+ ThrowException(exception);
116
+ }
117
+ const str = value;
118
+ if (str === '') {
119
+ SetExceptionMessage(exception, 'Expected non-empty string but received empty string');
120
+ ThrowException(exception);
121
+ }
122
+ if (str.trim() === '') {
123
+ SetExceptionMessage(exception, 'Expected non-empty string but received whitespace-only string');
124
+ ThrowException(exception);
125
+ }
126
+ }
127
+ /**
128
+ * Asserts that a string matches a regular expression pattern.
129
+ *
130
+ * This method validates that the provided string matches the specified regular
131
+ * expression pattern. The string must be a valid string type (use AssertString first
132
+ * if needed). Useful for validating formats like emails, phone numbers, IDs, and
133
+ * other structured text data.
134
+ *
135
+ * For performance optimization, regex patterns are cached when possible to avoid
136
+ * recompilation of the same patterns. This provides significant performance benefits
137
+ * when validating many values against the same pattern.
138
+ *
139
+ * @template TError - Custom error type to throw on failure
140
+ * @param value - The string to test against the pattern
141
+ * @param regex - The regular expression pattern to match against
142
+ * @param exception - Optional exception configuration for custom error handling
143
+ * @throws {StringError} When string does not match the pattern
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * // Email validation
148
+ * AssertStringMatches("hello@example.com", /^[^\s@]+@[^\s@]+\.[^\s@]+$/); // ✓ Valid
149
+ *
150
+ * // Digits only validation
151
+ * AssertStringMatches("123", /^\d+$/); // ✓ Valid
152
+ * AssertStringMatches("12a", /^\d+$/); // ✗ Throws StringError
153
+ *
154
+ * // Phone number format
155
+ * AssertStringMatches("(555) 123-4567", /^\(\d{3}\) \d{3}-\d{4}$/); // ✓ Valid
156
+ *
157
+ * // Alphanumeric with length constraint
158
+ * AssertStringMatches("abc123", /^[a-zA-Z0-9]{3,10}$/); // ✓ Valid
159
+ * AssertStringMatches("ab", /^[a-zA-Z0-9]{3,10}$/); // ✗ Throws (too short)
160
+ *
161
+ * // Performance benefit with repeated pattern usage
162
+ * const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
163
+ * AssertStringMatches("user1@example.com", emailPattern); // Cached for future use
164
+ * AssertStringMatches("user2@example.com", emailPattern); // Uses cached pattern
165
+ * ```
166
+ */
167
+ export function AssertStringMatches(value, regex, exception = {}) {
168
+ SetExceptionClass(exception, StringError);
169
+ AssertString(value, exception); // Ensure value is a string before matching
170
+ // Use cached regex for better performance when possible
171
+ // If the regex has a source and flags, we can cache it for reuse
172
+ let testRegex;
173
+ if (regex.source && regex.flags !== undefined) {
174
+ testRegex = getCachedRegex(regex.source, regex.flags);
175
+ }
176
+ else {
177
+ // Fallback to original regex if caching is not possible
178
+ testRegex = regex;
179
+ }
180
+ if (!testRegex.test(value)) {
181
+ SetExceptionMessage(exception, `String does not match the required pattern: ${regex.toString()}`);
182
+ ThrowException(exception);
183
+ }
184
+ }
185
+ //# sourceMappingURL=string.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string.js","sourceRoot":"","sources":["../../src/asserts/string.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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -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"}