Skip to content

Commit

Permalink
Merge pull request #25 from omgaz/upgrade-deps
Browse files Browse the repository at this point in the history
[patch] update deps
  • Loading branch information
omgaz authored Apr 30, 2020
2 parents 7df351f + f062ac2 commit 87db1db
Show file tree
Hide file tree
Showing 9 changed files with 745 additions and 400 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ npm install diffler

### Return

If same: returns `{}` *(empty object)*
If same: returns `{}` _(empty object)_

If different: A JSON object with preserved path structure. The resulting values will be an object with `from` and `to` fields.

### Example

```js
const diffler = require('diffler');
const diffler = require("diffler");

const before = { name: "omgaz", location: "London" };
const after = { name: "omgaz", location: "Melbourne" };
Expand Down
927 changes: 623 additions & 304 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "diffler",
"version": "2.0.1",
"version": "2.0.2",
"description": "A recursive JSON comparison script for humans",
"main": "./dist/index.js",
"dependencies": {},
"devDependencies": {
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.4.0",
"eslint-plugin-jsdoc": "^15.9.10",
"eslint-plugin-prettier": "^3.1.1",
"minify": "^4.1.3",
"mocha": "^6.2.1",
"prettier": "^1.18.2"
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-jsdoc": "^24.0.0",
"eslint-plugin-prettier": "^3.1.3",
"minify": "^5.1.1",
"mocha": "^7.1.2",
"prettier": "^2.0.5"
},
"scripts": {
"test": "echo \"Running Mocha Tests\" && mocha ./tests/index.js",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function diffler(obj1, obj2) {

// Iterate over obj1 looking for removals and differences in existing values
for (var key in obj1) {
if (obj1.hasOwnProperty(key) && typeof obj1[key] !== 'function') {
if (obj1.hasOwnProperty(key) && typeof obj1[key] !== "function") {
var obj1Val = obj1[key],
obj2Val = obj2[key];

Expand All @@ -23,7 +23,7 @@ function diffler(obj1, obj2) {
}

// If property is an object then we need to recursively go down the rabbit hole
else if (typeof obj1Val === 'object') {
else if (typeof obj1Val === "object") {
var tempDiff = diffler(obj1Val, obj2Val);
if (Object.keys(tempDiff).length > 0) {
if (tempDiff) {
Expand All @@ -44,7 +44,7 @@ function diffler(obj1, obj2) {

// Iterate over obj2 looking for any new additions
for (key in obj2) {
if (obj2.hasOwnProperty(key) && typeof obj2[key] !== 'function') {
if (obj2.hasOwnProperty(key) && typeof obj2[key] !== "function") {
var obj1Val = obj1[key],
obj2Val = obj2[key];

Expand Down
14 changes: 7 additions & 7 deletions tests/data/json-metadata-v1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module.exports = {
version: 1,
created: '2019-07-23T09:36:20.719Z',
modified: '2019-07-23T09:36:20.719Z',
assetUrl: 'readme.svg',
type: 'image/svg+xml',
created: "2019-07-23T09:36:20.719Z",
modified: "2019-07-23T09:36:20.719Z",
assetUrl: "readme.svg",
type: "image/svg+xml",
author: {
id: 14,
name: 'Gary',
name: "Gary",
},
keywords: ['app logo', 'logo', 'readme'],
status: 'draft',
keywords: ["app logo", "logo", "readme"],
status: "draft",
};
26 changes: 13 additions & 13 deletions tests/data/json-metadata-v2.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
module.exports = {
version: 2,
created: '2019-07-23T09:36:20.719Z',
modified: '2019-07-23T09:36:20.719Z',
assetUrl: 'stuff/Appicon-readme.svg',
type: 'image/svg+xml',
description: 'Readme icon for git repo',
created: "2019-07-23T09:36:20.719Z",
modified: "2019-07-23T09:36:20.719Z",
assetUrl: "stuff/Appicon-readme.svg",
type: "image/svg+xml",
description: "Readme icon for git repo",
author: {
id: 14,
name: 'Gary',
name: "Gary",
},
keywords: ['app logo', 'logo', 'readme'],
status: 'pending',
keywords: ["app logo", "logo", "readme"],
status: "pending",
versionHistory: {
1: {
modified: '2019-07-23T09:36:20.719Z',
assetUrl: 'readme.svg',
type: 'image/svg+xml',
modified: "2019-07-23T09:36:20.719Z",
assetUrl: "readme.svg",
type: "image/svg+xml",
author: {
id: 14,
name: 'Gary',
name: "Gary",
},
keywords: ['app logo', 'logo', 'readme'],
keywords: ["app logo", "logo", "readme"],
},
},
};
38 changes: 19 additions & 19 deletions tests/data/json-metadata-v3.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
module.exports = {
version: 3,
created: '2019-07-23T09:36:20.719Z',
modified: '2019-07-23T09:39:25.577Z',
assetUrl: 'stuff/Appicon-readme.png',
type: 'image/png',
description: 'Readme icon for git repo',
created: "2019-07-23T09:36:20.719Z",
modified: "2019-07-23T09:39:25.577Z",
assetUrl: "stuff/Appicon-readme.png",
type: "image/png",
description: "Readme icon for git repo",
author: {
id: 14,
name: 'Gary',
name: "Gary",
},
keywords: ['app logo', 'logo', 'readme'],
status: 'approved',
keywords: ["app logo", "logo", "readme"],
status: "approved",
versionHistory: {
1: {
modified: '2019-07-23T09:36:20.719Z',
assetUrl: 'readme.svg',
type: 'image/svg+xml',
modified: "2019-07-23T09:36:20.719Z",
assetUrl: "readme.svg",
type: "image/svg+xml",
author: {
id: 14,
name: 'Gary',
name: "Gary",
},
keywords: ['app logo', 'logo', 'readme'],
keywords: ["app logo", "logo", "readme"],
},
2: {
modified: '2019-07-23T09:36:20.719Z',
assetUrl: 'stuff/Appicon-readme.svg',
type: 'image/svg+xml',
description: 'Readme icon for git repo',
modified: "2019-07-23T09:36:20.719Z",
assetUrl: "stuff/Appicon-readme.svg",
type: "image/svg+xml",
description: "Readme icon for git repo",
author: {
id: 14,
name: 'Gary',
name: "Gary",
},
keywords: ['app logo', 'logo', 'readme'],
keywords: ["app logo", "logo", "readme"],
},
},
};
92 changes: 56 additions & 36 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
const assert = require('assert');
const diffler = require('../src');

describe('getDiff', () => {
describe('simple checks', () => {
it('returns false when no changes detected', () => {
const testObjectA = { name: 'gary' };
const testObjectB = { name: 'gary' };
const assert = require("assert");
const diffler = require("../src");

describe("getDiff", () => {
describe("simple checks", () => {
it("returns false when no changes detected", () => {
const testObjectA = { name: "gary" };
const testObjectB = { name: "gary" };
const difference = diffler(testObjectA, testObjectB);
assert.equal(Object.keys(difference).length, 0);
assert.deepEqual(difference, {});
});

it('should detect a single property change', () => {
const testObjectA = { name: 'gary' };
const testObjectB = { name: 'cindy' };
it("should detect a single property change", () => {
const testObjectA = { name: "gary" };
const testObjectB = { name: "cindy" };
const difference = diffler(testObjectA, testObjectB);

assert.equal(Object.keys(difference).length, 1);
assert.equal(Object.keys(difference.name).length, 2);

assert.equal(difference.name.from, 'gary');
assert.equal(difference.name.to, 'cindy');
assert.equal(difference.name.from, "gary");
assert.equal(difference.name.to, "cindy");
});

it('should detect no changes', () => {
const testObjectA = { name: 'gary' };
const testObjectB = { name: 'gary' };
it("should detect no changes", () => {
const testObjectA = { name: "gary" };
const testObjectB = { name: "gary" };
const difference = diffler(testObjectA, testObjectB);

assert.equal(Object.keys(difference).length, 0);
});

it('should detect type changes', () => {
const testObjectA = { name: '1' };
it("should detect type changes", () => {
const testObjectA = { name: "1" };
const testObjectB = { name: 1 };
const difference = diffler(testObjectA, testObjectB);

assert.equal(Object.keys(difference).length, 1);
assert.equal(Object.keys(difference.name).length, 2);

assert.equal(difference.name.from, '1');
assert.equal(difference.name.from, "1");
assert.equal(difference.name.to, 1);
});
});

describe('multiple checks', () => {
it('should detect a nested property change', () => {
const testObjectA = { name: 'gary', age: 33, weight: { unit: 'kg', value: 80 } };
const testObjectB = { name: 'gary', age: 33, weight: { unit: 'kg', value: 79 } };
describe("multiple checks", () => {
it("should detect a nested property change", () => {
const testObjectA = {
name: "gary",
age: 33,
weight: { unit: "kg", value: 80 },
};
const testObjectB = {
name: "gary",
age: 33,
weight: { unit: "kg", value: 79 },
};
const difference = diffler(testObjectA, testObjectB);

assert.equal(Object.keys(difference).length, 1);
Expand All @@ -57,9 +65,17 @@ describe('getDiff', () => {
assert.equal(difference.weight.value.to, 79);
});

it('should detect multiple nested property change', () => {
const testObjectA = { name: 'gary', age: 33, weight: { unit: 'kg', value: 80 } };
const testObjectB = { name: 'gary', age: 34, weight: { unit: 'stone', value: 12.4 } };
it("should detect multiple nested property change", () => {
const testObjectA = {
name: "gary",
age: 33,
weight: { unit: "kg", value: 80 },
};
const testObjectB = {
name: "gary",
age: 34,
weight: { unit: "stone", value: 12.4 },
};
const difference = diffler(testObjectA, testObjectB);

assert.equal(Object.keys(difference).length, 2);
Expand All @@ -71,33 +87,37 @@ describe('getDiff', () => {
assert.equal(difference.weight.value.from, 80);
assert.equal(difference.weight.value.to, 12.4);

assert.equal(difference.weight.unit.from, 'kg');
assert.equal(difference.weight.unit.to, 'stone');
assert.equal(difference.weight.unit.from, "kg");
assert.equal(difference.weight.unit.to, "stone");
});
});

describe('property removals', () => {
it('should detect a single property removal as null', () => {
const testObjectA = { name: 'gary' };
describe("property removals", () => {
it("should detect a single property removal as null", () => {
const testObjectA = { name: "gary" };
const testObjectB = {};
const difference = diffler(testObjectA, testObjectB);

assert.equal(Object.keys(difference).length, 1);
assert.equal(Object.keys(difference.name).length, 2);

assert.equal(difference.name.from, 'gary');
assert.equal(difference.name.from, "gary");
assert.equal(difference.name.to, null);
});

it('should detect a nested property removal as null', () => {
const testObjectA = { name: 'gary', age: 33, weight: { unit: 'kg', value: 80 } };
const testObjectB = { name: 'gary', age: 33 };
it("should detect a nested property removal as null", () => {
const testObjectA = {
name: "gary",
age: 33,
weight: { unit: "kg", value: 80 },
};
const testObjectB = { name: "gary", age: 33 };
const difference = diffler(testObjectA, testObjectB);

assert.equal(Object.keys(difference).length, 1);
assert.equal(Object.keys(difference.weight).length, 2);

assert.deepEqual(difference.weight.from, { unit: 'kg', value: 80 });
assert.deepEqual(difference.weight.from, { unit: "kg", value: 80 });
assert.equal(difference.weight.to, null);
});
});
Expand Down
22 changes: 14 additions & 8 deletions tests/performance.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const benchmarker = require('./benchmarker');
const diffler = require('../src');
const metadatav3 = require('./data/json-metadata-v3');
const metadatav2 = require('./data/json-metadata-v2');
const benchmarker = require("./benchmarker");
const diffler = require("../src");
const metadatav3 = require("./data/json-metadata-v3");
const metadatav2 = require("./data/json-metadata-v2");

function suite() {
diffler(metadatav2, metadatav3);
}

const ops = benchmarker.bench10(suite);
const benchmark = 222587; // update value to set new benchmark
const benchmark = 217203; // update value to set new benchmark
console.info(`Executed ${ops} ops/s`);
const opsDiff = benchmark / ops;
const opsDiffAsPercentage = Math.round(opsDiff * 100);
Expand All @@ -19,9 +19,15 @@ if (opsDiffAsPercentageDifference === 0) {
return;
} else if (opsDiffAsPercentageDifference < -10) {
console.error(
`Bench complete: Code ran ${Math.abs(opsDiffAsPercentageDifference)}% slower, it's time to do something.`,
`Bench complete: Code ran ${Math.abs(
opsDiffAsPercentageDifference
)}% slower, it's time to do something.`
);
return;
}
const message = opsDiffAsPercentageDifference > 0 ? 'faster' : 'slower';
console.info(`Bench complete: Code ran ${Math.abs(opsDiffAsPercentageDifference)}% ${message} than benchmark.`);
const message = opsDiffAsPercentageDifference > 0 ? "faster" : "slower";
console.info(
`Bench complete: Code ran ${Math.abs(
opsDiffAsPercentageDifference
)}% ${message} than benchmark.`
);

0 comments on commit 87db1db

Please sign in to comment.