@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.
- 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 +1 -1
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { IAssertException } from '../asserts/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a value is not a valid object or fails an object assertion.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* throw new ObjectError('Value is not a valid object');
|
|
7
|
+
*/
|
|
8
|
+
export declare class ObjectError extends Error {
|
|
9
|
+
constructor(message?: string);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when an object is missing a required property or a property fails an assertion.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* throw new PropertyError('Object is missing required property');
|
|
16
|
+
*/
|
|
17
|
+
export declare class ObjectPropertyError extends Error {
|
|
18
|
+
constructor(message?: string);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Asserts that a value is a plain object (not null, not an array, not a function).
|
|
22
|
+
*
|
|
23
|
+
* This method validates that the provided value is an object type, excluding null,
|
|
24
|
+
* arrays, and functions which are technically objects in JavaScript but not plain
|
|
25
|
+
* objects. After this assertion, the value is typed as Record<string, unknown>
|
|
26
|
+
* for safe property access.
|
|
27
|
+
*
|
|
28
|
+
* @template TError - Custom error type to throw on failure
|
|
29
|
+
* @param value - The value to validate as an object
|
|
30
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
31
|
+
* @throws {ObjectError} When value is not a plain object
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* AssertObject({ name: "John" }); // ✓ Valid (plain object)
|
|
36
|
+
* AssertObject({}); // ✓ Valid (empty object)
|
|
37
|
+
* AssertObject(new Date()); // ✓ Valid (object instance)
|
|
38
|
+
* AssertObject([1, 2, 3]); // ✗ Throws ObjectError (array)
|
|
39
|
+
* AssertObject(null); // ✗ Throws ObjectError (null)
|
|
40
|
+
* AssertObject("string"); // ✗ Throws ObjectError (primitive)
|
|
41
|
+
* AssertObject(() => {}); // ✗ Throws ObjectError (function)
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function AssertObject(value: unknown, exception?: IAssertException): asserts value is Record<string, unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* Asserts that an object has a specific property (inherited or own).
|
|
47
|
+
*
|
|
48
|
+
* This method validates that the specified property exists in the object,
|
|
49
|
+
* including properties from the prototype chain. Uses the 'in' operator
|
|
50
|
+
* for property detection. This is useful for checking if an object conforms
|
|
51
|
+
* to an expected interface or has required properties.
|
|
52
|
+
*
|
|
53
|
+
* @template T - The object type
|
|
54
|
+
* @template K - The property key type
|
|
55
|
+
* @template TError - Custom error type to throw on failure
|
|
56
|
+
* @param value - The object to check for the property
|
|
57
|
+
* @param property - The property key to check for
|
|
58
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
59
|
+
* @throws {PropertyError} When object does not have the specified property
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const obj = { name: "John", age: 30 };
|
|
64
|
+
* AssertHasProperty(obj, "name"); // ✓ Valid (own property)
|
|
65
|
+
* AssertHasProperty(obj, "toString"); // ✓ Valid (inherited property)
|
|
66
|
+
* AssertHasProperty(obj, "invalid"); // ✗ Throws PropertyError
|
|
67
|
+
*
|
|
68
|
+
* // Type narrowing with property presence
|
|
69
|
+
* function processUser(obj: unknown) {
|
|
70
|
+
* AssertObject(obj);
|
|
71
|
+
* AssertHasProperty(obj, "name");
|
|
72
|
+
* // obj["name"] is now accessible safely
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function AssertObjectHasProperty<T extends object, K extends PropertyKey>(value: T, property: K, exception?: IAssertException): void;
|
|
77
|
+
/**
|
|
78
|
+
* Asserts that an object has a specific own property (not inherited).
|
|
79
|
+
*
|
|
80
|
+
* This method validates that the specified property exists as an own property
|
|
81
|
+
* of the object, excluding properties from the prototype chain. Uses
|
|
82
|
+
* Object.prototype.hasOwnProperty.call() for reliable detection. This is useful
|
|
83
|
+
* when you need to ensure a property is directly defined on the object.
|
|
84
|
+
*
|
|
85
|
+
* @template T - The object type
|
|
86
|
+
* @template K - The property key type
|
|
87
|
+
* @template TError - Custom error type to throw on failure
|
|
88
|
+
* @param value - The object to check for the own property
|
|
89
|
+
* @param property - The property key to check for
|
|
90
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
91
|
+
* @throws {PropertyError} When object does not have the specified own property
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const obj = { name: "John" };
|
|
96
|
+
* AssertHasOwnProperty(obj, "name"); // ✓ Valid (own property)
|
|
97
|
+
* AssertHasOwnProperty(obj, "toString"); // ✗ Throws (inherited property)
|
|
98
|
+
* AssertHasOwnProperty(obj, "invalid"); // ✗ Throws (doesn't exist)
|
|
99
|
+
*
|
|
100
|
+
* // Checking for data vs inherited methods
|
|
101
|
+
* function validateUserData(obj: object) {
|
|
102
|
+
* AssertHasOwnProperty(obj, "id"); // Must be own property
|
|
103
|
+
* AssertHasOwnProperty(obj, "name"); // Must be own property
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function AssertObjectHasOwnProperty<T extends object, K extends PropertyKey>(value: T, property: K, exception?: IAssertException): void;
|
|
108
|
+
/**
|
|
109
|
+
* Asserts that a specific property of an object is not null or undefined.
|
|
110
|
+
*
|
|
111
|
+
* This method validates that the specified property exists and has a non-nullish value.
|
|
112
|
+
* It combines property existence checking with null/undefined validation. Useful for
|
|
113
|
+
* validating that required object properties have been properly initialized.
|
|
114
|
+
*
|
|
115
|
+
* @template T - The object type
|
|
116
|
+
* @template K - The property key type (must be a key of T)
|
|
117
|
+
* @template TError - Custom error type to throw on failure
|
|
118
|
+
* @param value - The object to check
|
|
119
|
+
* @param property - The property key to validate for non-null value
|
|
120
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
121
|
+
* @throws {PropertyError} When property is null, undefined, or doesn't exist
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const user = { id: 123, name: "John", email: null };
|
|
126
|
+
* AssertPropertyNotNull(user, "id"); // ✓ Valid (123 is not null/undefined)
|
|
127
|
+
* AssertPropertyNotNull(user, "name"); // ✓ Valid ("John" is not null/undefined)
|
|
128
|
+
* AssertPropertyNotNull(user, "email"); // ✗ Throws PropertyError (null)
|
|
129
|
+
*
|
|
130
|
+
* // Type narrowing for property values
|
|
131
|
+
* function processUser(user: { name?: string | null }) {
|
|
132
|
+
* AssertPropertyNotNull(user, "name");
|
|
133
|
+
* // user.name is now typed as string (null/undefined excluded)
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export declare function AssertObjectPropertyNotNull<T extends object, K extends keyof T>(value: T, property: K, exception?: IAssertException): void;
|
|
138
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/object/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAMvD;;;;;GAKG;AACH,qBAAa,WAAY,SAAQ,KAAK;gBACzB,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;;;GAKG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;gBACjC,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,GAAE,gBAAqB,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAyBvH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,WAAW,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,GAAE,gBAAqB,GAAG,IAAI,CAU9I;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,WAAW,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,GAAE,gBAAqB,GAAG,IAAI,CAUjJ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,2BAA2B,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,SAAS,GAAE,gBAAqB,GAAG,IAAI,CAW9I"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { SetExceptionClass, SetExceptionMessage, ThrowException } from '../asserts/utils.js';
|
|
2
|
+
/** Maximum number of characters to include from a value in an error message. */
|
|
3
|
+
const MAX_VALUE_DISPLAY_LENGTH = 100;
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when a value is not a valid object or fails an object assertion.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* throw new ObjectError('Value is not a valid object');
|
|
9
|
+
*/
|
|
10
|
+
export class ObjectError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message ?? 'Object assertion failed');
|
|
13
|
+
this.name = 'ObjectError';
|
|
14
|
+
Object.setPrototypeOf(this, ObjectError.prototype);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Error thrown when an object is missing a required property or a property fails an assertion.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* throw new PropertyError('Object is missing required property');
|
|
22
|
+
*/
|
|
23
|
+
export class ObjectPropertyError extends Error {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message ?? 'Object Property Assertion Failed');
|
|
26
|
+
this.name = 'ObjectPropertyError';
|
|
27
|
+
Object.setPrototypeOf(this, ObjectPropertyError.prototype);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Asserts that a value is a plain object (not null, not an array, not a function).
|
|
32
|
+
*
|
|
33
|
+
* This method validates that the provided value is an object type, excluding null,
|
|
34
|
+
* arrays, and functions which are technically objects in JavaScript but not plain
|
|
35
|
+
* objects. After this assertion, the value is typed as Record<string, unknown>
|
|
36
|
+
* for safe property access.
|
|
37
|
+
*
|
|
38
|
+
* @template TError - Custom error type to throw on failure
|
|
39
|
+
* @param value - The value to validate as an object
|
|
40
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
41
|
+
* @throws {ObjectError} When value is not a plain object
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* AssertObject({ name: "John" }); // ✓ Valid (plain object)
|
|
46
|
+
* AssertObject({}); // ✓ Valid (empty object)
|
|
47
|
+
* AssertObject(new Date()); // ✓ Valid (object instance)
|
|
48
|
+
* AssertObject([1, 2, 3]); // ✗ Throws ObjectError (array)
|
|
49
|
+
* AssertObject(null); // ✗ Throws ObjectError (null)
|
|
50
|
+
* AssertObject("string"); // ✗ Throws ObjectError (primitive)
|
|
51
|
+
* AssertObject(() => {}); // ✗ Throws ObjectError (function)
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function AssertObject(value, exception = {}) {
|
|
55
|
+
SetExceptionClass(exception, ObjectError);
|
|
56
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value) || typeof value === 'function') {
|
|
57
|
+
const actualType = value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value;
|
|
58
|
+
let valueStr;
|
|
59
|
+
try {
|
|
60
|
+
if (typeof value === 'function') {
|
|
61
|
+
valueStr = '[Function]';
|
|
62
|
+
}
|
|
63
|
+
else if (value === null) {
|
|
64
|
+
valueStr = 'null';
|
|
65
|
+
}
|
|
66
|
+
else if (value === undefined) {
|
|
67
|
+
valueStr = 'undefined';
|
|
68
|
+
}
|
|
69
|
+
else if (typeof value === 'symbol') {
|
|
70
|
+
valueStr = `[Symbol: ${String(value)}]`;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
valueStr = JSON.stringify(value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
valueStr = String(value);
|
|
78
|
+
}
|
|
79
|
+
SetExceptionMessage(exception, `Expected object but received ${actualType}: ${valueStr.slice(0, MAX_VALUE_DISPLAY_LENGTH)}`);
|
|
80
|
+
ThrowException(exception);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Asserts that an object has a specific property (inherited or own).
|
|
85
|
+
*
|
|
86
|
+
* This method validates that the specified property exists in the object,
|
|
87
|
+
* including properties from the prototype chain. Uses the 'in' operator
|
|
88
|
+
* for property detection. This is useful for checking if an object conforms
|
|
89
|
+
* to an expected interface or has required properties.
|
|
90
|
+
*
|
|
91
|
+
* @template T - The object type
|
|
92
|
+
* @template K - The property key type
|
|
93
|
+
* @template TError - Custom error type to throw on failure
|
|
94
|
+
* @param value - The object to check for the property
|
|
95
|
+
* @param property - The property key to check for
|
|
96
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
97
|
+
* @throws {PropertyError} When object does not have the specified property
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const obj = { name: "John", age: 30 };
|
|
102
|
+
* AssertHasProperty(obj, "name"); // ✓ Valid (own property)
|
|
103
|
+
* AssertHasProperty(obj, "toString"); // ✓ Valid (inherited property)
|
|
104
|
+
* AssertHasProperty(obj, "invalid"); // ✗ Throws PropertyError
|
|
105
|
+
*
|
|
106
|
+
* // Type narrowing with property presence
|
|
107
|
+
* function processUser(obj: unknown) {
|
|
108
|
+
* AssertObject(obj);
|
|
109
|
+
* AssertHasProperty(obj, "name");
|
|
110
|
+
* // obj["name"] is now accessible safely
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export function AssertObjectHasProperty(value, property, exception = {}) {
|
|
115
|
+
// First check if value is an object, using ObjectError
|
|
116
|
+
AssertObject(value, { class: ObjectError });
|
|
117
|
+
// Then check for property existence, using the configured exception class or default
|
|
118
|
+
SetExceptionClass(exception, ObjectPropertyError);
|
|
119
|
+
if (!(property in value)) {
|
|
120
|
+
SetExceptionMessage(exception, `Expected object to have property '${String(property)}' but property was not found`);
|
|
121
|
+
ThrowException(exception);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Asserts that an object has a specific own property (not inherited).
|
|
126
|
+
*
|
|
127
|
+
* This method validates that the specified property exists as an own property
|
|
128
|
+
* of the object, excluding properties from the prototype chain. Uses
|
|
129
|
+
* Object.prototype.hasOwnProperty.call() for reliable detection. This is useful
|
|
130
|
+
* when you need to ensure a property is directly defined on the object.
|
|
131
|
+
*
|
|
132
|
+
* @template T - The object type
|
|
133
|
+
* @template K - The property key type
|
|
134
|
+
* @template TError - Custom error type to throw on failure
|
|
135
|
+
* @param value - The object to check for the own property
|
|
136
|
+
* @param property - The property key to check for
|
|
137
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
138
|
+
* @throws {PropertyError} When object does not have the specified own property
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const obj = { name: "John" };
|
|
143
|
+
* AssertHasOwnProperty(obj, "name"); // ✓ Valid (own property)
|
|
144
|
+
* AssertHasOwnProperty(obj, "toString"); // ✗ Throws (inherited property)
|
|
145
|
+
* AssertHasOwnProperty(obj, "invalid"); // ✗ Throws (doesn't exist)
|
|
146
|
+
*
|
|
147
|
+
* // Checking for data vs inherited methods
|
|
148
|
+
* function validateUserData(obj: object) {
|
|
149
|
+
* AssertHasOwnProperty(obj, "id"); // Must be own property
|
|
150
|
+
* AssertHasOwnProperty(obj, "name"); // Must be own property
|
|
151
|
+
* }
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export function AssertObjectHasOwnProperty(value, property, exception = {}) {
|
|
155
|
+
// First check if value is an object, using ObjectError
|
|
156
|
+
AssertObject(value, { class: ObjectError });
|
|
157
|
+
// Then check for own property existence, using the configured exception class or default
|
|
158
|
+
SetExceptionClass(exception, ObjectPropertyError);
|
|
159
|
+
if (!Object.prototype.hasOwnProperty.call(value, property)) {
|
|
160
|
+
SetExceptionMessage(exception, `Expected object to have own property '${String(property)}' but property was not found`);
|
|
161
|
+
ThrowException(exception);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Asserts that a specific property of an object is not null or undefined.
|
|
166
|
+
*
|
|
167
|
+
* This method validates that the specified property exists and has a non-nullish value.
|
|
168
|
+
* It combines property existence checking with null/undefined validation. Useful for
|
|
169
|
+
* validating that required object properties have been properly initialized.
|
|
170
|
+
*
|
|
171
|
+
* @template T - The object type
|
|
172
|
+
* @template K - The property key type (must be a key of T)
|
|
173
|
+
* @template TError - Custom error type to throw on failure
|
|
174
|
+
* @param value - The object to check
|
|
175
|
+
* @param property - The property key to validate for non-null value
|
|
176
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
177
|
+
* @throws {PropertyError} When property is null, undefined, or doesn't exist
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const user = { id: 123, name: "John", email: null };
|
|
182
|
+
* AssertPropertyNotNull(user, "id"); // ✓ Valid (123 is not null/undefined)
|
|
183
|
+
* AssertPropertyNotNull(user, "name"); // ✓ Valid ("John" is not null/undefined)
|
|
184
|
+
* AssertPropertyNotNull(user, "email"); // ✗ Throws PropertyError (null)
|
|
185
|
+
*
|
|
186
|
+
* // Type narrowing for property values
|
|
187
|
+
* function processUser(user: { name?: string | null }) {
|
|
188
|
+
* AssertPropertyNotNull(user, "name");
|
|
189
|
+
* // user.name is now typed as string (null/undefined excluded)
|
|
190
|
+
* }
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
export function AssertObjectPropertyNotNull(value, property, exception = {}) {
|
|
194
|
+
// First check if value is an object, using ObjectError
|
|
195
|
+
AssertObject(value, { class: ObjectError });
|
|
196
|
+
// Then check for property value, using the configured exception class or default
|
|
197
|
+
SetExceptionClass(exception, ObjectPropertyError);
|
|
198
|
+
if (value[property] === null || value[property] === undefined) {
|
|
199
|
+
const actualValue = value[property] === null ? 'null' : 'undefined';
|
|
200
|
+
SetExceptionMessage(exception, `Expected property '${String(property)}' to be non-null/non-undefined but received ${actualValue}`);
|
|
201
|
+
ThrowException(exception);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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ropertyKeySafe, isPropertyPathSafe, sanitizePropertyKey, filterDange
|
|
|
26
26
|
export { ObjectInvert } from './object-invert.js';
|
|
27
27
|
export { ObjectFlatten } from './object-flatten.js';
|
|
28
28
|
export { ObjectDiff } from './object-diff.js';
|
|
29
|
+
export { ObjectError, ObjectPropertyError, AssertObjectHasProperty, AssertObjectHasOwnProperty, AssertObjectPropertyNotNull } from './assert.js';
|
|
29
30
|
export type { IObjectDiffResult } from './object-diff.js';
|
|
30
31
|
export type { TConstructableObject, TObjectOmitExtraProperties, TCachedObjectFilterFunction, TCachedObjectMapperFunction, TObjectPredicate, TObjectTransformer, TObjectComparator, TObjectEqualityComparator, TPropertyMapper, TPropertyFilter } from './types.js';
|
|
31
32
|
export type { IObjectFilterOptions, ICachedObjectFilterOptions, ICachedObjectMapOptions } from './types.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,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ePropertyKey, filterDange
|
|
|
27
27
|
export { ObjectInvert } from './object-invert.js';
|
|
28
28
|
export { ObjectFlatten } from './object-flatten.js';
|
|
29
29
|
export { ObjectDiff } from './object-diff.js';
|
|
30
|
+
export { ObjectError, ObjectPropertyError, AssertObjectHasProperty, AssertObjectHasOwnProperty, AssertObjectPropertyNotNull } from './assert.js';
|
|
30
31
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,YAAY;AACZ,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AACtF,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,m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,EAAE,MAAM,qBAAqB,CAAC;AACpK,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { IAssertException } from '../asserts/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a value is not a valid string or fails a string assertion.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* throw new StringError('Value is not a valid string');
|
|
7
|
+
*/
|
|
8
|
+
export declare class StringError extends Error {
|
|
9
|
+
constructor(message?: string);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Asserts that a value is a string primitive type.
|
|
13
|
+
*
|
|
14
|
+
* This method validates that the provided value is of type 'string'. It accepts
|
|
15
|
+
* any string including empty strings. This is a strict type check that will
|
|
16
|
+
* reject string objects created with new String().
|
|
17
|
+
*
|
|
18
|
+
* @template TError - Custom error type to throw on failure
|
|
19
|
+
* @param value - The value to validate as a string
|
|
20
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
21
|
+
* @throws {StringError} When value is not a string primitive
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* AssertString("hello"); // ✓ Valid
|
|
26
|
+
* AssertString(""); // ✓ Valid (empty string is still a string)
|
|
27
|
+
* AssertString("123"); // ✓ Valid (numeric string)
|
|
28
|
+
* AssertString(123); // ✗ Throws StringError (number)
|
|
29
|
+
* AssertString(null); // ✗ Throws StringError
|
|
30
|
+
* AssertString(new String("hello")); // ✗ Throws StringError (String object)
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function AssertString(value: unknown, exception?: IAssertException): asserts value is string;
|
|
34
|
+
/**
|
|
35
|
+
* Asserts that a value is a non-empty string (after trimming whitespace).
|
|
36
|
+
*
|
|
37
|
+
* This method validates that the provided value is a string and contains at least
|
|
38
|
+
* one non-whitespace character after trimming. This is useful for validating user
|
|
39
|
+
* inputs, form fields, and API parameters where empty or whitespace-only strings
|
|
40
|
+
* are not acceptable.
|
|
41
|
+
*
|
|
42
|
+
* @template TError - Custom error type to throw on failure
|
|
43
|
+
* @param value - The value to validate as a non-empty string
|
|
44
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
45
|
+
* @throws {StringError} When value is not a string or is empty/whitespace-only
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* AssertStringNotEmpty("hello"); // ✓ Valid
|
|
50
|
+
* AssertStringNotEmpty(" a "); // ✓ Valid (has non-whitespace content)
|
|
51
|
+
* AssertStringNotEmpty("x"); // ✓ Valid (single character)
|
|
52
|
+
* AssertStringNotEmpty(""); // ✗ Throws StringError (empty)
|
|
53
|
+
* AssertStringNotEmpty(" "); // ✗ Throws StringError (whitespace only)
|
|
54
|
+
* AssertStringNotEmpty("\t\n "); // ✗ Throws StringError (whitespace only)
|
|
55
|
+
* AssertStringNotEmpty(123); // ✗ Throws StringError (not a string)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function AssertStringNotEmpty(value: unknown, exception?: IAssertException): asserts value is string;
|
|
59
|
+
/**
|
|
60
|
+
* Asserts that a string matches a regular expression pattern.
|
|
61
|
+
*
|
|
62
|
+
* This method validates that the provided string matches the specified regular
|
|
63
|
+
* expression pattern. The string must be a valid string type (use AssertString first
|
|
64
|
+
* if needed). Useful for validating formats like emails, phone numbers, IDs, and
|
|
65
|
+
* other structured text data.
|
|
66
|
+
*
|
|
67
|
+
* For performance optimization, regex patterns are cached when possible to avoid
|
|
68
|
+
* recompilation of the same patterns. This provides significant performance benefits
|
|
69
|
+
* when validating many values against the same pattern.
|
|
70
|
+
*
|
|
71
|
+
* @template TError - Custom error type to throw on failure
|
|
72
|
+
* @param value - The string to test against the pattern
|
|
73
|
+
* @param regex - The regular expression pattern to match against
|
|
74
|
+
* @param exception - Optional exception configuration for custom error handling
|
|
75
|
+
* @throws {StringError} When string does not match the pattern
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* // Email validation
|
|
80
|
+
* AssertStringMatches("hello@example.com", /^[^\s@]+@[^\s@]+\.[^\s@]+$/); // ✓ Valid
|
|
81
|
+
*
|
|
82
|
+
* // Digits only validation
|
|
83
|
+
* AssertStringMatches("123", /^\d+$/); // ✓ Valid
|
|
84
|
+
* AssertStringMatches("12a", /^\d+$/); // ✗ Throws StringError
|
|
85
|
+
*
|
|
86
|
+
* // Phone number format
|
|
87
|
+
* AssertStringMatches("(555) 123-4567", /^\(\d{3}\) \d{3}-\d{4}$/); // ✓ Valid
|
|
88
|
+
*
|
|
89
|
+
* // Alphanumeric with length constraint
|
|
90
|
+
* AssertStringMatches("abc123", /^[a-zA-Z0-9]{3,10}$/); // ✓ Valid
|
|
91
|
+
* AssertStringMatches("ab", /^[a-zA-Z0-9]{3,10}$/); // ✗ Throws (too short)
|
|
92
|
+
*
|
|
93
|
+
* // Performance benefit with repeated pattern usage
|
|
94
|
+
* const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
95
|
+
* AssertStringMatches("user1@example.com", emailPattern); // Cached for future use
|
|
96
|
+
* AssertStringMatches("user2@example.com", emailPattern); // Uses cached pattern
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function AssertStringMatches(value: string, regex: RegExp, exception?: IAssertException): void;
|
|
100
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/string/assert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAqD5D;;;;;GAKG;AACH,qBAAa,WAAY,SAAQ,KAAK;gBACzB,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,GAAE,gBAAqB,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAMtG;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,GAAE,gBAAqB,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAe9G;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,GAAE,gBAAqB,GAAG,IAAI,CAkBxG"}
|