-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr-invalid-arg-type.ts
219 lines (197 loc) · 5.39 KB
/
err-invalid-arg-type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @file Errors - ERR_INVALID_ARG_TYPE
* @module errnode/errors/ERR_INVALID_ARG_TYPE
* @see https://github.com/nodejs/node/blob/v22.7.0/lib/internal/errors.js#L1373-L1443
*/
import E from '#e'
import { codes } from '#src/enums'
import type { NodeError, NodeErrorConstructor } from '#src/interfaces'
import { determineSpecificType } from '#src/utils'
import formatList from '#src/utils/format-list'
import { DOT } from '@flex-development/tutils'
import { ok } from 'devlop'
/**
* `ERR_INVALID_ARG_TYPE` schema.
*
* @see {@linkcode NodeError}
* @see https://nodejs.org/api/errors.html#err_invalid_arg_type
*
* @extends {NodeError<codes.ERR_INVALID_ARG_TYPE>}
* @extends {TypeError}
*/
interface ErrInvalidArgType
extends NodeError<codes.ERR_INVALID_ARG_TYPE>, TypeError {}
/**
* `ERR_INVALID_ARG_TYPE` message arguments.
*/
type Args = [
name: string,
expected: string | readonly string[],
actual: unknown
]
/**
* `ERR_INVALID_ARG_TYPE` constructor.
*
* @see {@linkcode Args}
* @see {@linkcode ErrInvalidArgType}
* @see {@linkcode NodeErrorConstructor}
*
* @extends {NodeErrorConstructor<ErrInvalidArgType,Args>}
*/
interface ErrInvalidArgTypeConstructor
extends NodeErrorConstructor<ErrInvalidArgType, Args> {
/**
* Create a new `ERR_INVALID_ARG_TYPE` error.
*
* @see {@linkcode ErrInvalidArgType}
*
* @param {string} name
* Name of invalid argument or property
* @param {ReadonlyArray<string> | string} expected
* Expected type(s)
* @param {unknown} actual
* Value supplied by user
* @return {ErrInvalidArgType}
*/
new (
name: string,
expected: string | readonly string[],
actual: unknown
): ErrInvalidArgType
}
/**
* `ERR_INVALID_ARG_TYPE` model.
*
* Thrown when an argument of the wrong type is passed to a Node.js API.
*
* @see {@linkcode ErrInvalidArgTypeConstructor}
*
* @type {ErrInvalidArgTypeConstructor}
*
* @class
*/
const ERR_INVALID_ARG_TYPE: ErrInvalidArgTypeConstructor = E(
codes.ERR_INVALID_ARG_TYPE,
TypeError,
/**
* @param {string} name
* Name of invalid argument or property
* @param {ReadonlyArray<string> | string} expected
* Expected type(s)
* @param {unknown} actual
* Value supplied by user
* @return {string}
* Error message
*/
function message(
name: string,
expected: string | readonly string[],
actual: unknown
): string {
ok(typeof name === 'string', '\'name\' must be a string')
if (typeof expected === 'string') expected = [expected]
/**
* Primitive value names.
*
* Sorted by a rough estimate on most frequently used entries.
*
* @const {Set<string>} kTypes
*/
const kTypes: Set<string> = new Set([
'string',
'function',
'number',
'object',
'Function',
'Object',
'boolean',
'bigint',
'symbol'
])
/**
* Error message.
*
* @var {string} message
*/
let message: string = 'The '
// stylize invalid argument name
message += name.endsWith(' argument')
? name
: `'${name}' ${name.includes(DOT) ? 'property' : 'argument'}`
// continue building error message
message += ' must be '
/**
* List of expected class instances.
*
* @const {string[]} instances
*/
const instances: string[] = []
/**
* List of other expected types.
*
* @const {string[]} other
*/
const other: string[] = []
/**
* List of expected primitive types.
*
* @const {string[]} types
*/
const types: string[] = []
// get expected types
for (const value of expected) {
ok(typeof value === 'string', 'values in `expected` must be strings')
if (kTypes.has(value)) {
types.push(value.toLowerCase())
} else if (/^([A-Z][\da-z]*)+$/.exec(value)) {
instances.push(value)
} else {
ok(value !== 'object', '"object" should be written as "Object"')
other.push(value)
}
}
// special case: handle `object` in case other instances are allowed to
// outline the differences between each other
if (instances.length) {
/**
* Position of `'object'` in {@linkcode types}.
*
* @const {number} position
*/
const position: number = types.indexOf('object')
// replace 'object' in types with 'Object' in instances
if (position !== -1) {
types.splice(position, 1)
instances.push('Object')
}
}
// add expected primitive types to error message
if (types.length) {
message += `${types.length > 1 ? 'one ' : ''}of type `
message += formatList(types, 'or')
if (instances.length || other.length) message += ' or '
}
// add expected class instances to error message
if (instances.length) {
message += 'an instance of ' + formatList(instances, 'or')
if (other.length) message += ' or '
}
// add other expected types to error message
if (other.length) {
if (other.length > 1) {
message += `one of ${formatList(other, 'or')}`
} else {
/* c8 ignore next */
if (other[0]!.toLowerCase() !== other[0]) message += 'an '
message += `${other[0]}`
}
}
return `${message}. Received ${determineSpecificType(actual)}`
}
)
export {
ERR_INVALID_ARG_TYPE as default,
type ErrInvalidArgType,
type Args as ErrInvalidArgTypeArgs,
type ErrInvalidArgTypeConstructor
}