From aedc77fdd4d4bfe5a1347050982bd31435199aed Mon Sep 17 00:00:00 2001 From: Hen Shalom Date: Thu, 1 Mar 2018 22:21:48 +0200 Subject: [PATCH] fix formatting to airbnb javascript --- src/JsonDiff.js | 83 +++++++++++++---------- test/jsonDiff.test.js | 150 +++++++++++++++++++++++++++--------------- 2 files changed, 146 insertions(+), 87 deletions(-) diff --git a/src/JsonDiff.js b/src/JsonDiff.js index 8c57964..f075534 100644 --- a/src/JsonDiff.js +++ b/src/JsonDiff.js @@ -1,41 +1,56 @@ -function getJsonDiff(currentJson, newJson) { - let pathChange = []; - checkObjectTypeDiffAdd(currentJson, newJson, pathChange, ""); - checkObjectDiffDelete(currentJson, newJson, pathChange, ""); - return pathChange; +function checkArryTypeDiff(currentArray, newArray, changeList, currentPath) { + if (Array.isArray(newArray) && newArray !== undefined) { + for (let key = 0; key < newArray.length; key += 1) { + checkArryTypeDiff( + currentArray[key] ? currentArray[key] : [], + newArray[key], + changeList, + `${currentPath}.[${key}]`, + ); + } + } else if (currentArray !== newArray) { + changeList.push(`${Array.isArray(currentArray) ? 'Add' : 'Update'} ${currentPath}>${newArray}`); + } } function checkObjectTypeDiffAdd(currentJson, newJson, changeList, currentPath) { - if (typeof newJson == "object") { - for (let key of Object.keys(newJson)) - checkObjectTypeDiffAdd( - currentJson[key] ? currentJson[key] : {}, - newJson[key], - changeList, - `${currentPath}.${key}` - ); + if (typeof newJson === 'object') { + if (!Array.isArray(newJson)) { + Object.keys(newJson).forEach((key) => { + checkObjectTypeDiffAdd( + currentJson[key] ? currentJson[key] : {}, + newJson[key], + changeList, + `${currentPath}.${key}`, + ); + }); } else { - if (currentJson !== newJson) - changeList.push( - `${ - typeof currentJson === "object" ? "Add" : "Update" - } ${currentPath}:${newJson}` - ); - } -} -function checkObjectDiffDelete(currentJson, newJson, changeList, currentPath) { - if (typeof currentJson == "object") { - for (let key of Object.keys(currentJson)) - checkObjectDiffDelete( - currentJson[key], - newJson[key] ? newJson[key] : {}, - changeList, - `${currentPath}.${key}` - ); - } else { - if (currentJson !== newJson && typeof newJson === "object") - changeList.push(`Delete ${currentPath}:${currentJson}`); + checkArryTypeDiff(currentJson, newJson, changeList, currentPath); } + } else if (currentJson !== newJson) { + changeList.push(`${typeof currentJson === 'object' ? 'Add' : 'Update'} ${currentPath}:${newJson}`); + } } -export { getJsonDiff }; +function checkObjectDiffDelete(currentJson, newJson, changeList, currentPath) { + if (typeof currentJson === 'object') { + Object.keys(currentJson).forEach((key) => { + checkObjectDiffDelete( + currentJson[key], + newJson[key] ? newJson[key] : {}, + changeList, + `${currentPath}.${key}`, + ); + }); + } else if (currentJson !== newJson && typeof newJson === 'object') { + changeList.push(`Delete ${currentPath}:${currentJson}`); + } +} + +function getJsonDiff(currentJson, newJson) { + const pathChange = []; + checkObjectTypeDiffAdd(currentJson, newJson, pathChange, ''); + checkObjectDiffDelete(currentJson, newJson, pathChange, ''); + return pathChange; +} +export { getJsonDiff, checkObjectTypeDiffAdd, checkObjectDiffDelete, checkArryTypeDiff }; diff --git a/test/jsonDiff.test.js b/test/jsonDiff.test.js index 21a659e..c07f2d9 100644 --- a/test/jsonDiff.test.js +++ b/test/jsonDiff.test.js @@ -1,56 +1,100 @@ -import "babel-polyfill"; -import mocha from "mocha"; -import chai from "chai"; -import { getJsonDiff } from "../src/JsonDiff"; -const expect = chai.expect; +import 'babel-polyfill'; +import { describe, it } from 'mocha'; +import { expect } from 'chai'; +import { + getJsonDiff, + checkArryTypeDiff, + checkObjectTypeDiffAdd, + checkObjectDiffDelete, +} from '../src/JsonDiff'; -describe("Json Diff", function() { - describe("#getJsonDiff", function() { - it("should return the property diff between two object", function() { - let res = getJsonDiff({}, { color: "blue" }); - expect(res[0]).to.equal("Add .color:blue"); - }); - it("should return the property inside property diff between two object", function() { - let res = getJsonDiff({}, { color: { deep: "blue" } }); - expect(res[0]).to.equal("Add .color.deep:blue"); - }); - it("should return multi properties diff ", function() { - let res = getJsonDiff( - {}, - { color: { deep: "blue" }, shadow: "light" } - ); - expect(res).to.deep.equal([ - "Add .color.deep:blue", - "Add .shadow:light" - ]); - }); - it("should ignore properties that also exists in the old json ", function() { - let res = getJsonDiff( - { shadow: "light" }, - { color: { deep: "blue" }, shadow: "light" } - ); - expect(res).to.deep.equal(["Add .color.deep:blue"]); - }); - it("should not return anything when two json are the same ", function() { - let res = getJsonDiff( - { color: { deep: "blue" }, shadow: "light" }, - { color: { deep: "blue" }, shadow: "light" } - ); - expect(res).to.deep.equal([]); - }); - it("write update if value where changed ", function() { - let res = getJsonDiff( - { color: { deep: "blue" }, shadow: "light" }, - { color: { deep: "blue" }, shadow: "dark" } - ); - expect(res).to.deep.equal(["Update .shadow:dark"]); - }); - it("write Delete if the head is ahead ", function() { - let res = getJsonDiff( - { color: { deep: "blue" }, shadow: "light" }, - { color: { deep: "blue" } } - ); - expect(res).to.deep.equal(["Delete .shadow:light"]); - }); +describe('Json Diff', () => { + describe('#checkObjectTypeDiffAdd', () => { + it('should return the property diff between two object', () => { + const res = []; + checkObjectTypeDiffAdd({}, { color: 'blue' }, res, ''); + expect(res[0]).to.equal('Add .color:blue'); }); + it('should return the property inside property diff between two object', () => { + const res = []; + checkObjectTypeDiffAdd({}, { color: { deep: 'blue' } }, res, ''); + expect(res[0]).to.equal('Add .color.deep:blue'); + }); + it('should return multi properties diff ', () => { + const res = []; + checkObjectTypeDiffAdd({}, { color: { deep: 'blue' }, shadow: 'light' }, res, ''); + expect(res).to.deep.equal(['Add .color.deep:blue', 'Add .shadow:light']); + }); + it('should ignore properties that also exists in the old json ', () => { + const res = []; + checkObjectTypeDiffAdd( + { shadow: 'light' }, + { color: { deep: 'blue' }, shadow: 'light' }, + res, + '', + ); + expect(res).to.deep.equal(['Add .color.deep:blue']); + }); + it('should not return anything when two json are the same ', () => { + const res = []; + checkObjectTypeDiffAdd( + { color: { deep: 'blue' }, shadow: 'light' }, + { color: { deep: 'blue' }, shadow: 'light' }, + res, + '', + ); + expect(res).to.deep.equal([]); + }); + it('write update if value where changed ', () => { + const res = []; + checkObjectTypeDiffAdd( + { color: { deep: 'blue' }, shadow: 'light' }, + { color: { deep: 'blue' }, shadow: 'dark' }, + res, + '', + ); + expect(res).to.deep.equal(['Update .shadow:dark']); + }); + }); + describe('#checkObjectDiffDelete', () => { + it('write Delete if the head is ahead ', () => { + const res = []; + checkObjectDiffDelete( + { color: { deep: 'blue' }, shadow: 'light' }, + { color: { deep: 'blue' } }, + res, + '', + ); + expect(res).to.deep.equal(['Delete .shadow:light']); + }); + it('should return new element in array', () => { + const res = getJsonDiff( + { color: { deep: 'blue' }, shadow: 'light' }, + { color: { deep: 'blue' } }, + ); + expect(res).to.deep.equal(['Delete .shadow:light']); + }); + }); + describe('#checkArryTypeDiff', () => { + it('should return the array diff', () => { + const res = []; + checkArryTypeDiff([], ['1'], res, ''); + expect(res[0]).to.equal('Add .[0]>1'); + }); + it('should return the array update', () => { + const res = []; + checkArryTypeDiff(['3'], ['1'], res, ''); + expect(res[0]).to.equal('Update .[0]>1'); + }); + it('should return the diff of a complex array', () => { + const res = []; + checkArryTypeDiff([], [[1, 2], [3, 4]], res, ''); + expect(res).to.deep.equal([ + 'Add .[0].[0]>1', + 'Add .[0].[1]>2', + 'Add .[1].[0]>3', + 'Add .[1].[1]>4', + ]); + }); + }); });