@produck/type-error 0.1.1 → 0.1.2
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 +139 -0
- package/index.d.ts +52 -4
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# @produck/type-error
|
|
2
|
+
|
|
3
|
+
A utility library for generating and throwing TypeError messages following the
|
|
4
|
+
Produck TypeError template format.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @produck/type-error
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## API
|
|
13
|
+
|
|
14
|
+
### `ErrorMessage(role: string, expected: string): string`
|
|
15
|
+
|
|
16
|
+
Generates a formatted error message for type validation failures.
|
|
17
|
+
|
|
18
|
+
**Parameters:**
|
|
19
|
+
- `role` - Description of the parameter role (e.g., `"args[0]"`, `"options.name"`)
|
|
20
|
+
- `expected` - Expected type or value description (e.g., `"string"`, `"function"`)
|
|
21
|
+
|
|
22
|
+
**Returns:** Formatted error message string
|
|
23
|
+
|
|
24
|
+
**Throws:** `TypeError` if role or expected is not a string
|
|
25
|
+
|
|
26
|
+
**Example:**
|
|
27
|
+
```javascript
|
|
28
|
+
import { ErrorMessage } from '@produck/type-error';
|
|
29
|
+
|
|
30
|
+
const message = ErrorMessage('args[0]', 'string');
|
|
31
|
+
// Returns: Invalid "args[0]", one "string" expected.
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### `ThrowTypeError(role: string, expected: string): never`
|
|
35
|
+
|
|
36
|
+
Throws a TypeError with a formatted message.
|
|
37
|
+
|
|
38
|
+
**Parameters:**
|
|
39
|
+
- `role` - Description of the parameter role
|
|
40
|
+
- `expected` - Expected type or value description
|
|
41
|
+
|
|
42
|
+
**Throws:** `TypeError` always with formatted message
|
|
43
|
+
|
|
44
|
+
**Example:**
|
|
45
|
+
```javascript
|
|
46
|
+
import { ThrowTypeError } from '@produck/type-error';
|
|
47
|
+
|
|
48
|
+
function myFunction(value) {
|
|
49
|
+
if (typeof value !== 'string') {
|
|
50
|
+
ThrowTypeError('args[0]', 'string');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### `AssertionChecker(validate: Validate, defaultExpected: string): Assert`
|
|
56
|
+
|
|
57
|
+
Creates a reusable assertion function with a custom validation function.
|
|
58
|
+
|
|
59
|
+
**Parameters:**
|
|
60
|
+
- `validate` - Function that returns `true` if value is valid, `false` otherwise
|
|
61
|
+
- `defaultExpected` - Default "expected" message used in error messages
|
|
62
|
+
|
|
63
|
+
**Returns:** An assertion function `(value: unknown, role: string, expected?: string) => undefined`
|
|
64
|
+
|
|
65
|
+
**Throws:** `TypeError` if validate is not a function or defaultExpected is not
|
|
66
|
+
a string
|
|
67
|
+
|
|
68
|
+
**Example:**
|
|
69
|
+
```javascript
|
|
70
|
+
import { AssertionChecker } from '@produck/type-error';
|
|
71
|
+
|
|
72
|
+
const assertString = AssertionChecker(
|
|
73
|
+
(value) => typeof value === 'string',
|
|
74
|
+
'string'
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// Use the assertion
|
|
78
|
+
try {
|
|
79
|
+
assertString(42, 'args[0]');
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error(error.message);
|
|
82
|
+
// TypeError: Invalid "args[0]", one "string" expected.
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// With custom expected message
|
|
86
|
+
try {
|
|
87
|
+
assertString(null, 'args[0]', 'non-null string');
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error(error.message);
|
|
90
|
+
// TypeError: Invalid "args[0]", one "non-null string" expected.
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Usage Patterns
|
|
95
|
+
|
|
96
|
+
### Basic Validation
|
|
97
|
+
|
|
98
|
+
```javascript
|
|
99
|
+
import { ThrowTypeError } from '@produck/type-error';
|
|
100
|
+
|
|
101
|
+
function processData(data) {
|
|
102
|
+
if (typeof data !== 'object' || data === null) {
|
|
103
|
+
ThrowTypeError('args[0]', 'object');
|
|
104
|
+
}
|
|
105
|
+
// ...
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Creating Custom Validators
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
import { AssertionChecker } from '@produck/type-error';
|
|
113
|
+
|
|
114
|
+
const assertNumber = AssertionChecker(
|
|
115
|
+
(v) => typeof v === 'number',
|
|
116
|
+
'number'
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const assertArray = AssertionChecker(
|
|
120
|
+
(v) => Array.isArray(v),
|
|
121
|
+
'array'
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
function calculateSum(numbers) {
|
|
125
|
+
assertArray(numbers, 'args[0]');
|
|
126
|
+
return numbers.reduce((sum, n) => {
|
|
127
|
+
assertNumber(n, 'array element');
|
|
128
|
+
return sum + n;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
MIT
|
|
136
|
+
|
|
137
|
+
## Author
|
|
138
|
+
|
|
139
|
+
ChaosLee
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Assertion function that validates a value and throws TypeError if invalid
|
|
3
|
+
* @param value - The value to validate
|
|
4
|
+
* @param role - Description of the parameter role (e.g., "args[0]")
|
|
5
|
+
* @param expected - Expected type/value description (optional, uses default)
|
|
6
|
+
*/
|
|
7
|
+
export type Assert = (
|
|
8
|
+
value: unknown,
|
|
9
|
+
role: string,
|
|
10
|
+
expected?: string
|
|
11
|
+
) => undefined;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Validation function that checks if a value meets criteria
|
|
15
|
+
* @param value - The value to validate
|
|
16
|
+
* @returns true if valid, false otherwise
|
|
17
|
+
*/
|
|
2
18
|
export type Validate = (value: unknown) => boolean;
|
|
3
19
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Generates a formatted error message for type validation failures
|
|
22
|
+
* @param role - Description of the parameter role
|
|
23
|
+
* @param expected - Expected type/value description
|
|
24
|
+
* @returns Formatted error message string
|
|
25
|
+
* @throws TypeError when role or expected is not a string
|
|
26
|
+
*/
|
|
27
|
+
export function ErrorMessage(
|
|
28
|
+
role: string,
|
|
29
|
+
expected: string
|
|
30
|
+
): string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Throws a TypeError with a formatted message for type validation failures
|
|
34
|
+
* @param role - Description of the parameter role
|
|
35
|
+
* @param expected - Expected type/value description
|
|
36
|
+
* @throws TypeError always throws with formatted message
|
|
37
|
+
*/
|
|
38
|
+
export function ThrowTypeError(
|
|
39
|
+
role: string,
|
|
40
|
+
expected: string
|
|
41
|
+
): never;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Creates an assertion checker function with a custom validation function
|
|
45
|
+
* @param validate - Function that returns true if value is valid
|
|
46
|
+
* @param defaultExpected - Default "expected" message for the assertion
|
|
47
|
+
* @returns Assertion function that validates values
|
|
48
|
+
* @throws TypeError when validate is not a function or
|
|
49
|
+
* defaultExpected is not a string
|
|
50
|
+
*/
|
|
51
|
+
export function AssertionChecker(
|
|
52
|
+
validate: Validate,
|
|
53
|
+
defaultExpected: string
|
|
54
|
+
): Assert;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@produck/type-error",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ChaosLee",
|
|
@@ -24,5 +24,5 @@
|
|
|
24
24
|
"test": "node ./test/index.mjs",
|
|
25
25
|
"test:only": "node --test-only ./test/index.mjs"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "e643e102546b9afc6dc47e1dd38fbb9e8157b86e"
|
|
28
28
|
}
|