-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.js
More file actions
69 lines (61 loc) · 1.37 KB
/
utils.js
File metadata and controls
69 lines (61 loc) · 1.37 KB
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
const {URL} = require('url');
function THROW(err) {
throw err;
}
function ASSERT(msg, ...params) {
for (const [index, param] of params.entries()) {
if (!param) {
THROW(new Error(`${msg} : Failed at [${index}]`));
}
}
}
function PARAMCHECK(...params) {
for (const [index, param] of params.entries()) {
if (param === undefined) {
THROW(new Error(`Param Check : Failed at [${index}]`));
}
}
}
function CONDITIONALPARAMCHECK(...params) {
for (const [index, [cond, param]] of params.entries()) {
if (!cond) {
continue;
}
if (param === undefined) {
THROW(new Error(`Conditional Param Check : Failed at [${index}]`));
}
}
}
function _empty() {}
function tryCatch(...params) {
const body = params.shift();
try {
return body();
} catch (err) {
if (params.length > 0) {
return tryCatch(...params);
}
throw err;
}
}
function createUrl(url, base) {
return tryCatch(
() => {
return new URL(url);
},
() => {
return new URL(url, base);
},
() => {
return {href: url};
}
);
}
module.exports = {
THROW,
ASSERT: process.env.NODE_ENV === 'production' ? _empty : ASSERT,
PARAMCHECK: process.env.NODE_ENV === 'production' ? _empty : PARAMCHECK,
CONDITIONALPARAMCHECK: process.env.NODE_ENV === 'production' ? _empty : CONDITIONALPARAMCHECK,
tryCatch,
createUrl
};