-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigChecks.js
54 lines (52 loc) · 1.39 KB
/
configChecks.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
const _ = require("lodash");
const ConfigError = require("../exceptions/ConfigError");
function _global(config) {
return {
name: config.name,
row: _sanitizeInteger(config, "row"),
col: _sanitizeInteger(config, "col"),
rowspan: _sanitizeInteger(config, "rowspan"),
colspan: _sanitizeInteger(config, "colspan")
};
}
function _sanitizeInteger(config, key, defaultValue) {
// When default is undefined, it means the value must be define
if (!config.hasOwnProperty(key)) {
if (_.isUndefined(defaultValue)) {
throw new ConfigError(
`Widget '${config.name}' is missing required integer attribute '${key}'`
);
} else {
return defaultValue;
}
}
if (!_.isInteger(config[key])) {
throw new ConfigError(
`Widget '${config.name}' attribute '${key}' must be an integer`
);
}
return config[key];
}
function _sanitizeRegExp(config, key) {
if (!config.hasOwnProperty(key)) {
throw new ConfigError(
`Widget '${config.name}' is missing required attribute '${key}'`
);
}
try {
return new RegExp(config[key], "gi");
} catch (e) {
throw new ConfigError(
`Widget '${
config.name
}' attribute '${key}' must be a valid regular expression. Got Error ${
e.message
}`
);
}
}
module.exports = {
global: _global,
sanitizeInteger: _sanitizeInteger,
sanitizeRegExp: _sanitizeRegExp
};