Created
July 4, 2021 22:50
-
-
Save zubair1024/bb13aded9cad7ebe2819cd663332e1dc to your computer and use it in GitHub Desktop.
Figures out overlapping fault durations in a given timeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//TODO make sure all the Faults fall inside the window per ascending start and end time | |
const fault1 = { name: "fault1", timing: [0, 10] }; | |
const fault2 = { name: "fault2", timing: [5, 20] }; | |
const fault3 = { name: "fault3", timing: [10, 30] }; | |
const fault4 = { name: "fault4", timing: [70, 80] }; | |
let allFaults = [fault2, fault1, fault3, fault4]; | |
allFaults.sort((a, b) => { | |
return a.timing[1] - b.timing[1]; | |
}); | |
const getOverlappingFaults = (allFaults) => { | |
const overlappingFaults = []; | |
for (let i = 0; i < allFaults.length; i++) { | |
for (let j = i + 1; j < allFaults.length; j++) { | |
const comp1 = allFaults[i]; | |
const comp2 = allFaults[j]; | |
if (comp1 && comp2 && comp1.timing[1] > comp2.timing[0]) { | |
overlappingFaults.push([comp1, comp2]); | |
} | |
} | |
} | |
return overlappingFaults; | |
}; | |
const getNonOverlappingFaults = (allFaults, overlappingFaults) => { | |
const nonOverlappingFaults = allFaults.filter((item) => { | |
let found = false; | |
for (let i = 0; i < overlappingFaults.length; i++) { | |
const overlappingFault = overlappingFaults[i]; | |
if ( | |
overlappingFault[0].name === item.name || | |
overlappingFault[1].name === item.name | |
) { | |
found = true; | |
break; | |
} | |
} | |
return !found; | |
}); | |
return nonOverlappingFaults; | |
}; | |
const reduceOverlappingFaults = (array) => { | |
let tmp = []; | |
for (let i = 0; i < array.length; i++) { | |
const overlappingFaultComb = array[i]; | |
tmp.push({ | |
name: `${overlappingFaultComb[0].name}+${overlappingFaultComb[1].name}`, | |
timing: [ | |
overlappingFaultComb[0].timing[0], | |
overlappingFaultComb[1].timing[1], | |
], | |
}); | |
} | |
return tmp; | |
}; | |
const reduceResult = (allFaults) => { | |
const overlappingFaults = getOverlappingFaults(allFaults); | |
if (!overlappingFaults.length) { | |
return allFaults; | |
} | |
const nonOverlappingFaults = getNonOverlappingFaults( | |
allFaults, | |
overlappingFaults | |
); | |
const reducedOverlappingFaults = reduceOverlappingFaults(overlappingFaults); | |
const newArray = [...reducedOverlappingFaults, ...nonOverlappingFaults]; | |
reduceResult(newArray); | |
}; | |
const result = reduceResult(allFaults); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment