diff --git a/dist/index.js b/dist/index.js index 8f0e242..e2e0b10 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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 0) { if (tempDiff) { @@ -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]; diff --git a/tests/data/json-metadata-v1.js b/tests/data/json-metadata-v1.js index 370fb38..c22ff56 100644 --- a/tests/data/json-metadata-v1.js +++ b/tests/data/json-metadata-v1.js @@ -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', }; diff --git a/tests/data/json-metadata-v2.js b/tests/data/json-metadata-v2.js index ec9fc37..ab3ca64 100644 --- a/tests/data/json-metadata-v2.js +++ b/tests/data/json-metadata-v2.js @@ -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'], }, }, }; diff --git a/tests/data/json-metadata-v3.js b/tests/data/json-metadata-v3.js index 0a43fe0..44ca422 100644 --- a/tests/data/json-metadata-v3.js +++ b/tests/data/json-metadata-v3.js @@ -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'], }, }, }; diff --git a/tests/index.js b/tests/index.js index 858e142..ff69419 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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); @@ -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); @@ -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); }); }); diff --git a/tests/performance.js b/tests/performance.js index 06199be..c64f661 100644 --- a/tests/performance.js +++ b/tests/performance.js @@ -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); @@ -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.`);