-
Notifications
You must be signed in to change notification settings - Fork 0
/
afirmar.js
34 lines (31 loc) · 990 Bytes
/
afirmar.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
const same = (a, b) => {
if (b.test) {
if (b.test(a)) return true;
throw new Error(`'${a}' does not match ${b}`);
}
if (Array.isArray(a) || Array.isArray(b)) {
return JSON.stringify(a) === JSON.stringify(b);
}
if (typeof a === "object" || typeof b === "object") {
return JSON.stringify(a) === JSON.stringify(b);
}
return a === b;
};
const assert = (claim, equals = true) => {
if (same(claim, equals)) return;
throw new Error(`'${claim}' is not '${equals}'`);
};
export const assertType = (obj, type) => {
const plain = JSON.stringify(obj);
if (type === "array") {
if (Array.isArray(obj)) return true;
throw new Error(`'${plain}' is not an 'array' (it is '${typeof obj}')`);
}
if (type === "object" && Array.isArray(obj)) {
throw new Error(`'${plain}' is not an 'object' (it is an 'array')`);
}
if (typeof obj !== type) {
throw new Error(`'${plain}' is not a '${type}' (it is '${typeof obj}')`);
}
};
export default assert;