forked from yargs/argsert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (141 loc) · 3.66 KB
/
index.js
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
const REQUIRED = 'required';
const OPTIONAL = 'optional';
export default function argsert(typeConfig, ...args) {
if (
typeof typeConfig !== 'string' ||
(!isRequired(typeConfig) && !isOptional(typeConfig))
) {
args = [typeConfig, ...args];
typeConfig = this || '';
}
const types = getTypes(typeConfig);
const configuredKeys = Object.keys(types);
const numRequired = configuredKeys.filter(k => REQUIRED in types[k]).length;
args = compactArgs(args);
if (args.length < numRequired) {
throw new Error(
`Not enough arguments provided. Expected ${numRequired} but received ${
args.length
}.`
);
}
if (args.length > configuredKeys.length) {
throw new Error(
`Too many arguments provided. Expected max ${
configuredKeys.length
} but received ${args.length}.`
);
}
args.forEach((arg, index) => {
const observedType = Array.isArray(arg)
? 'array'
: arg === null
? 'null'
: arg instanceof Error
? 'error'
: isPromise(arg)
? 'promise'
: Buffer.isBuffer(arg)
? 'buffer'
: typeof arg;
const typesAtIndex = types[index];
const errorMessage = invalidArgMessage.bind(
this,
positionName(index),
typesAtIndex,
observedType
);
if (
REQUIRED in typesAtIndex &&
isMissingFrom(typesAtIndex.required, observedType)
) {
throw new TypeError(errorMessage(REQUIRED));
}
if (
OPTIONAL in typesAtIndex &&
arg !== undefined &&
isMissingFrom(typesAtIndex.optional, observedType)
) {
throw new TypeError(errorMessage(OPTIONAL));
}
});
return true;
}
function getTypes(typeConfig) {
return typeConfig.split(' ').reduce((result, str, index) => {
if (isOptional(str)) {
return Object.assign({}, result, {
[index]: {
[OPTIONAL]: str
.split('|')
.map(s => s.replace('[', '').replace(']', ''))
}
});
}
if (isRequired(str)) {
return Object.assign({}, result, {
[index]: {
[REQUIRED]: str
.split('|')
.map(s => s.replace('<', '').replace('>', ''))
}
});
}
if (str === '') {
return result;
}
throw new Error(
`Invalid type config in the ${positionName(index)} position.`
);
}, {});
}
function isMissingFrom(types, observed) {
return types.indexOf(observed) < 0 && types.indexOf('*') < 0;
}
function compactArgs(args) {
const lastArg = args[args.length - 1];
if (lastArg === undefined || lastArg === '') {
return args.filter(arg => arg !== undefined && arg !== '');
}
return args;
}
function invalidArgMessage(position, types, observed, kind) {
return `Invalid ${position} argument. Expected ${expectedTypes(
types,
kind
)} but received ${observed}.`;
}
function positionName(index) {
const positionNames = [
'first',
'second',
'third',
'fourth',
'fifth',
'sixth'
];
return positionNames[index] || 'manyith';
}
function expectedTypes(types, kind) {
return types[kind]
.concat(
kind === OPTIONAL && types[OPTIONAL].indexOf('undefined') < 0
? 'undefined'
: []
)
.join(' or ');
}
function isOptional(arg) {
return arg.match(/^\[(\w+|\*)((?:\|(\w+))*?)]/);
}
function isRequired(arg) {
return arg.match(/^<(\w+|\*)((?:\|(\w+))*?)>/);
}
// 'borrowed' from: https://github.com/then/is-promise/commit/ed0eaa4dec17597f0dae892a0472a9b7f459320d
function isPromise(obj) {
return (
Boolean(obj) &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function'
);
}