Skip to content

Instantly share code, notes, and snippets.

@julienetie
Last active May 14, 2021 20:49
Show Gist options
  • Save julienetie/047310f89c21a8874d45c92251c07cf1 to your computer and use it in GitHub Desktop.
Save julienetie/047310f89c21a8874d45c92251c07cf1 to your computer and use it in GitHub Desktop.
Diff Array test
Array.diff = function(left, right)
{
var o = left;
var n = right;
var ns = {};
var os = {};
for (var i = 0; i < n.length; i++) {
if (ns[n[i]] == null)
ns[n[i]] = { rows: [], o: null };
ns[n[i]].rows.push(i);
}
for (var i = 0; i < o.length; i++) {
if (os[o[i]] == null)
os[o[i]] = { rows: [], n: null };
os[o[i]].rows.push(i);
}
for (var i in ns) {
if (ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1) {
n[ns[i].rows[0]] = { text: n[ns[i].rows[0]], row: os[i].rows[0] };
o[os[i].rows[0]] = { text: o[os[i].rows[0]], row: ns[i].rows[0] };
}
}
for (var i = 0; i < n.length - 1; i++) {
if (n[i].text != null && n[i + 1].text == null && n[i].row + 1 < o.length && o[n[i].row + 1].text == null && n[i + 1] == o[n[i].row + 1]) {
n[i + 1] = { text: n[i + 1], row: n[i].row + 1 };
o[n[i].row + 1] = { text: o[n[i].row + 1], row: i + 1 };
}
}
for (var i = n.length - 1; i > 0; i--) {
if (n[i].text != null && n[i - 1].text == null && n[i].row > 0 && o[n[i].row - 1].text == null &&
n[i - 1] == o[n[i].row - 1]) {
n[i - 1] = { text: n[i - 1], row: n[i].row - 1 };
o[n[i].row - 1] = { text: o[n[i].row - 1], row: i - 1 };
}
}
return { left: o, right: n };
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse odio nisi, sollicitudin nec augue nec, gravida aliquet ex. Sed vel elementum lacus. Sed vitae tristique mauris, sed viverra erat. Integer porttitor elit nec purus rutrum, vel imperdiet orci mollis. Fusce volutpat turpis non justo vehicula, sit amet lobortis odio viverra. Nullam imperdiet vitae justo ut tincidunt. Vivamus convallis tristique neque, sit amet efficitur odio rhoncus ut. Nam ac magna quis mauris mollis commodo. Duis vestibulum ornare velit, id molestie arcu varius quis. Vestibulum ante ipsum primis in faucibus orci luctus #et ultrices posuere cubilia curae;

Pellentesque a nibh tellus. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras tempus ipsum vel ligula gravida, quis vulputate metus condimentum. Nullam tortor turpis, ullamcorper sit amet viverra vitae, pellentesque a justo. Quisque malesuada purus non augue suscipit imperdiet. Nulla aliquam ante non neque porttitor, at maximus mi feugiat. Donec pretium dignissim tortor sit amet ornare. Suspendisse potenti. Maecenas vel erat in eros imperdiet laoreet sagittis eu turpis. Quisque commodo ornare lorem sed eleifend. Lorem ips um dolor sit amet, consectetur adipiscing elit.

console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
console.log('hello world')
MIT License
Copyright (c) 2019 Julien Etienne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment