-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoerce-input.js
More file actions
50 lines (50 loc) · 1.11 KB
/
coerce-input.js
File metadata and controls
50 lines (50 loc) · 1.11 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
'use strict';
/**
* Coerces values into JavaScript object types where available
* @function Coerce
* @param {any} value
* @returns {any}
* @throws {!SyntaxError}
* @author S0AndS0
* @license AGPL-3.0
* @example
* Coerce('1');
* //> 1
*
* Coerce('stringy');
* //> "stringy"
*
* Coerce('{"key": "value"}');
* //> {key: "value"}
*/
const Coerce_Input = (value) => {
try {
return JSON.parse(value);
}
catch (e) {
/* istanbul ignore next */
if (!(e instanceof SyntaxError)) {
throw e;
}
if (['undefined', undefined].includes(value)) {
return undefined;
}
else if (['NaN', NaN].includes(value)) {
return NaN;
}
else if (['Infinity', Infinity].includes(value)) {
return Infinity;
}
else if (['-Infinity', -Infinity].includes(value)) {
return -Infinity;
}
else {
return value;
}
}
};
/* istanbul ignore next */
if (typeof module !== 'undefined') {
module.exports = { Coerce_Input };
}
//# sourceMappingURL=coerce-input.js.map