-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (129 loc) · 4.44 KB
/
index.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
var Util = {
/**
* The iteration cache which will store iterated nodes
*/
_iterCache: [],
_isArray: function (item) {
return Object.prototype.toString.call(item) === '[object Array]'
},
_isObject: function (item) {
return Object.prototype.toString.call(item) === '[object Object]'
},
_isUndefined: function (item) {
return typeof item === 'undefined';
},
/**
* Stringify the given item.
* If the item has circular references, the circular references
* will be marked as [CIRCULAR REFERENCE]
*/
_stringify: function (item) {
var cache = [];
return JSON.stringify(item, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Found circular reference
return '[CIRCULAR REFERENCE]';
}
cache.push(value);
return value;
}
return value;
});
},
_deepEqualLog: function (title, path, actual, expected) {
title = title || '';
path = path || [];
return title + ' ' + path.join(" -> ") + ' | actual: ' + Util._stringify(actual) + ', expected: ' + Util._stringify(expected);
},
_doDeepEqual: function (actual, expected, notEqualCallback, path) {
var iter;
notEqualCallback = notEqualCallback || function () {};
path = path || [];
if (Util._iterCache.indexOf(actual) !== -1) {
// We have iterated this node before and it passed deep equal check
return true;
}
// Primitive type
if (actual === expected) {
return true;
}
// NaN
if (Number.isNaN(actual) && Number.isNaN(expected)) {
return true;
} else if (Number.isNaN(actual) || Number.isNaN(expected)) {
notEqualCallback(Util._deepEqualLog('[Value different]', path, actual, expected));
return false;
}
// Array
if (Util._isArray(actual) && Util._isArray(expected)) {
iter = actual.length;
if (iter !== expected.length) {
notEqualCallback(Util._deepEqualLog('[Array with different length]', path, actual, expected));
return false;
}
// Mark the actual and expected array has been iterated
Util._iterCache.push(actual);
while (iter--) {
if (!Util._doDeepEqual(actual[iter], expected[iter], notEqualCallback, path.concat(iter))) {
return false;
}
}
return true;
} else if (Util._isArray(actual) || Util._isArray(expected)) {
notEqualCallback(Util._deepEqualLog('[Different type]', path, actual, expected));
return false;
}
// Object
if (Util._isObject(actual) && Util._isObject(expected)) {
if (Object.keys(actual).length !== Object.keys(expected).length) {
notEqualCallback(Util._deepEqualLog('[Object with different keys]', path, actual, expected));
return false;
}
// Mark the actual and expected array has been iterated
Util._iterCache.push(actual);
for (iter in actual) {
if (actual.hasOwnProperty(iter)) {
if (!Util._doDeepEqual(actual[iter], expected[iter], notEqualCallback, path.concat(iter))) {
return false;
}
}
}
return true;
} else if (Util._isObject(actual) || Util._isObject(expected)) {
notEqualCallback(Util._deepEqualLog('[Different type]', path, actual, expected));
return false;
}
// Default to false
notEqualCallback(Util._deepEqualLog('[Value different]', path, actual, expected));
return false;
},
/**
* Check the deep equal for primitive type values, array and objects.
* The native assert.deepEqual doesn't work well for NaN case as well
* as +0/-0 case. See https://github.com/substack/node-deep-equal for
* more details
*/
deepEqual: function (actual, expected, notEqualCallback) {
notEqualCallback = notEqualCallback || function () {};
Util._iterCache = [];
return Util._doDeepEqual(actual, expected, notEqualCallback);
},
/**
* Compare and return both the deep equal results, as well as the not equal message
*/
deepEqualWithMessage: function (actual, expected) {
var notEqualMessages = [],
isDeepEqual = Util.deepEqual(actual, expected, function () {
notEqualMessages.push(Array.prototype.slice.call(arguments).join(''));
});
return {
isDeepEqual: isDeepEqual,
message: notEqualMessages.join('\n')
};
}
};
module.exports = {
deepEqual: Util.deepEqual,
deepEqualWithMessage: Util.deepEqualWithMessage
};