-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
27 lines (23 loc) · 933 Bytes
/
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
const throwIfMissing = arg => {
throw new Error(`${arg} is not defined!`)
}
module.exports.arg = function ({ arg, err, accept } = throwIfMissing`object`) {
if (accept && !accept.options) throwIfMissing`accept.options`;
else if (accept && !Array.isArray(accept.options)) throw new Error('accept.options is not Array!')
//Check exist argument
if (!arg) throw new Error(err || 'one argument has not been defined!')
if (typeof arg === 'object') throw new Error('arg can\'t be a object')
//Check if argument is accepted
if (accept && accept.options) {
if (!accept.options.find(i => {
if (typeof i === 'string') i = i.toLowerCase();
if (typeof arg === 'string') arg = arg.toLowerCase();
return i === arg
})) {
throw new Error(accept.err || 'one argument has not been accepted!')
}
}
}
module.exports.args = function (array = throwIfMissing`array`) {
array.map(i => this.arg(i))
}