-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.js
More file actions
63 lines (50 loc) · 1.73 KB
/
validator.js
File metadata and controls
63 lines (50 loc) · 1.73 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
61
62
63
/***********************************************
"validator.js"
Created by Michael Cheng on 5/25/2020 11:03:24 AM
http://michaelcheng.us/
michael@michaelcheng.us
--All Rights Reserved--
***********************************************/
'use strict';
function objEqual(a, b) {
// http://stackoverflow.com/a/16788517/4230736
if(a == null || b == null) return a === b;
if(a.constructor !== b.constructor) return false;
if(a instanceof Function) return a === b;
if(a instanceof RegExp) return a === b;
if(a === b || a.valueOf() === b.valueOf()) return true;
if(Array.isArray(a) && a.length !== b.length) return false;
if(a instanceof Date) return false;
if(!(a instanceof Object)) return false;
if(!(b instanceof Object)) return false;
var level = Object.keys(a);
return Object.keys(b).every(k => ~level.indexOf(k)) &&
level.every(k => equal(a[k], b[k]));
}
function equal(spec) {
const expected = spec.expectedResult, actual = spec.actualResult;
return objEqual(expected, actual);
}
function exact(spec) {
return spec.expectedResult === spec.actualResult;
}
export const OBSERVER_STORE = Symbol('janus-observer-store');
function observerCalled(spec) {
const fn = spec.actualResult;
return fn[OBSERVER_STORE].callCount > 0;
}
function observerCalledTimes(spec) {
const fn = spec.actualResult;
return fn[OBSERVER_STORE].callCount === spec.expectedResult;
}
function observerCalledWith(spec) {
const fn = spec.actualResult;
return objEqual(fn[OBSERVER_STORE].calls[fn[OBSERVER_STORE].calls.length-1], spec.expectedResult);
}
export const Validator = {
EQUAL: equal,
EXACT: exact,
OBSERVER_CALLED: observerCalled,
OBSERVER_CALLED_TIMES: observerCalledTimes,
OBSERVER_CALLED_WITH: observerCalledWith,
};