Skip to content

Commit

Permalink
Merge pull request #27 from omgaz/patch
Browse files Browse the repository at this point in the history
[patch] fixes
  • Loading branch information
omgaz authored Apr 30, 2020
2 parents a7cb065 + 8199a45 commit 040c75b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 105 deletions.
8 changes: 0 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,4 @@
* @license MIT https://github.com/omgaz/diffler
* Author: Gary Chisholm @omgaz
*/
/**
* @preserve
* Read in two objects. Iterate over them and return the differences.
*
* @param {object} obj1 First object to compare from.
* @param {object} obj2 Second object to compare against obj1.
* @returns {object} Nested json object of changed properties containing a from and to key.
*/
function diffler(f,e){var o={};for(var r in f)if(f.hasOwnProperty(r)&&"function"!=typeof f[r]){var n=f[r],t=e[r];if(r in e)if("object"==typeof n){var i=diffler(n,t);0<Object.keys(i).length&&i&&(o[r]=i)}else n!==t&&(o[r]={from:n,to:t});else o[r]={from:n,to:null}}for(r in e)if(e.hasOwnProperty(r)&&"function"!=typeof e[r]){n=f[r],t=e[r];r in f||((o=o||{})[r]={from:null,to:t})}return o}module.exports=diffler;
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

/**
* @preserve
* Read in two objects. Iterate over them and return the differences.
*
* @param {object} obj1 First object to compare from.
Expand All @@ -16,7 +15,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 @@ -29,7 +28,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 @@ -50,7 +49,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'],
},
},
};
82 changes: 41 additions & 41 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
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", () => {
describe('multiple checks', () => {
it('should detect a nested property change', () => {
const testObjectA = {
name: "gary",
name: 'gary',
age: 33,
weight: { unit: "kg", value: 80 },
weight: { unit: 'kg', value: 80 },
};
const testObjectB = {
name: "gary",
name: 'gary',
age: 33,
weight: { unit: "kg", value: 79 },
weight: { unit: 'kg', value: 79 },
};
const difference = diffler(testObjectA, testObjectB);

Expand All @@ -65,16 +65,16 @@ describe("getDiff", () => {
assert.equal(difference.weight.value.to, 79);
});

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

Expand All @@ -87,37 +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", () => {
it('should detect a nested property removal as null', () => {
const testObjectA = {
name: "gary",
name: 'gary',
age: 33,
weight: { unit: "kg", value: 80 },
weight: { unit: 'kg', value: 80 },
};
const testObjectB = { name: "gary", age: 33 };
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
20 changes: 7 additions & 13 deletions tests/performance.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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);
Expand All @@ -19,15 +19,9 @@ 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 040c75b

Please sign in to comment.