-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
54 lines (49 loc) · 1.31 KB
/
utils.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
/**
* Returns a copy of the given object. The object must be serializable to
* JSON, thus cloning will only work on primitive objects.
*/
function cloneObject (obj) {
return JSON.parse(JSON.stringify(obj))
}
/**
* Check if a given string is a valid string
*
* @param sString - the string that needs to be checked
*/
function isValidString (sString) {
if (typeof (sString) !== 'string') {
throw new Error(`String expected, found ${sString}`)
}
if (sString === '') {
throw new Error(`Non-empty string expected.`)
}
}
/**
* Check if given argument is a number n with min <= n <= max.
*
* @param n - the thing to be checked
*/
function isInRange (n, min, max) {
return typeof n === 'number' && n >= min && n <= max
}
/**
* Check if a given variable is a valid Integer
*
* @param int - variable/Integer that needs to be checked
**/
function isInteger (int) {
if (typeof int !== 'number' || int % 1 !== 0) {
throw new Error(`${int} is not a valid Integer.`)
}
}
/**
* Check if a given variable is a valid Timestamp
*
* @param timest - variable/Integer that needs to be checked
**/
function isTimestamp (timest) {
if (typeof timest !== 'number') {
throw new Error(`${timest} is not a valid Integer.`)
}
}
module.exports = { cloneObject, isValidString, isInRange, isInteger, isTimestamp }