mirror of
https://github.com/HenShalom/json-version-control.git
synced 2026-05-18 06:06:07 -06:00
fix formatting to airbnb javascript
This commit is contained in:
parent
544eea4f11
commit
aedc77fdd4
2 changed files with 146 additions and 87 deletions
|
|
@ -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 };
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue