-
Notifications
You must be signed in to change notification settings - Fork 1
/
asserts.js
38 lines (29 loc) · 958 Bytes
/
asserts.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
'use strict';
const assertRequestedType = (name, type) => {
type = [].concat(type);
return (v) => {
const result = type.some((t) => v.constructor.name === t);
if (!result) {
throw new Error(`"${name}" option must be: ${type.join(' or ')}, but got ${typeof v}`);
}
};
};
const assertBoolean = (name) => assertRequestedType(name, 'Boolean');
const assertString = (name) => assertRequestedType(name, 'String');
const assertInteger = (name) => assertRequestedType(name, 'Number');
const assertArray = (name) => assertRequestedType(name, 'Array');
const assertPositiveInteger = (name) => {
return (v) => {
if (typeof v !== 'number' || v <= 0) {
throw new Error(`'${name}' must be positive integer, but got '${v}'`);
}
};
};
module.exports = {
assertRequestedType,
assertBoolean,
assertString,
assertInteger,
assertArray,
assertPositiveInteger
};