-
Notifications
You must be signed in to change notification settings - Fork 3
/
check.js
204 lines (174 loc) · 5.03 KB
/
check.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @packageDocumentation
* This is hTest’s assertion library (but you can use any other)
* Most functions generate assertion functions based on the parameters you specify.
*/
import { getType } from "./util.js";
/**
* Combine multiple checks, requiring a test to pass all of them to pass
* @param {...function} fns
* @returns {function}
*/
export function and (...fns) {
return function (...args) {
return fns.every(fn => fn(...args));
}
}
/**
* Combine multiple checks, requiring a test to pass any of them to pass
* @param {...function} fns
* @returns {function}
*/
export function or (...fns) {
return function (...args) {
return fns.some(fn => fn(...args));
}
}
/**
* Check value type
* @param {string} type
* @returns {function}
*/
export function is (type) {
type = type.toLowerCase();
return function (actual) {
return getType(actual) === type;
}
}
/**
* Apply a checking function recursively to objects and collections
* @param {function} [check] Function to apply to compare primitive values. Defaults to strict equality
* @returns {function(actual, expect): boolean}
*/
export function deep (check = (a, b) => a === b) {
let callee = function (actual, expect) {
if (check.call(this, actual, expect)) {
return true;
}
if (typeof expect !== "object") {
// If not an object, it's definitely not a container object,
// and we know it doesn't pass, so we can fail early.
return false;
}
if (Array.isArray(expect)) {
if (!Array.isArray(actual) || actual.length < expect.length) {
return false;
}
return expect.every((ref, i) => callee.call(this, actual[i], ref));
}
let type = getType(expect);
let actualtype = getType(actual);
if (expect?.[Symbol.iterator]) {
// Iterable collection (Array, Set, Map, NodeList, etc.)
if (actualtype !== type) {
return false;
}
return callee.call(this, [...actual], [...expect]);
}
// Compare objects recursively
if (type === "object") {
if (actualtype !== type) {
return false;
}
let propertyUnion = new Set([...Object.keys(expect), ...Object.keys(actual)]);
return [...propertyUnion].every(key => callee(actual[key], expect[key]));
}
return false;
}
callee.shallow = check;
return callee;
};
/**
* Shallow equals function at the core of many other comparison functions
* @param {object} options
* @param {boolean} [options.looseTypes = false] If true, skip type check (e.g. "5" can match 5)
* @param {boolean} [options.subset = false] If true, `undefined` is considered equal to anything
* @param {number} [options.epsilon = 0] Epsilon for number comparison
* @returns {function(actual, expect): boolean}
*/
export function shallowEquals ({
looseTypes = false,
subset = false,
epsilon = 0,
} = {}) {
return function (actual, expect) {
if (expect === actual) {
return true;
}
if (expect === null) {
return actual === null;
}
if (expect === undefined && subset) {
return true;
}
let expectType = getType(expect);
let actualType = getType(actual);
if (expectType === actualType || looseTypes) {
if (expectType === "number") {
if (Number.isNaN(expect)) {
return Number.isNaN(actual);
}
if (epsilon > 0) {
return Math.abs(expect - actual) <= epsilon;
}
}
return expect == actual;
}
return false;
}
}
/**
* Compare by equality. Slighly more permissive than `===`.
* Deep by default, use `equals.shallow` for shallow comparison.
* @param {*} expect
* @param {*} actual
* @returns {boolean}
*/
export const equals = deep(shallowEquals());
/**
* Compare by specifying subsets of properties to compare
* @param {*} expect
* @param {*} actual
* @returns {boolean}
*/
export const subset = deep(shallowEquals({subset: true}));
/**
* Compare numbers or lists of numbers with a margin of error
* @param {object} [options] Options object
* @param {number} [o.epsilon = 0] Epsilon for comparison
* @returns {function(actual, expect): boolean}
*/
export function proximity ({epsilon = Number.EPSILON, ...options} = {}) {
return shallowEquals({epsilon, ...options});
}
/**
* Check that numbers (or lists of numbers) are within certain upper and/or lower bounds
* @param {object} [options]
* @param {number} options.gt
* @param {number} options.gte
* @param {number} options.lt
* @param {number} options.lte
* @param {number} options.from Alias of `options.lt`
* @param {number} options.max Alias of `options.lte`
* @param {number} options.to Alias of `options.gt`
* @param {number} options.min Alias of `options.gte`
* @returns {function(actual, expect): boolean}
*/
export function range (options = {}) {
options.lt ??= options.from;
options.lte ??= options.min;
options.gt ??= options.to;
options.gte ??= options.max;
return function (actual) {
return (
(options.lt === undefined || actual < options.lt) &&
(options.lte === undefined || actual <= options.lte) &&
(options.gt === undefined || actual > options.gt) &&
(options.gte === undefined || actual >= options.gte)
);
}
}
/**
* Alias of `range()`
*/
export const between = range