JSFiddle - React, Tailwind, and code Playground
by Kevin Faulhaber
JavaScript
/*
Created by Grimbode
Class DeepDifference
*/
function DeepDifference(o) {
this.arr = o.arr.sort(sortNumber) || {};
this.against = o.against.sort(sortNumber) || {};
//results stocked here
this.appended = [];
this.prepended = [];
this.removed = [];
this.lastPrependedIndex = 0;
};
DeepDifference.prototype.run = function () {
this.findPrepended();
this.findAppended();
this.findRemoved();
};
DeepDifference.prototype.findPrepended = function () {
for (var i = 0; i < this.arr.length; i++) {
for (var j = 0; j < this.against.length; j++) {
if(this.arr[i] == this.against[j]){
return false;
}else{
this.prepended.push(this.arr[i]);
this.lastPrependedIndex = i;
}
}
}
};
DeepDifference.prototype.findAppended = function () {
if(this.lastPrependedIndex < this.arr.length-1){
var temp = this.arr.slice(this.lastPrependedIndex + 1, this.arr.length);
for (var i = 0; i < temp; i++) {
for (var j = 0; j < this.against.length; j++) {
if(temp != this.against[j]){
this.appended.push(temp);
}
}
}
}
};
DeepDifference.prototype.findRemoved = function () {
};
DeepDifference.prototype.getAppended = function () {
return this.appended;
};
DeepDifference.prototype.getPrepended = function () {
return this.prepended;
};
DeepDifference.prototype.getRemoved = function () {
return this.removed;
};
DeepDifference.prototype.getResults = function () {
return [this.prepended, this.appended, this.removed];
};
//==============================================
function sortNumber(a, b) {
return a - b;
}
//==============================================