This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (56 loc) · 1.59 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
/**
* Set object values for a context.
*
* Old object values are reset after the current testing context has finished.
*
* If you pass in a function, it is called to fetch arguments in before block.
*
* @param object {Object | Array} The object or array that should have its values changed
* @param [newValuesOrCb] {(Object | Array) | Function => (Object | Array)}
* New values for the object.
*/
var setForContext = function (object, newValuesOrCb) {
var oldObject;
var assertArguments = function () {
if (typeof object !== 'object') {
throw new Error('Invariant error: First argument for setForContext ' +
'must be an object or an array, you gave ' + JSON.stringify(object));
}
};
before(function () {
var newValues, i;
assertArguments();
if (typeof newValuesOrCb === 'function') {
newValues = newValuesOrCb();
}
else {
newValues = newValuesOrCb;
}
if (Array.isArray(object)) {
oldObject = object.slice(0);
object.length = 0;
for (i = 0; i < newValues.length; i++) {
object[i] = newValues[i];
}
}
else {
oldObject = Object.assign({}, object);
Object.assign(object, newValues);
}
});
after(function () {
var i;
assertArguments();
if (Array.isArray(oldObject)) {
object.length = 0;
for (i = 0; i < oldObject.length; i++) {
object[i] = oldObject[i];
}
}
else {
Object.keys(object).forEach(function (key) { delete object[key]; });
Object.assign(object, oldObject);
}
});
};
module.exports = setForContext;