-
Notifications
You must be signed in to change notification settings - Fork 11
/
chai-exclude.js
221 lines (182 loc) · 5.87 KB
/
chai-exclude.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import fclone from 'fclone'
/**
* Chai plugin to exclude keys from comparison.
*/
function chaiExclude (chai, utils) {
const assert = chai.assert
const Assertion = chai.Assertion
/**
* Check if the argument is an array.
*
* @param {any} arg
* @returns {Boolean}
*/
function isArray (arg) {
return Array.isArray(arg)
}
/**
* Check if the argument is an object.
*
* @param {any} arg
* @returns {Boolean}
*/
function isObject (arg) {
return arg === Object(arg) && Object.prototype.toString.call(arg) !== '[object Array]'
}
/**
* Remove keys from an object or an array.
*
* @param {Object|Array} val object or array to remove keys
* @param {Array} props array of keys to remove
* @param {Boolean} recursive true if property needs to be removed recursively
* @returns {Object}
*/
function removeKeysFrom (val, props, recursive = false) {
// Replace circular values with '[Circular]'
const obj = fclone(val)
if (isObject(obj)) {
return removeKeysFromObject(obj, props, recursive)
}
return removeKeysFromArray(obj, props, recursive)
}
/**
* Remove keys from an object and return a new object.
*
* @param {Object} obj object to remove keys
* @param {Array} props array of keys to remove
* @param {Boolean} recursive true if property needs to be removed recursively
* @returns {Object}
*/
function removeKeysFromObject (obj, props, recursive = false) {
const res = {}
for (const [key, val] of Object.entries(obj)) {
if (props.includes(key)) continue
if (recursive && isObject(val)) {
res[key] = removeKeysFromObject(val, props, true)
} else if (recursive && isArray(val)) {
res[key] = removeKeysFromArray(val, props, true)
} else {
res[key] = val
}
}
return res
}
/**
* Remove keys from an object inside an array and return a new array.
*
* @param {Array} array array with objects
* @param {Array} props array of keys to remove
* @param {Boolean} recursive true if property needs to be removed recursively
* @returns {Array}
*/
function removeKeysFromArray (array, props, recursive = false) {
const res = []
let val = {}
if (!array.length) {
return res
}
for (let i = 0; i < array.length; i++) {
if (isObject(array[i])) {
val = removeKeysFromObject(array[i], props, recursive)
} else if (isArray(array[i])) {
val = removeKeysFromArray(array[i], props, recursive)
} else {
val = array[i]
}
res.push(val)
}
return res
}
/**
* Override standard assertion logic method to remove the keys from other part of the equation.
*
* @param {Object} _super
* @returns {Function}
*/
function removeKeysAndAssert (_super) {
return function (val) {
const props = utils.flag(this, 'excludingProps')
if (utils.flag(this, 'excluding')) {
val = removeKeysFrom(val, props)
} else if (utils.flag(this, 'excludingEvery')) {
val = removeKeysFrom(val, props, true)
}
// In case of 'use strict' and babelified code
arguments[0] = val
return _super.apply(this, arguments)
}
}
/**
* Keep standard chaining logic.
*
* @param {Object} _super
* @returns {Function}
*/
function keepChainingBehavior (_super) {
return function () {
_super.apply(this, arguments)
}
}
/**
* Add a new method 'deepEqualExcluding' to 'chai.assert'.
*/
utils.addMethod(assert, 'deepEqualExcluding', function (actual, expected, props, message) {
new Assertion(actual, message).excluding(props).to.deep.equal(expected)
})
/**
* Add a new method `deepEqualExcludingEvery` to 'chai.assert'.
*/
utils.addMethod(assert, 'deepEqualExcludingEvery', function (actual, expected, props, message) {
new Assertion(actual, message).excludingEvery(props).to.deep.equal(expected)
})
/**
* Add a new method 'excluding' to the assertion library.
*/
Assertion.addMethod('excluding', function (props) {
utils.expectTypes(this, ['object', 'array'])
const obj = this._obj
// If exclude parameter is not provided
if (!props) {
return this
}
if (typeof props === 'string') {
props = [props]
}
if (!isArray(props)) {
throw new Error('Excluding params should be either a string or an array')
}
this._obj = removeKeysFrom(obj, props)
utils.flag(this, 'excluding', true)
utils.flag(this, 'excludingProps', props)
})
/**
* Add a new method 'excludingEvery' to the assertion library.
*/
Assertion.addMethod('excludingEvery', function (props) {
utils.expectTypes(this, ['object', 'array'])
const obj = this._obj
// If exclude parameter is not provided
if (!props) {
return this
}
if (typeof props === 'string') {
props = [props]
}
if (!isArray(props)) {
throw new Error('Excluding params should be either a string or an array')
}
this._obj = removeKeysFrom(obj, props, true)
utils.flag(this, 'excludingEvery', true)
utils.flag(this, 'excludingProps', props)
})
Assertion.overwriteMethod('eq', removeKeysAndAssert)
Assertion.overwriteMethod('eql', removeKeysAndAssert)
Assertion.overwriteMethod('eqls', removeKeysAndAssert)
Assertion.overwriteMethod('equal', removeKeysAndAssert)
Assertion.overwriteMethod('equals', removeKeysAndAssert)
Assertion.overwriteChainableMethod('include', removeKeysAndAssert, keepChainingBehavior)
Assertion.overwriteChainableMethod('contain', removeKeysAndAssert, keepChainingBehavior)
Assertion.overwriteChainableMethod('contains', removeKeysAndAssert, keepChainingBehavior)
Assertion.overwriteChainableMethod('includes', removeKeysAndAssert, keepChainingBehavior)
}
export default chaiExclude