add check array diff with delete

This commit is contained in:
Hen Shalom 2018-03-10 22:23:22 +02:00
parent 61447d275a
commit dbe3e737a6
2 changed files with 48 additions and 10 deletions

View file

@ -1,7 +1,7 @@
function checkArryTypeDiff(currentArray, newArray, changeList, currentPath = '') {
function checkArryTypeDiffAdd(currentArray, newArray, changeList, currentPath = '') {
if (Array.isArray(newArray) && newArray !== undefined) {
for (let key = 0; key < newArray.length; key += 1) {
checkArryTypeDiff(
checkArryTypeDiffAdd(
currentArray[key] ? currentArray[key] : [],
newArray[key],
changeList,
@ -12,6 +12,20 @@ function checkArryTypeDiff(currentArray, newArray, changeList, currentPath = '')
changeList.push(`${Array.isArray(currentArray) ? 'Add' : 'Update'} ${currentPath}>${newArray}`);
}
}
function checkArryTypeDiffDelete(currentArray, newArray, changeList, currentPath = '') {
if (Array.isArray(currentArray) && currentArray !== undefined) {
for (let key = 0; key < currentArray.length; key += 1) {
checkArryTypeDiffDelete(
currentArray[key],
newArray[key] ? newArray[key] : [],
changeList,
`${currentPath}.[${key}]`,
);
}
} else if (currentArray !== newArray) {
changeList.push(`Delete ${currentPath}>${currentArray}`);
}
}
function checkObjectTypeDiffAdd(currentJson, newJson, changeList, currentPath = '') {
if (typeof newJson === 'object') {
@ -25,7 +39,7 @@ function checkObjectTypeDiffAdd(currentJson, newJson, changeList, currentPath =
);
});
} else {
checkArryTypeDiff(currentJson, newJson, changeList, currentPath);
checkArryTypeDiffAdd(currentJson, newJson, changeList, currentPath);
}
} else if (currentJson !== newJson) {
changeList.push(`${typeof currentJson === 'object' ? 'Add' : 'Update'} ${currentPath}:${newJson}`);
@ -53,4 +67,10 @@ function getJsonDiff(currentJson, newJson) {
checkObjectDiffDelete(currentJson, newJson, pathChange);
return pathChange;
}
export { getJsonDiff, checkObjectTypeDiffAdd, checkObjectDiffDelete, checkArryTypeDiff };
export {
getJsonDiff,
checkObjectTypeDiffAdd,
checkObjectDiffDelete,
checkArryTypeDiffAdd,
checkArryTypeDiffDelete,
};

View file

@ -3,9 +3,10 @@ import { describe, it } from 'mocha';
import { expect } from 'chai';
import {
getJsonDiff,
checkArryTypeDiff,
checkArryTypeDiffAdd,
checkObjectTypeDiffAdd,
checkObjectDiffDelete,
checkArryTypeDiffDelete,
} from '../src/JsonDiff';
describe('Json Diff', () => {
@ -71,20 +72,20 @@ describe('Json Diff', () => {
expect(res).to.deep.equal(['Delete .shadow:light']);
});
});
describe('#checkArryTypeDiff', () => {
it('should return the array diff', () => {
describe('#checkArryTypeDiffAdd', () => {
it('should return the array diff Add', () => {
const res = [];
checkArryTypeDiff([], ['1'], res);
checkArryTypeDiffAdd([], ['1'], res);
expect(res[0]).to.equal('Add .[0]>1');
});
it('should return the array update', () => {
const res = [];
checkArryTypeDiff(['3'], ['1'], res);
checkArryTypeDiffAdd(['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);
checkArryTypeDiffAdd([], [[1, 2], [3, 4]], res);
expect(res).to.deep.equal([
'Add .[0].[0]>1',
'Add .[0].[1]>2',
@ -93,4 +94,21 @@ describe('Json Diff', () => {
]);
});
});
describe('#checkArryTypeDiffDelete', () => {
it('should return the array diff delete', () => {
const res = [];
checkArryTypeDiffDelete(['1'], [], res);
expect(res[0]).to.equal('Delete .[0]>1');
});
it('should return the diff (Delete) of a complex array', () => {
const res = [];
checkArryTypeDiffDelete([[1, 2], [3, 4]], [], res);
expect(res).to.deep.equal([
'Delete .[0].[0]>1',
'Delete .[0].[1]>2',
'Delete .[1].[0]>3',
'Delete .[1].[1]>4',
]);
});
});
});