@pawells/typescript-common 1.0.1 → 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 +1 -1
@@ -0,0 +1,539 @@
1
+ import { ObjectEquals } from './internal-utils.js';
2
+ import { SetExceptionClass, SetExceptionMessage, ThrowException } from './utils.js';
3
+ /** Maximum number of characters to include from a value in an error message. */
4
+ const MAX_VALUE_DISPLAY_LENGTH = 50;
5
+ /**
6
+ * Error thrown when a value is unexpectedly null or undefined.
7
+ *
8
+ * @example
9
+ * throw new NotNullError('Value must not be null or undefined');
10
+ */
11
+ export class NullError extends Error {
12
+ constructor(message) {
13
+ super(message ?? 'Value is not null or undefined.');
14
+ this.name = 'NullError';
15
+ Object.setPrototypeOf(this, NullError.prototype);
16
+ }
17
+ }
18
+ /**
19
+ * Error thrown when a value is unexpectedly null or undefined.
20
+ *
21
+ * @example
22
+ * throw new NotNullError('Value must not be null or undefined');
23
+ */
24
+ export class NotNullError extends Error {
25
+ constructor(message) {
26
+ super(message ?? 'Value is null or undefined.');
27
+ this.name = 'NotNullError';
28
+ Object.setPrototypeOf(this, NotNullError.prototype);
29
+ }
30
+ }
31
+ /**
32
+ * Error thrown when a custom predicate assertion fails.
33
+ *
34
+ * @example
35
+ * throw new PredicateError('Predicate assertion failed');
36
+ */
37
+ export class PredicateError extends Error {
38
+ constructor(message) {
39
+ super(message ?? 'Value does not satisfy the predicate condition');
40
+ this.name = 'PredicateError';
41
+ Object.setPrototypeOf(this, PredicateError.prototype);
42
+ }
43
+ }
44
+ /**
45
+ * Error thrown when a value does not conform to a user-supplied type guard.
46
+ *
47
+ * @example
48
+ * throw new TypeGuardError('Value does not conform to the required type');
49
+ */
50
+ export class TypeGuardError extends Error {
51
+ constructor(message) {
52
+ super(message ?? 'Type guard assertion failed');
53
+ this.name = 'TypeGuardError';
54
+ Object.setPrototypeOf(this, TypeGuardError.prototype);
55
+ }
56
+ }
57
+ /**
58
+ * Error thrown when a value is not an instance of the expected constructor.
59
+ *
60
+ * @example
61
+ * throw new InstanceOfError('Value is not an instance of the expected type');
62
+ */
63
+ export class InstanceOfError extends Error {
64
+ constructor(message) {
65
+ super(message ?? 'InstanceOf assertion failed');
66
+ this.name = 'InstanceOfError';
67
+ Object.setPrototypeOf(this, InstanceOfError.prototype);
68
+ }
69
+ }
70
+ /**
71
+ * Error thrown when a value is not a function.
72
+ *
73
+ * @example
74
+ * throw new FunctionError('Value is not a function');
75
+ */
76
+ export class FunctionError extends Error {
77
+ constructor(message) {
78
+ super(message ?? 'Function assertion failed');
79
+ this.name = 'FunctionError';
80
+ Object.setPrototypeOf(this, FunctionError.prototype);
81
+ }
82
+ }
83
+ /**
84
+ * Error thrown when a value is not a symbol.
85
+ *
86
+ * @example
87
+ * throw new SymbolError('Value is not a symbol');
88
+ */
89
+ export class SymbolError extends Error {
90
+ constructor(message) {
91
+ super(message ?? 'Symbol assertion failed');
92
+ this.name = 'SymbolError';
93
+ Object.setPrototypeOf(this, SymbolError.prototype);
94
+ }
95
+ }
96
+ /**
97
+ * Error thrown when a class does not extend the expected base class.
98
+ *
99
+ * @example
100
+ * throw new ExtendsError('Class does not extend the expected base class');
101
+ */
102
+ export class ExtendsError extends Error {
103
+ constructor(message) {
104
+ super(message ?? 'Extends assertion failed');
105
+ this.name = 'ExtendsError';
106
+ Object.setPrototypeOf(this, ExtendsError.prototype);
107
+ }
108
+ }
109
+ /**
110
+ * Asserts that a value equals an expected value using deep equality comparison.
111
+ *
112
+ * This function performs a comprehensive deep equality check between the provided value and
113
+ * the expected value using ObjectUtils.Equals. The comparison algorithm handles:
114
+ * - Primitive values (numbers, strings, booleans, null, undefined)
115
+ * - Complex nested objects with multiple levels of nesting
116
+ * - Arrays with elements in the same order
117
+ * - Mixed data structures combining objects and arrays
118
+ * - Special values like NaN, Infinity, and -0
119
+ *
120
+ * The deep comparison ensures that all nested properties and array elements are
121
+ * recursively compared, making it suitable for complex data structure validation
122
+ * in testing scenarios and runtime assertions.
123
+ *
124
+ * @template T - The type of values being compared (both value and expected must be of same type)
125
+ * @param value - The actual value to compare against the expected value
126
+ * @param expected - The expected value that the actual value should equal
127
+ * @param exception - Configuration object for custom error handling and messaging.
128
+ * Allows customization of error type, message, and additional metadata
129
+ * @throws {PropertyError} When values are not deeply equal, with descriptive error message
130
+ * @throws {TError} When custom exception type is specified and values don't match
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * // Primitive value comparisons
135
+ * AssertEquals(5, 5); // ✓ Valid (numbers)
136
+ * AssertEquals("hello", "hello"); // ✓ Valid (strings)
137
+ * AssertEquals(true, true); // ✓ Valid (booleans)
138
+ *
139
+ * // Array comparisons (order matters)
140
+ * AssertEquals([1, 2, 3], [1, 2, 3]); // ✓ Valid (same elements, same order)
141
+ * AssertEquals([1, 2], [2, 1]); // ✗ Throws (different order)
142
+ * AssertEquals([], []); // ✓ Valid (empty arrays)
143
+ *
144
+ * // Object comparisons (deep equality)
145
+ * AssertEquals({a: 1, b: 2}, {a: 1, b: 2}); // ✓ Valid (same properties)
146
+ * AssertEquals({a: {b: 1}}, {a: {b: 1}}); // ✓ Valid (nested objects)
147
+ * AssertEquals({}, {}); // ✓ Valid (empty objects)
148
+ *
149
+ * // Complex nested structures
150
+ * const obj1 = {users: [{id: 1, name: "John"}], count: 1};
151
+ * const obj2 = {users: [{id: 1, name: "John"}], count: 1};
152
+ * AssertEquals(obj1, obj2); // ✓ Valid (deeply equal)
153
+ *
154
+ * // Failure cases
155
+ * AssertEquals(5, 10); // ✗ Throws PropertyError
156
+ * AssertEquals({a: 1}, {a: 2}); // ✗ Throws PropertyError
157
+ * AssertEquals([1, 2], [1, 2, 3]); // ✗ Throws PropertyError
158
+ *
159
+ * // Custom exception handling
160
+ * AssertEquals(1, 2, {
161
+ * message: "Values should be equal"
162
+ * }); // ✗ Throws with custom message
163
+ * ```
164
+ */
165
+ export function AssertEquals(value, expected, exception = {}) {
166
+ if (!ObjectEquals(value, expected)) {
167
+ SetExceptionMessage(exception, `Expected ${value} to equal ${expected}`);
168
+ ThrowException(exception);
169
+ }
170
+ }
171
+ /**
172
+ * Asserts that a value does not equal an expected value using deep equality comparison.
173
+ *
174
+ * This function performs a comprehensive deep equality check and ensures the values are NOT equal.
175
+ * It uses the same sophisticated comparison logic as AssertEquals but with inverted logic,
176
+ * making it perfect for validating that values have changed, are distinct from unwanted values,
177
+ * or ensuring that mutations have occurred successfully.
178
+ *
179
+ * The deep comparison algorithm checks:
180
+ * - All primitive values for strict inequality
181
+ * - Nested object properties at all levels
182
+ * - Array elements and their ordering
183
+ * - Mixed data structures with complex nesting
184
+ * - Special numeric values (NaN, Infinity, -0)
185
+ *
186
+ * This function is particularly useful in testing scenarios where you need to verify
187
+ * that data transformations, mutations, or state changes have actually occurred.
188
+ *
189
+ * @template T - The type of values being compared (both value and expected must be of same type)
190
+ * @param value - The actual value to compare against the unwanted value
191
+ * @param expected - The value that should NOT be equal to the actual value
192
+ * @param exception - Configuration object for custom error handling and messaging.
193
+ * Defaults to empty object if not provided, allowing for optional customization
194
+ * @throws {PropertyError} When values are deeply equal (and shouldn't be), with descriptive error message
195
+ * @throws {TError} When custom exception type is specified and values are unexpectedly equal
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * // Primitive value comparisons (should be different)
200
+ * AssertNotEquals(5, 10); // ✓ Valid (different numbers)
201
+ * AssertNotEquals("hello", "world"); // ✓ Valid (different strings)
202
+ * AssertNotEquals(true, false); // ✓ Valid (different booleans)
203
+ *
204
+ * // Array comparisons (should be different)
205
+ * AssertNotEquals([1, 2], [1, 3]); // ✓ Valid (different elements)
206
+ * AssertNotEquals([1, 2], [2, 1]); // ✓ Valid (different order)
207
+ * AssertNotEquals([1], [1, 2]); // ✓ Valid (different lengths)
208
+ *
209
+ * // Object comparisons (should be different)
210
+ * AssertNotEquals({a: 1}, {a: 2}); // ✓ Valid (different property values)
211
+ * AssertNotEquals({a: 1}, {b: 1}); // ✓ Valid (different property names)
212
+ * AssertNotEquals({a: {b: 1}}, {a: {b: 2}}); // ✓ Valid (different nested values)
213
+ *
214
+ * // Testing mutations and transformations
215
+ * const original = {users: [{id: 1, name: "John"}]};
216
+ * const modified = {users: [{id: 1, name: "Jane"}]};
217
+ * AssertNotEquals(original, modified); // ✓ Valid (data was modified)
218
+ *
219
+ * // Verifying state changes
220
+ * let counter = 0;
221
+ * const initialState = counter;
222
+ * counter++;
223
+ * AssertNotEquals(counter, initialState); // ✓ Valid (state changed)
224
+ *
225
+ * // Failure cases (when values are unexpectedly equal)
226
+ * AssertNotEquals(5, 5); // ✗ Throws PropertyError
227
+ * AssertNotEquals([1, 2], [1, 2]); // ✗ Throws PropertyError
228
+ * AssertNotEquals({a: 1}, {a: 1}); // ✗ Throws PropertyError
229
+ *
230
+ * // Custom exception handling
231
+ * AssertNotEquals(1, 1, {
232
+ * message: "Values should be different"
233
+ * }); // ✗ Throws with custom message
234
+ * ```
235
+ */
236
+ export function AssertNotEquals(value, expected, exception = {}) {
237
+ if (ObjectEquals(value, expected)) {
238
+ SetExceptionMessage(exception, `Expected ${value} to not equal ${expected}`);
239
+ ThrowException(exception);
240
+ }
241
+ }
242
+ /**
243
+ * Asserts that a value is null or undefined (nullish assertion).
244
+ *
245
+ * This method validates that the provided value is either null or undefined,
246
+ * effectively performing a nullish assertion. This is useful for validating
247
+ * that optional values are properly unset, that cleanup operations succeeded,
248
+ * or that certain conditions result in null/undefined states.
249
+ *
250
+ * @template T - The type of the value being validated
251
+ * @param value - The value to validate as null or undefined
252
+ * @param exception - Optional exception configuration for custom error handling
253
+ * @throws {NullError} When value is not null or undefined
254
+ *
255
+ * @example
256
+ * ```typescript
257
+ * AssertNull(null); // ✓ Valid
258
+ * AssertNull(undefined); // ✓ Valid
259
+ * AssertNull("hello"); // ✗ Throws NullError
260
+ * AssertNull(0); // ✗ Throws NullError
261
+ * AssertNull(false); // ✗ Throws NullError
262
+ * AssertNull(""); // ✗ Throws NullError
263
+ *
264
+ * // Validation of cleanup operations
265
+ * function cleanup(resource: Resource | null) {
266
+ * resource?.dispose();
267
+ * resource = null;
268
+ * AssertNull(resource); // Verify cleanup succeeded
269
+ * }
270
+ * ```
271
+ */
272
+ export function AssertNull(value, exception = {}) {
273
+ SetExceptionClass(exception, NullError);
274
+ if (value !== null && value !== undefined) {
275
+ SetExceptionMessage(exception, `Expected null or undefined but received ${typeof value}: ${JSON.stringify(value)}`);
276
+ ThrowException(exception);
277
+ }
278
+ }
279
+ /**
280
+ * Asserts that a value is not null or undefined (non-nullish assertion).
281
+ *
282
+ * This method validates that the provided value is neither null nor undefined,
283
+ * effectively performing a non-nullish assertion. This is particularly useful
284
+ * for validating function parameters, API responses, and optional values that
285
+ * should have been initialized. After this assertion, TypeScript will narrow
286
+ * the type to exclude null and undefined.
287
+ *
288
+ * @template T - The type of the value being validated
289
+ * @template TError - Custom error type to throw on failure
290
+ * @param value - The value to validate for non-null/undefined
291
+ * @param exception - Optional exception configuration for custom error handling
292
+ * @throws {NotNullError} When value is null or undefined
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * AssertNotNull("hello"); // ✓ Valid
297
+ * AssertNotNull(0); // ✓ Valid (0 is not null/undefined)
298
+ * AssertNotNull(false); // ✓ Valid (false is not null/undefined)
299
+ * AssertNotNull(""); // ✓ Valid (empty string is not null/undefined)
300
+ * AssertNotNull(null); // ✗ Throws NotNullError
301
+ * AssertNotNull(undefined); // ✗ Throws NotNullError
302
+ *
303
+ * // Type narrowing example
304
+ * function process(value: string | null | undefined) {
305
+ * AssertNotNull(value);
306
+ * // value is now typed as string (null/undefined excluded)
307
+ * return value.toUpperCase();
308
+ * }
309
+ * ```
310
+ */
311
+ export function AssertNotNull(value, exception = {}) {
312
+ SetExceptionClass(exception, NotNullError);
313
+ if (value === null || value === undefined) {
314
+ const actualValue = value === null ? 'null' : 'undefined';
315
+ SetExceptionMessage(exception, `Expected non-null/non-undefined value but received ${actualValue}`);
316
+ ThrowException(exception);
317
+ }
318
+ }
319
+ /**
320
+ * Generic assertion method that validates a value using a custom predicate function.
321
+ *
322
+ * This method provides a flexible way to perform custom validations using any
323
+ * predicate function that returns a boolean. It's the most generic assertion
324
+ * in the library and can be used to implement complex validation logic that
325
+ * doesn't fit into other specific assertion methods.
326
+ *
327
+ * @template T - The type of value being validated
328
+ * @template TError - Custom error type to throw on failure
329
+ * @param value - The value to validate
330
+ * @param predicate - A TValidationPredicate function that returns true if the value is valid
331
+ * @param exception - Optional exception configuration for custom error handling
332
+ * @throws {PredicateError} When predicate returns false
333
+ *
334
+ * @example
335
+ * ```typescript
336
+ * // Simple numeric validation
337
+ * AssertPredicate(42, (x) => x > 0 && x < 100); // ✓ Valid
338
+ *
339
+ * // String validation
340
+ * AssertPredicate("hello", (s) => s.length > 3); // ✓ Valid
341
+ * AssertPredicate("hi", (s) => s.length > 3); // ✗ Throws PredicateError
342
+ *
343
+ * // Complex object validation
344
+ * AssertPredicate(user, (u) =>
345
+ * typeof u.name === 'string' &&
346
+ * u.age >= 0 &&
347
+ * u.email.includes('@')
348
+ * );
349
+ *
350
+ * // Array validation
351
+ * AssertPredicate([1, 2, 3], (arr) =>
352
+ * arr.every(n => typeof n === 'number')
353
+ * );
354
+ * ```
355
+ */
356
+ export function AssertPredicate(value, predicate, exception = {}) {
357
+ SetExceptionClass(exception, PredicateError);
358
+ if (!predicate(value)) {
359
+ SetExceptionMessage(exception, 'Value does not satisfy the predicate condition');
360
+ ThrowException(exception);
361
+ }
362
+ }
363
+ /**
364
+ * Asserts that a value matches a specific type using a type guard function.
365
+ *
366
+ * This method validates that the provided value passes a custom type guard,
367
+ * enabling complex type validations beyond primitive type checks. Type guards
368
+ * are functions that return `value is T` and provide runtime type checking
369
+ * with TypeScript type narrowing. This is particularly useful for validating
370
+ * complex object structures and custom types.
371
+ *
372
+ * @template T - The target type to validate for
373
+ * @template TError - Custom error type to throw on failure
374
+ * @param value - The value to validate
375
+ * @param typeGuard - A TGuard function that returns `value is T`
376
+ * @param exception - Optional exception configuration for custom error handling
377
+ * @throws {TypeGuardError} When value does not pass the type guard
378
+ *
379
+ * @example
380
+ * ```typescript
381
+ * // Define interfaces and type guards
382
+ * interface User { name: string; age: number; }
383
+ * const isUser = (obj: unknown): obj is User =>
384
+ * typeof obj === 'object' && obj !== null &&
385
+ * 'name' in obj && typeof obj.name === 'string' &&
386
+ * 'age' in obj && typeof obj.age === 'number';
387
+ *
388
+ * // Usage
389
+ * AssertIsType(data, isUser);
390
+ * // data is now typed as User
391
+ *
392
+ * // Array of specific type
393
+ * const isStringArray = (arr: unknown): arr is string[] =>
394
+ * Array.isArray(arr) && arr.every(item => typeof item === 'string');
395
+ *
396
+ * AssertIsType(someArray, isStringArray);
397
+ * // someArray is now typed as string[]
398
+ * ```
399
+ */
400
+ export function AssertIsType(value, typeGuard, exception = {}) {
401
+ SetExceptionClass(exception, TypeGuardError);
402
+ if (!typeGuard(value)) {
403
+ const actualType = value === null ? 'null' : value === undefined ? 'undefined' : typeof value;
404
+ SetExceptionMessage(exception, `Expected value to conform to required type but received ${actualType}: ${JSON.stringify(value).slice(0, MAX_VALUE_DISPLAY_LENGTH)}`);
405
+ ThrowException(exception);
406
+ }
407
+ }
408
+ /**
409
+ * Asserts that a value is an instance of a specific class or constructor function.
410
+ *
411
+ * This method validates that the provided value is an instance of the specified
412
+ * constructor function using the instanceof operator. This is useful for validating
413
+ * object instances, built-in types like Date and Error, and custom class instances.
414
+ * After this assertion, the value is properly typed as an instance of the constructor.
415
+ *
416
+ * @template T - The instance type to validate for
417
+ * @template TError - Custom error type to throw on failure
418
+ * @param value - The value to validate as an instance
419
+ * @param constructor - The constructor function to check against
420
+ * @param exception - Optional exception configuration for custom error handling
421
+ * @throws {InstanceOfError} When value is not an instance of the constructor
422
+ *
423
+ * @example
424
+ * ```typescript
425
+ * // Built-in types
426
+ * AssertInstanceOf(new Date(), Date); // ✓ Valid
427
+ * AssertInstanceOf(new Error(), Error); // ✓ Valid
428
+ * AssertInstanceOf([], Array); // ✓ Valid
429
+ * AssertInstanceOf(/regex/, RegExp); // ✓ Valid
430
+ *
431
+ * // Custom classes
432
+ * class Person { constructor(public name: string) {} }
433
+ * const person = new Person("John");
434
+ * AssertInstanceOf(person, Person); // ✓ Valid
435
+ *
436
+ * // Invalid cases
437
+ * AssertInstanceOf("string", Date); // ✗ Throws InstanceOfError
438
+ * AssertInstanceOf(123, Error); // ✗ Throws InstanceOfError
439
+ * AssertInstanceOf({}, Array); // ✗ Throws InstanceOfError
440
+ * ```
441
+ */
442
+ export function AssertInstanceOf(value, constructor, exception = {}) {
443
+ SetExceptionClass(exception, InstanceOfError);
444
+ if (!(value instanceof constructor)) {
445
+ const actualType = value === null ? 'null' : value === undefined ? 'undefined' : value.constructor?.name || typeof value;
446
+ SetExceptionMessage(exception, `Expected instance of ${constructor.name} but received ${actualType}`);
447
+ ThrowException(exception);
448
+ }
449
+ }
450
+ /**
451
+ * Asserts that a value is a function.
452
+ *
453
+ * This method validates that the provided value is a function (typeof === 'function').
454
+ * Useful for runtime validation of callbacks, API hooks, and dynamic invocations.
455
+ * Throws FunctionError or custom error if assertion fails.
456
+ *
457
+ * @param value - The value to validate as a function
458
+ * @param exception - Optional exception configuration for custom error handling
459
+ * @throws {FunctionError} When value is not a function
460
+ *
461
+ * @example
462
+ * AssertFunction(() => {}); // ✓ Valid
463
+ * AssertFunction(function() {}); // ✓ Valid
464
+ * AssertFunction(123); // ✗ Throws FunctionError
465
+ * AssertFunction(null); // ✗ Throws FunctionError
466
+ */
467
+ export function AssertFunction(value, exception = {}) {
468
+ SetExceptionClass(exception, FunctionError);
469
+ if (typeof value !== 'function') {
470
+ const actualType = value === null ? 'null' : value === undefined ? 'undefined' : typeof value;
471
+ SetExceptionMessage(exception, `Expected function but received ${actualType}`);
472
+ ThrowException(exception);
473
+ }
474
+ }
475
+ /**
476
+ * Asserts that a value is a symbol.
477
+ *
478
+ * This method validates that the provided value is a symbol (typeof === 'symbol').
479
+ * Useful for runtime validation of unique keys, metadata, and advanced API contracts.
480
+ * Throws SymbolError or custom error if assertion fails.
481
+ *
482
+ * @param value - The value to validate as a symbol
483
+ * @param exception - Optional exception configuration for custom error handling
484
+ * @throws {SymbolError} When value is not a symbol
485
+ *
486
+ * @example
487
+ * AssertSymbol(Symbol('foo')); // ✓ Valid
488
+ * AssertSymbol(Symbol.iterator); // ✓ Valid
489
+ * AssertSymbol('not-a-symbol'); // ✗ Throws SymbolError
490
+ * AssertSymbol(123); // ✗ Throws SymbolError
491
+ */
492
+ export function AssertSymbol(value, exception = {}) {
493
+ SetExceptionClass(exception, SymbolError);
494
+ if (typeof value !== 'symbol') {
495
+ const actualType = value === null ? 'null' : value === undefined ? 'undefined' : typeof value;
496
+ SetExceptionMessage(exception, `Expected symbol but received ${actualType}`);
497
+ ThrowException(exception);
498
+ }
499
+ }
500
+ /**
501
+ * Asserts that a class extends another class.
502
+ *
503
+ * This method validates that the provided derived class extends the specified base class.
504
+ * Useful for runtime validation of inheritance relationships in TypeScript and JavaScript.
505
+ * Throws ExtendsError or custom error if assertion fails.
506
+ *
507
+ * @param derived - The class to validate as extending base
508
+ * @param base - The base class to check against
509
+ * @param exception - Optional exception configuration for custom error handling
510
+ * @throws {ExtendsError} When derived does not extend base
511
+ *
512
+ * @example
513
+ * class Base {}
514
+ * class Derived extends Base {}
515
+ * AssertExtends(Derived, Base); // ✓ Valid
516
+ * AssertExtends(Base, Derived); // ✗ Throws ExtendsError
517
+ */
518
+ export function AssertExtends(derived, base, exception = {}) {
519
+ SetExceptionClass(exception, ExtendsError);
520
+ if (typeof derived !== 'function' || typeof base !== 'function') {
521
+ SetExceptionMessage(exception, 'Both arguments must be class constructors');
522
+ ThrowException(exception);
523
+ }
524
+ let proto = Object.getPrototypeOf(derived.prototype);
525
+ const baseProto = base.prototype;
526
+ let found = false;
527
+ while (proto) {
528
+ if (proto === baseProto) {
529
+ found = true;
530
+ break;
531
+ }
532
+ proto = Object.getPrototypeOf(proto);
533
+ }
534
+ if (!found) {
535
+ SetExceptionMessage(exception, `Expected ${derived.name} to extend ${base.name}`);
536
+ ThrowException(exception);
537
+ }
538
+ }
539
+ //# sourceMappingURL=generic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generic.js","sourceRoot":"","sources":["../../src/asserts/generic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEpF,gFAAgF;AAChF,MAAM,wBAAwB,GAAG,EAAE,CAAC;AAQpC;;;;;GAKG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IACnC,YAAY,OA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AC3C,IAAI,OAAO,OAAO,KAAK,UAAU,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;QACjE,mBAAmB,CAAC,SAAS,EAAE,2CAA2C,CAAC,CAAC;QAC5E,cAAc,CAAC,SAAS,CAAC,CAAC;IAC3B,CAAC;IACD,IAAI,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACrD,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;IACjC,IAAI,KAAK,GAAG,KAAK,CAAC;IAElB,OAAO,KAAK,EAAE,CAAC;QACd,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACzB,KAAK,GAAG,IAAI,CAAC;YACb,MAAM;QACP,CAAC;QACD,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IACtC,CAAC;IACD,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,mBAAmB,CAAC,SAAS,EAAE,YAAY,OAAO,CAAC,IAAI,cAAc,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAClF,cAAc,CAAC,SAAS,CAAC,CAAC;IAC3B,CAAC;AACF,CAAC"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @fileoverview Main entry point for the asserts package.
3
+ * Provides assertion utilities for various data types including arrays, booleans, numbers, objects, and strings.
4
+ */
5
+ /**
6
+ * Export all array assertion functions and types
7
+ */
8
+ export * from '../array/assert.js';
9
+ /**
10
+ * Export all boolean assertion functions and types
11
+ */
12
+ export * from '../boolean/assert.js';
13
+ /**
14
+ * Export all generic assertion functions and types
15
+ */
16
+ export * from './generic.js';
17
+ /**
18
+ * Export all number assertion functions and types
19
+ */
20
+ export * from '../number/assert.js';
21
+ /**
22
+ * Export all object assertion functions and types
23
+ */
24
+ export * from '../object/assert.js';
25
+ /**
26
+ * Export all string assertion functions and types
27
+ */
28
+ export * from '../string/assert.js';
29
+ /**
30
+ * Export type definitions and interfaces
31
+ */
32
+ export * from './types.js';
33
+ /**
34
+ * Export utility functions for advanced usage
35
+ */
36
+ export * from './utils.js';
37
+ /**
38
+ * Export error classes for standardized error handling
39
+ */
40
+ export * from './errors.js';
41
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/asserts/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,cAAc,oBAAoB,CAAC;AAEnC;;GAEG;AACH,cAAc,sBAAsB,CAAC;AAErC;;GAEG;AACH,cAAc,cAAc,CAAC;AAE7B;;GAEG;AACH,cAAc,qBAAqB,CAAC;AAEpC;;GAEG;AACH,cAAc,qBAAqB,CAAC;AAEpC;;GAEG;AACH,cAAc,qBAAqB,CAAC;AAEpC;;GAEG;AACH,cAAc,YAAY,CAAC;AAE3B;;GAEG;AACH,cAAc,YAAY,CAAC;AAE3B;;GAEG;AACH,cAAc,aAAa,CAAC"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @fileoverview Main entry point for the asserts package.
3
+ * Provides assertion utilities for various data types including arrays, booleans, numbers, objects, and strings.
4
+ */
5
+ /**
6
+ * Export all array assertion functions and types
7
+ */
8
+ export * from '../array/assert.js';
9
+ /**
10
+ * Export all boolean assertion functions and types
11
+ */
12
+ export * from '../boolean/assert.js';
13
+ /**
14
+ * Export all generic assertion functions and types
15
+ */
16
+ export * from './generic.js';
17
+ /**
18
+ * Export all number assertion functions and types
19
+ */
20
+ export * from '../number/assert.js';
21
+ /**
22
+ * Export all object assertion functions and types
23
+ */
24
+ export * from '../object/assert.js';
25
+ /**
26
+ * Export all string assertion functions and types
27
+ */
28
+ export * from '../string/assert.js';
29
+ /**
30
+ * Export type definitions and interfaces
31
+ */
32
+ export * from './types.js';
33
+ /**
34
+ * Export utility functions for advanced usage
35
+ */
36
+ export * from './utils.js';
37
+ /**
38
+ * Export error classes for standardized error handling
39
+ */
40
+ export * from './errors.js';
41
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/asserts/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,cAAc,oBAAoB,CAAC;AAEnC;;GAEG;AACH,cAAc,sBAAsB,CAAC;AAErC;;GAEG;AACH,cAAc,cAAc,CAAC;AAE7B;;GAEG;AACH,cAAc,qBAAqB,CAAC;AAEpC;;GAEG;AACH,cAAc,qBAAqB,CAAC;AAEpC;;GAEG;AACH,cAAc,qBAAqB,CAAC;AAEpC;;GAEG;AACH,cAAc,YAAY,CAAC;AAE3B;;GAEG;AACH,cAAc,YAAY,CAAC;AAE3B;;GAEG;AACH,cAAc,aAAa,CAAC"}