-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.cjs
39 lines (33 loc) · 1016 Bytes
/
lib.cjs
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
function subtract(set, array) {
return set.filter((item) => !array.includes(item));
}
function makeRegexp(suffixes, extname) {
const suffix = suffixes.length > 0 ? ['', ...suffixes].join('\\.') : '';
const mode = extname.length === 1 ? extname[0] : `(${extname.join('|')})`;
return new RegExp(`${suffix}\\.${mode}$`);
}
function childTypeCheck(parameters) {
if (parameters.length > 0) {
parameters.forEach((parameter) => {
if (typeof parameter !== 'string' || parameter === '') {
throw new TypeError(
`Parameter ${JSON.stringify(parameter)} is not valid`,
);
}
});
}
}
function arrayTypeCheck(parameters, name, length = false) {
if (!Array.isArray(parameters)) {
throw new TypeError(`Parameter \`${name}\` should be an array`);
}
if (length && parameters.length === 0) {
throw new TypeError(`Parameter \`${name}\` shouldn't be empty`);
}
childTypeCheck(parameters);
}
module.exports = {
makeRegexp,
subtract,
arrayTypeCheck,
};