forked from InteractiveAdvertisingBureau/iabtcf-es
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsameDataDiffRef.ts
More file actions
80 lines (48 loc) · 1.61 KB
/
sameDataDiffRef.ts
File metadata and controls
80 lines (48 loc) · 1.61 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {expect} from 'chai';
import {isPrimitive} from './isPrimitive.js';
export const sameDataDiffRef = (obj1: object, obj2: object, objName: string, ignoreKeys?: string[]): void => {
if (obj1 === undefined && obj2 === undefined) {
return;
}
expect(typeof obj1, `typeof ${objName}`).to.equal(typeof obj2);
expect(obj1, objName).not.to.equal(obj2);
const keySet = new Set<string>(
Object.keys(obj1)
.concat(Object.keys(obj2))
/**
* filter out all of the keys that end with an underscore since those are
* private members
*/
.filter((key: string) => (key.indexOf('_') !== key.length - 1)),
);
if (Array.isArray(ignoreKeys)) {
ignoreKeys.forEach((item: string): void => {
if (keySet.has(item)) {
keySet.delete(item);
}
});
}
for (const key of keySet) {
if (key.indexOf('_') !== key.length - 1) {
const item1 = obj1[key];
const item2 = obj2[key];
const itsType = typeof item1;
expect(typeof item2, `typeof ${objName}->${key}`).to.equal(itsType);
if (item1 === undefined && item2 === undefined) {
continue;
}
if (isPrimitive(item1)) {
expect(item2, `${objName}->${key}`).to.equal(item1);
} else {
if (item1 instanceof Date) {
const time1 = item1.getTime();
const time2 = item2.getTime();
// should be within the 200 milisecond range
expect(time2, `${objName}->${key}`).to.be.within(time1 - 100, time1 + 100);
} else {
sameDataDiffRef(item1, item2, key, ignoreKeys);
}
}
}
}
};