Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace third party deep-equal library with native implementation #5509

Merged
merged 3 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 24 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
"core-js-pure": "^3.6.5",
"criteo-direct-rsa-validate": "^1.1.0",
"crypto-js": "^3.3.0",
"deep-equal": "^1.0.1",
"dlv": "1.1.3",
"dset": "2.0.1",
"express": "^4.15.4",
Expand Down
20 changes: 17 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-console */
import { config } from './config.js';
import clone from 'just-clone';
import deepequal from 'deep-equal';
import find from 'core-js-pure/features/array/find.js';
import includes from 'core-js-pure/features/array/includes.js';

Expand Down Expand Up @@ -1169,13 +1168,28 @@ export function buildUrl(obj) {
}

/**
* This function compares two objects for checking their equivalence.
* This function deeply compares two objects checking for their equivalence.
* @param {Object} obj1
* @param {Object} obj2
* @returns {boolean}
*/
export function deepEqual(obj1, obj2) {
return deepequal(obj1, obj2);
if (obj1 === obj2) return true;
else if ((typeof obj1 === 'object' && obj1 !== null) && (typeof obj2 === 'object' && obj2 !== null)) {
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false;
for (let prop in obj1) {
if (obj2.hasOwnProperty(prop)) {
if (!deepEqual(obj1[prop], obj2[prop])) {
return false;
}
} else {
return false;
}
}
return true;
} else {
return false;
}
}

export function mergeDeep(target, ...sources) {
Expand Down
63 changes: 63 additions & 0 deletions test/spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,4 +1115,67 @@ describe('Utils', function () {
});
});
});

describe('deepEqual', function() {
it('should return "true" if comparing the same object', function() {
const obj1 = {
banner: {
sizeConfig: [
{ minViewPort: [0, 0], sizes: [] },
{ minViewPort: [1000, 0], sizes: [[1000, 300], [1000, 90], [970, 250], [970, 90], [728, 90]] },
],
},
};
const obj2 = obj1;
expect(utils.deepEqual(obj1, obj2)).to.equal(true);
});
it('should return "true" if two deeply nested objects are equal', function() {
const obj1 = {
banner: {
sizeConfig: [
{ minViewPort: [0, 0], sizes: [] },
{ minViewPort: [1000, 0], sizes: [[1000, 300], [1000, 90], [970, 250], [970, 90], [728, 90]] },
],
},
};
const obj2 = {
banner: {
sizeConfig: [
{ minViewPort: [0, 0], sizes: [] },
{ minViewPort: [1000, 0], sizes: [[1000, 300], [1000, 90], [970, 250], [970, 90], [728, 90]] },
],
},
};
expect(utils.deepEqual(obj1, obj2)).to.equal(true);
});
it('should return "true" if comparting the same primitive values', function() {
const primitive1 = 'Prebid.js';
const primitive2 = 'Prebid.js';
expect(utils.deepEqual(primitive1, primitive2)).to.equal(true);
});
it('should return "false" if comparing two different primitive values', function() {
const primitive1 = 12;
const primitive2 = 123;
expect(utils.deepEqual(primitive1, primitive2)).to.equal(false);
});
it('should return "false" if comparing two different deeply nested objects', function() {
const obj1 = {
banner: {
sizeConfig: [
{ minViewPort: [0, 0], sizes: [] },
{ minViewPort: [1000, 0], sizes: [[1000, 300], [1000, 90], [970, 250], [970, 90], [728, 90]] },
],
},
};
const obj2 = {
banner: {
sizeConfig: [
{ minViewPort: [0, 0], sizes: [] },
{ minViewPort: [1000, 0], sizes: [[1000, 300], [728, 90]] },
],
},
}
expect(utils.deepEqual(obj1, obj2)).to.equal(false);
});
});
});