-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
257 lines (224 loc) · 8 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
function findLastIndex (array, predicate) {
let l = array.length
while (l--) {
if (predicate(array[l], l, array)) { return l }
}
return -1
}
/**
* KitsuneParser
*
* Can be used for advanced commands with parsing arguments.
* Usage: new KitsuneParser()
*/
class KitsuneParser {
/**
* KitsuneParser
*
* Can be used for advanced commands with parsing arguments.
*/
constructor () {}
async parseAsync(args, usage) {
return await this.parse(args, usage)
}
/**
* Parse the arguments.
*
* @param {array} args arguments that need to be parsed
* @param {object} usage object that contains usage for every single argument
* @returns {array} parsed arguments with their own types
*/
parse (args, usage) {
const parsed = []
if (!Array.isArray(usage)) {
throw new KitsuneParserMessageError('Parser arguments must be an array.')
}
// required key fix start
usage = usage.map(arg => {
if (arg.required === undefined) {
arg.required = true
}
return arg
})
const requiredArray = usage.map(arg => arg.required)
const len = requiredArray.filter(val => val === false).length
const pos = findLastIndex(requiredArray, val => val === false)
if (len > 1) {
throw new KitsuneParserArgumentError(undefined, pos, undefined, {
id: 'NON_REQUIRED_ARGUMENT_MUST_BE_ONE',
message: 'Non-required argument must be only one.'
})
}
if (pos < requiredArray.length - 1 && pos !== -1) {
throw new KitsuneParserArgumentError(undefined, pos, undefined, {
id: 'NON_REQUIRED_ARGUMENT_AT_THE_END',
message: 'Non-required argument must be at the end of the arguments array.'
})
}
// required key fix end
let usageIndex = 0
usage.forEach(({ type, required, count, ...otherOptions }) => {
const currentArgument = count ? args.slice(usageIndex, count === -1 ? usageIndex + args.length: usageIndex + count) : args[usageIndex]
let result
if (!Array.isArray(type)) {
if (!(type instanceof KitsuneParserType)) throw new KitsuneParserArgumentError(undefined, pos, type, {
id: 'TYPE_IS_NOT_KITSUNEPARSERTYPE',
message: 'Type is not created from new KitsuneParserType(name, validate, transform) or instanceof KitsuneParserType'
})
if (!Array.isArray(currentArgument)) {
result = type.options(otherOptions).parse(currentArgument)
} else {
result = currentArgument.map(arg => type.options(otherOptions).parse(arg))
}
} else {
type.some(t => {
if (!(t instanceof KitsuneParserType)) throw new KitsuneParserArgumentError(undefined, pos, t, {
id: 'TYPE_IS_NOT_KITSUNEPARSERTYPE',
message: 'Type is not created from new KitsuneParserType(name, validate, transform) or instanceof KitsuneParserType'
})
if (!Array.isArray(currentArgument)) {
result = t.options(otherOptions).parse(currentArgument)
} else {
result = currentArgument.map(arg => t.options(otherOptions).parse(arg, otherOptions))
}
})
}
if (Array.isArray(result)) {
const indices = result.flatMap((bool, index) => bool !== undefined ? [] : index)
if (indices.length) {
throw new KitsuneParserArgumentError(currentArgument[indices[0]], usageIndex + indices[0], type, {
id: 'REQUIRED_ARGUMENT_UNDEFINED',
message: 'Required argument is undefined'
})
}
if (result.length < count) {
throw new KitsuneParserArgumentError(currentArgument, usageIndex, type, {
id: 'ARGUMENTS_COUNT_IS_NOT_EQUAL_TYPE_COUNT',
message: 'Passed arguments count is not equal to type count.'
})
}
}
if (result === undefined && required) {
throw new KitsuneParserArgumentError(currentArgument, usageIndex, type, {
id: 'NO_REQUIRED_ARGUMENT',
message: 'Required argument is undefined'
})
}
parsed.push(Array.isArray(result) ? result.map(e => e) : result)
count ? usageIndex += count : usageIndex++
})
return parsed
}
}
/**
* KitsuneParser
*
* Can be used for advanced commands with parsing arguments.
* Usage: new KitsuneParserType(name, validate, transform)
*/
class KitsuneParserType {
static STRING = new KitsuneParserType(
'string',
(val, options) => options.explicit ? typeof val === 'string' || val instanceof String : true,
val => String(val)
)
static NUMBER = new KitsuneParserType(
'number',
val => !isNaN(val) || typeof val === 'number' || val instanceof Number,
val => parseInt(val)
)
static BOOLEAN = new KitsuneParserType(
'boolean',
val => String(val) === 'true' || String(val) === 'false',
val => String(val) === 'true'
)
/**
* Create new parser type
* @param {string} name name of the type
* @param {function} validate value validator function
* @param {function} transform value transformer function
*/
constructor(name, validate, transform) {
this._name = name
this._validate = validate || this._validate
this._transform = transform || this._transform
this._options = {}
}
/**
* Add any options for parsing. Used in _validate(value) function
* @param {JSON} opts
*/
options(opts) {
this._options = opts
return this
}
/**
* This is a bare-bones function. Create new type or override this function in your type class.
*
* Validate any given value.
* @param {*} value
*/
_validate(value) { }
/**
* This is a bare-bones function. Create new type or override this function in your type class.
*
* Transform any given value if validated by _validate(value) function.
* @param {*} value
*/
_transform(value) { }
/**
* Parse the value by given validation and transforming functions.
* You may add options by options({ ... }) before this function.
*/
parse(value) {
return this._validate(value, this._options) && this._transform(value) || undefined
}
}
/**
* Default kitsune error.
* Usage: (e instanceof KitsuneParserError)
*/
class KitsuneParserError extends Error {
constructor (m) {
super(m)
this.message = m
this.name = 'KitsuneParserError'
}
}
/**
* Default parser argument error.
* Usage: new KitsuneParserArgumentError(value, index, type, message)
*/
class KitsuneParserArgumentError extends KitsuneParserError {
/**
* Argument error of KitsuneParser
*
* @param {*} value value of argument
* @param {number} index index of argument
* @param {string} type type of argument
* @param {string=} message additional message
*/
constructor (value, index, type, message) {
super(message || undefined)
this.value = value
this.index = index
this.type = type
}
}
class KitsuneParserMessageError extends KitsuneParserError {
/**
* Message error of KitsuneParser.
*
* @param {String} message message
*/
constructor (message) {
super(message)
}
}
module.exports = {
KitsuneParserError,
KitsuneParserArgumentError,
KitsuneParserMessageError,
KitsuneParserType,
KitsuneParser
}