-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.mjs
48 lines (43 loc) · 1.38 KB
/
utils.mjs
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
/*
* Author: A.G.
* Date: 2019/11/15
*/
export function execRegExpGroups(regexp, s, _default_ = null, cb = null) {
if (typeof s !== "string") {
throw new TypeError("Input is not a string");
}
let match = regexp.exec(s);
let result = match ? match.groups : _default_;
if ((typeof cb === "function") && (match !== null)) {
try {
let userResult = cb(result);
if (typeof userResult !== "undefined") {
return userResult;
}
} catch (e) {
console.error(e);
}
}
return result;
}
export function toHex(value, digits = 2) {
if (typeof value === "number") {
let hex = value.toString(16).toUpperCase().padStart(digits, "0");
return `0x${hex}`;
}
return "";
}
export function parseParameters(parser, input) {
let params = `${input}`.split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/);
return parser.fields.reduce((accum, fieldName, i) => {
accum[fieldName] = i < params.length ? params[i] : null
return accum;
}, {});
}
export function validateParameters(schema, input, throwsErrorOnInvalid = true) {
let { value, error } = schema.validate(input);
return error ? throwsErrorOnInvalid ? ((() => { throw error })()) : null : value;
}
export function unescapeFileName(filename) {
return `${filename}`.replace(/"([^"]+(?="))"/g, '$1');
}