-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathparam-util.js
More file actions
60 lines (51 loc) · 1.59 KB
/
param-util.js
File metadata and controls
60 lines (51 loc) · 1.59 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
'use strict';
/**
* @module ParamUtil
*/
/**
* Create a function to check, whether a value is one of the given types.
* @param types {...string[]}
* @return {function(*): boolean}
*/
function isInTypes (...types) {
return function (value) {
return types.includes(typeof value);
};
}
/**
* Check if a value is defined (not missing)
* @param value
* @return {boolean}
*/
function isDefined (value) {
// TODO: in future versions, consider changing this to `value !== undefined && value !== null && value !== ''`,
// which might be a breaking changes for some users
return !!value;
}
/**
* Safely converts a value to a string in the following order:
* - If the value is already a string, return it.
* - If the value has a `toString` method, call it and return the result.
* - If the value is `null` or `undefined`, return an empty string.
* - Otherwise, use the global `String` function to convert the value.
* @param value {*}
* @return {string}
*/
function toString(value) {
const type = typeof value;
if (type === 'string') {
return value;
}
if (type === 'undefined' || value === null) {
throw new TypeError(`Cannot convert ${value} to a string`);
}
if (type === 'number' || type === 'bigint') {
const val = String(value);
if (val === 'NaN' || val === 'Infinity' || val === '-Infinity') {
throw new TypeError(`Invalid numeric value ${value}, cannot be converted to a string (${val})`);
}
return val;
}
throw new TypeError(`Cannot convert value ${value} of type ${type} to a string`);
}
module.exports = { isInTypes, isDefined, toString };