diff --git a/package-lock.json b/package-lock.json index 9ec3e386..e3050843 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,12 +21,12 @@ "classnames": "^2.2.6", "date-fns": "^3.6.0", "details-element-polyfill": "^2.3.0", + "diff": "^5.2.0", "docx": "5.0.0-rc4", "fast-array-diff": "^1.1.0", "file-saver": "^2.0.0", "immutable": "^3.8.2", "is-hotkey": "^0.2.0", - "jsondiffpatch": "^0.4.1", "jsonpath-plus": "^7.2.0", "lodash": "^4.17.21", "mdn-polyfills": "^5.15.0", @@ -2446,6 +2446,14 @@ "uuid": "^8.3.2" } }, + "node_modules/@ukhomeoffice/asl-components/node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/@ukhomeoffice/asl-components/node_modules/mustache": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/mustache/-/mustache-3.2.1.tgz", @@ -3992,18 +4000,13 @@ "integrity": "sha512-jnZ/m0+b1gz3EcooitqL7oDEkKHEro659dt8bWB/T/HjiILucoQhHvvi5MEOAIFJXxxO+rIYJ/t3qCgfUOSU5g==" }, "node_modules/diff": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", - "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", + "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", "engines": { "node": ">=0.3.1" } }, - "node_modules/diff-match-patch": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/diff-match-patch/-/diff-match-patch-1.0.5.tgz", - "integrity": "sha512-IayShXAgj/QMXgB0IWmKx+rOPuGMhqm5w6jvFxmVenXKIzRqTAAsbBPT3kWQeGANj3jGgvcvv4yK6SxqYmikgw==" - }, "node_modules/direction": { "version": "0.1.5", "resolved": "https://registry.npmjs.org/direction/-/direction-0.1.5.tgz", @@ -6663,21 +6666,6 @@ "node": ">=6" } }, - "node_modules/jsondiffpatch": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/jsondiffpatch/-/jsondiffpatch-0.4.1.tgz", - "integrity": "sha512-t0etAxTUk1w5MYdNOkZBZ8rvYYN5iL+2dHCCx/DpkFm/bW28M6y5nUS83D4XdZiHy35Fpaw6LBb+F88fHZnVCw==", - "dependencies": { - "chalk": "^2.3.0", - "diff-match-patch": "^1.0.0" - }, - "bin": { - "jsondiffpatch": "bin/jsondiffpatch" - }, - "engines": { - "node": ">=8.17.0" - } - }, "node_modules/jsonify": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.1.tgz", diff --git a/package.json b/package.json index 5a3da012..e0b7ca7e 100644 --- a/package.json +++ b/package.json @@ -36,12 +36,12 @@ "classnames": "^2.2.6", "date-fns": "^3.6.0", "details-element-polyfill": "^2.3.0", + "diff": "^5.2.0", "docx": "5.0.0-rc4", "fast-array-diff": "^1.1.0", "file-saver": "^2.0.0", "immutable": "^3.8.2", "is-hotkey": "^0.2.0", - "jsondiffpatch": "^0.4.1", "jsonpath-plus": "^7.2.0", "lodash": "^4.17.21", "mdn-polyfills": "^5.15.0", diff --git a/test/array-diff.js b/test/array-diff.js new file mode 100644 index 00000000..768c95f5 --- /dev/null +++ b/test/array-diff.js @@ -0,0 +1,72 @@ +const { diffChars } = require('diff'); + +const findArrayDifferences = (oldArray, newArray) => { + const changes = []; + + oldArray.forEach((oldStr, arrayIndex) => { + const newStr = newArray[arrayIndex] || ""; + + const diffResult = diffChars(oldStr, newStr); + + let charIndexOld = 0; + let charIndexNew = 0; + + const changeObj = { + arrayIndex, + added: [], + removed: [] + }; + + diffResult.forEach(part => { + if (part.added) { + changeObj.added.push({ + value: part.value, + charIndex: charIndexNew, + start: charIndexNew, + end: charIndexNew + part.value.length - 1 + }); + charIndexNew += part.value.length; + } else if (part.removed) { + changeObj.removed.push({ + value: part.value, + charIndex: charIndexOld, + start: charIndexOld, + end: charIndexOld + part.value.length - 1 + }); + charIndexOld += part.value.length; + } else { + charIndexOld += part.value.length; + charIndexNew += part.value.length; + } + }); + + changes.push(changeObj); + }); + + newArray.slice(oldArray.length).forEach((newStr, arrayIndex) => { + const globalArrayIndex = oldArray.length + arrayIndex; + const changeObj = { + arrayIndex: globalArrayIndex, + added: [], + removed: [] + }; + for (let charIndexNew = 0; charIndexNew < newStr.length; charIndexNew++) { + changeObj.added.push({ + value: newStr[charIndexNew], + charIndex: charIndexNew, + start: charIndexNew, + end: charIndexNew + }); + } + changes.push(changeObj); + }); + + return changes; +}; + +// Example usage: +const oldArray = ["Bingo", "Blue"]; +const newArray = ["Victory - kk", "BlueMarina", "Spain"]; + +const changes = findArrayDifferences(oldArray, newArray); +console.log(JSON.stringify(changes, null, 2));