-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathutil.js
121 lines (116 loc) · 3.06 KB
/
util.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
/**
* sanitizes input string by handling escape characters eg: converts '''' to '\'\''
* and trim input if required
*
* @param {String} inputString
* @param {Boolean} [trim] - indicates whether to trim string or not
* @returns {String}
*/
function sanitize (inputString, trim) {
if (typeof inputString !== 'string') {
return '';
}
(trim) && (inputString = inputString.trim());
return inputString.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
}
/**
* sanitizes input options
*
* @param {Object} options - Options provided by the user
* @param {Array} optionsArray - options array received from getOptions function
*
* @returns {Object} - Sanitized options object
*/
function sanitizeOptions (options, optionsArray) {
var result = {},
defaultOptions = {},
id;
optionsArray.forEach((option) => {
defaultOptions[option.id] = {
default: option.default,
type: option.type
};
if (option.type === 'enum') {
defaultOptions[option.id].availableOptions = option.availableOptions;
}
});
for (id in options) {
if (options.hasOwnProperty(id)) {
if (defaultOptions[id] === undefined) {
continue;
}
switch (defaultOptions[id].type) {
case 'boolean':
if (typeof options[id] !== 'boolean') {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
break;
case 'positiveInteger':
if (typeof options[id] !== 'number' || options[id] < 0) {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
break;
case 'enum':
if (!defaultOptions[id].availableOptions.includes(options[id])) {
result[id] = defaultOptions[id].default;
}
else {
result[id] = options[id];
}
break;
default:
result[id] = options[id];
}
}
}
for (id in defaultOptions) {
if (defaultOptions.hasOwnProperty(id)) {
if (result[id] === undefined) {
result[id] = defaultOptions[id].default;
}
}
}
return result;
}
/**
*
* @param {Array} array - form data array
* @param {String} key - key of form data param
* @param {String} type - type of form data param(file/text)
* @param {String} val - value/src property of form data param
* @param {String} disabled - Boolean denoting whether the param is disabled or not
* @param {String} contentType - content type header of the param
*
* Appends a single param to form data array
*/
function addFormParam (array, key, type, val, disabled, contentType) {
if (type === 'file') {
array.push({
key: key,
type: type,
src: val,
disabled: disabled,
contentType: contentType
});
}
else {
array.push({
key: key,
type: type,
value: val,
disabled: disabled,
contentType: contentType
});
}
}
module.exports = {
sanitize: sanitize,
sanitizeOptions: sanitizeOptions,
addFormParam: addFormParam
};