From e3398767bee0989fef561f4740e2d6be7ba4223d Mon Sep 17 00:00:00 2001 From: malekhassan Date: Fri, 29 Apr 2022 18:23:16 +0300 Subject: [PATCH 1/3] add the sort property to check the array if the index sequence is sensitive or not --- README.md | 9 ++++--- src/index.vue | 15 +++++++++++- src/utils.js | 68 ++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f48a314..1610bd9 100644 --- a/README.md +++ b/README.md @@ -64,10 +64,11 @@ export default { ## Props -| Attribute | Level | Description | Type | Default | -| --------- | ----- | ----------- | --------------------------------------------- | ------- | -| oldData | basic | json data | object or object Array, {...}, [{...}, {...}] | - | -| newData | basic | json data | object or object Array, {...}, [{...}, {...}] | - | +| Attribute | Level | Description | Type | Default | +| --------- | ----- | ------------------------| --------------------------------------------- | ------- | +| oldData | basic | json data | object or object Array, {...}, [{...}, {...}] | {} | +| newData | basic | json data | object or object Array, {...}, [{...}, {...}] | {} | +| sort | basic | Boolean (True, flase) | Boolean | false | ## Events diff --git a/src/index.vue b/src/index.vue index adc48ff..284407e 100644 --- a/src/index.vue +++ b/src/index.vue @@ -21,7 +21,20 @@ import { mergeData, isArray } from './utils.js'; import Tree from './tree.vue'; export default { - props: ['oldData', 'newData'], + props:{ + oldData: { + type: Object, + default: {}, + }, + newData: { + type: Object, + default: {}, + }, + sort:{ + type: Boolean, + default:false + } + }, components: { Tree, }, diff --git a/src/utils.js b/src/utils.js index d05b614..c1f7b3e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -37,7 +37,10 @@ const isTheSametype = (a, b) => { ); }; -const mergeData = (_old, _new) => { +let sort = false; +const mergeData = (_old, _new,_sort) => { + // the checking to be sensitive for the index of the array or not + sort = _sort // finally result let result = []; // each line No. @@ -98,6 +101,40 @@ const mergeData = (_old, _new) => { }; }; + // return an array with repeted values + const getRepatedValues = (a, b) =>{ + let RepatedValues = [] + for (const akey in a) { + if (Object.hasOwnProperty.call(a, akey)) { + let A_eleValue = a[akey]; + for (const bkey in b) { + if (Object.hasOwnProperty.call(b, bkey)) { + let B_eleValue = b[bkey]; + if (A_eleValue === B_eleValue) { + RepatedValues.push(A_eleValue) + } + } + } + } + } + return RepatedValues + } + + // return the repated value in the ild or in hte new object and the status of this value if "del" or "add" + const getIsRepatedValue = (A_Object,B_Object,A_Value,B_Value,RepatedValues)=>{ + let objContainer = {} + if (RepatedValues.includes(A_Value)) { + objContainer.RepatedObj = A_Object + objContainer.notRepated = B_Object + objContainer.action = 'add' + }else{ + objContainer.RepatedObj = B_Object + objContainer.notRepated = A_Object + objContainer.action = 'del' + } + return objContainer; + } + // merge two vars to target,target type Array[{}] const parseData = (a, b, target) => { let _ar = Object.keys(a); @@ -109,14 +146,9 @@ const mergeData = (_old, _new) => { let _stl = _ar.filter((ak) => _br.some((bk) => bk === ak)); // new added keys let _add = _br.filter((bk) => !_ar.some((ak) => ak === bk)); - // push deleted keys - _del.forEach((key, index) => { - let needComma = true; - if (_stl.length === 0 && _add.length === 0 && index === _del.length - 1) { - needComma = false; - } - target.push(parseValue(key, a[key], showIndex, needComma, 'del')); - }); + // getting the repated values + let RepatedValues = getRepatedValues(a, b) + // The core function: compare _stl.forEach((key, index) => { let needComma = true; @@ -142,14 +174,28 @@ const mergeData = (_old, _new) => { // rewrite lastline _target.lastLine = start++; } else { - target.push(parseValue(key, a[key], showIndex, true, 'del')); - target.push(parseValue(key, b[key], showIndex, needComma, 'add')); + if (sort && RepatedValues.length > 0) { + let isRepatedValue = getIsRepatedValue(a,b,a[key],b[key],RepatedValues) + target.push(parseValue(key, isRepatedValue.RepatedObj[key], showIndex, needComma, 'none')); + target.push(parseValue(key, isRepatedValue.notRepated[key], showIndex, needComma, isRepatedValue.action)); + }else{ + target.push(parseValue(key, a[key], showIndex, true, 'del')); + target.push(parseValue(key, b[key], showIndex, needComma, 'add')); + } } } else { target.push(parseValue(key, a[key], showIndex, true, 'del')); target.push(parseValue(key, b[key], showIndex, needComma, 'add')); } }); + // push deleted keys + _del.forEach((key, index) => { + let needComma = true; + if (_stl.length === 0 && _add.length === 0 && index === _del.length - 1) { + needComma = false; + } + target.push(parseValue(key, a[key], showIndex, needComma, 'del')); + }); // push new keys _add.forEach((key, index) => { target.push( From 7ffa93c5e89e0ed33dbe5a954d46549c6871cb59 Mon Sep 17 00:00:00 2001 From: malekhassan Date: Sat, 30 Apr 2022 18:28:09 +0300 Subject: [PATCH 2/3] add the sort parameter to the function --- src/index.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.vue b/src/index.vue index 284407e..331b070 100644 --- a/src/index.vue +++ b/src/index.vue @@ -54,7 +54,7 @@ export default { methods: { isArray: isArray, updateView() { - this.mergeView = mergeData(this.oldData, this.newData); + this.mergeView = mergeData(this.oldData, this.newData, this.sort); }, }, mounted() { From ff63ef03f907df3c6324f402b9c9c874c493f88e Mon Sep 17 00:00:00 2001 From: Dario Ackermann Date: Sun, 26 Feb 2023 22:00:18 +0100 Subject: [PATCH 3/3] Fix bug in utils.js Fix incorrect diff results when items have only been reordered --- src/utils.js | 68 +++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/src/utils.js b/src/utils.js index c1f7b3e..87726c0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -33,13 +33,13 @@ const isComplexType = (param) => { const isTheSametype = (a, b) => { return ( - Object.prototype.toString.call(a) === Object.prototype.toString.call(b) + Object.prototype.toString.call(a) === Object.prototype.toString.call(b) ); }; let sort = false; const mergeData = (_old, _new,_sort) => { - // the checking to be sensitive for the index of the array or not + // the checking to be sensitive for the index of the array or not sort = _sort // finally result let result = []; @@ -101,9 +101,9 @@ const mergeData = (_old, _new,_sort) => { }; }; - // return an array with repeted values - const getRepatedValues = (a, b) =>{ - let RepatedValues = [] + // return an array with repeated values + const getRepeatedValues = (a, b) =>{ + let RepeatedValues = [] for (const akey in a) { if (Object.hasOwnProperty.call(a, akey)) { let A_eleValue = a[akey]; @@ -111,28 +111,30 @@ const mergeData = (_old, _new,_sort) => { if (Object.hasOwnProperty.call(b, bkey)) { let B_eleValue = b[bkey]; if (A_eleValue === B_eleValue) { - RepatedValues.push(A_eleValue) + RepeatedValues.push(A_eleValue) } } - } + } } } - return RepatedValues + return RepeatedValues } - // return the repated value in the ild or in hte new object and the status of this value if "del" or "add" - const getIsRepatedValue = (A_Object,B_Object,A_Value,B_Value,RepatedValues)=>{ + // return the repeated value in the ild or in hte new object and the status of this value if "del" or "add" + const getIsRepeatedValue = (A_Object,B_Object,A_Value,B_Value,RepeatedValues)=>{ let objContainer = {} - if (RepatedValues.includes(A_Value)) { - objContainer.RepatedObj = A_Object - objContainer.notRepated = B_Object - objContainer.action = 'add' - }else{ - objContainer.RepatedObj = B_Object - objContainer.notRepated = A_Object - objContainer.action = 'del' - } - return objContainer; + if (RepeatedValues.includes(A_Value)) { + objContainer.RepeatedObj = A_Object + objContainer.notRepeated = B_Object + objContainer.actionR = 'none' + objContainer.actionNR = !RepeatedValues.includes(B_Value) ? 'add' : null + }else{ + objContainer.RepeatedObj = B_Object + objContainer.notRepeated = A_Object + objContainer.actionR = 'add' + objContainer.actionNR = !RepeatedValues.includes(A_Value) ? 'del' : null + } + return objContainer; } // merge two vars to target,target type Array[{}] @@ -146,8 +148,8 @@ const mergeData = (_old, _new,_sort) => { let _stl = _ar.filter((ak) => _br.some((bk) => bk === ak)); // new added keys let _add = _br.filter((bk) => !_ar.some((ak) => ak === bk)); - // getting the repated values - let RepatedValues = getRepatedValues(a, b) + // getting the repeated values + let RepeatedValues = getRepeatedValues(a, b) // The core function: compare _stl.forEach((key, index) => { @@ -156,15 +158,16 @@ const mergeData = (_old, _new,_sort) => { needComma = false; } if (a[key] === b[key]) { + // values haven't changed target.push(parseValue(key, b[key], showIndex, needComma, 'none')); } else if (isTheSametype(a[key], b[key])) { if (isComplexType(b[key])) { let _target = parseValue( - key, - isArray(a[key]) ? [] : {}, - showIndex, - needComma, - 'none' + key, + isArray(a[key]) ? [] : {}, + showIndex, + needComma, + 'none' ); target.push(_target); // back one step @@ -174,10 +177,11 @@ const mergeData = (_old, _new,_sort) => { // rewrite lastline _target.lastLine = start++; } else { - if (sort && RepatedValues.length > 0) { - let isRepatedValue = getIsRepatedValue(a,b,a[key],b[key],RepatedValues) - target.push(parseValue(key, isRepatedValue.RepatedObj[key], showIndex, needComma, 'none')); - target.push(parseValue(key, isRepatedValue.notRepated[key], showIndex, needComma, isRepatedValue.action)); + if (sort && RepeatedValues.length > 0) { + let isRepeatedValue = getIsRepeatedValue(a,b,a[key],b[key],RepeatedValues) + target.push(parseValue(key, isRepeatedValue.RepeatedObj[key], showIndex, needComma, isRepeatedValue.actionR)); + if(isRepeatedValue.actionNR) + target.push(parseValue(key, isRepeatedValue.notRepeated[key], showIndex, needComma, isRepeatedValue.actionNR)); }else{ target.push(parseValue(key, a[key], showIndex, true, 'del')); target.push(parseValue(key, b[key], showIndex, needComma, 'add')); @@ -199,7 +203,7 @@ const mergeData = (_old, _new,_sort) => { // push new keys _add.forEach((key, index) => { target.push( - parseValue(key, b[key], showIndex, _add.length !== index + 1, 'add') + parseValue(key, b[key], showIndex, _add.length !== index + 1, 'add') ); }); };