Skip to content

Instantly share code, notes, and snippets.

@acr13
Created February 6, 2017 18:20
Show Gist options
  • Save acr13/3ecf937ceca2c5a6143ca43ea7c31631 to your computer and use it in GitHub Desktop.
Save acr13/3ecf937ceca2c5a6143ca43ea7c31631 to your computer and use it in GitHub Desktop.
const range = (sh, sm = 0, eh = 0, em = 0) => ({
start: (sh * 100) + sm,
end: (eh * 100) + em
});
// const A = [ range(9, 0, 10, 0) ];
// const B = [ range(9, 0, 9, 30) ];
// const A = [ range(9, 0, 10, 0) ];
// const B = [ range(9, 15, 9, 30) ];
// const A = [ range(9, 0, 10, 0) ];
// const B = [ range(9, 0, 10, 0) ];
// const A = [ range(9, 0, 9, 30) ];
// const B = [ range(9, 30, 15) ];
// const A = [ range(9, 0, 9, 30), range(10, 0, 10, 30) ];
// const B = [ range(9, 15, 10, 15) ];
const A = [ range(9, 0, 11, 0), range(13, 0, 15, 0) ];
const B = [ range(9, 0, 9, 15), range(10, 0, 10, 15), range(12, 30, 16, 0) ];
// console.log(A);
// console.log(B);
// for each time range...
for (var i = 0; i < B.length; i++) {
var time = B[i];
for (var j = 0; j < A.length; j++) {
// case 1 - delete a block with the same start times
// if the end time is in this range, the start is now the end
// else delete this block (its all bad)
if (time.start === A[j].start) {
if (time.end < A[j].end) {
A[j].start = time.end;
} else {
A.splice(j, 1);
}
}
// case 2 - delete times that fall inside of a block
else if (time.start >= A[j].start && time.start <= A[j].end) {
// this start time is in this block, and ends past this block
// so we delete the rest of the block
if (time.end >= A[j].end) {
A[j].end = time.start;
}
// the end time falls in this block, so we have to splice
// this block into two different ones (taking 15mins out of an hour)
else {
// update this one first
var tempEnd = A[j].end;
A[j].end = time.start;
// add a new one
A.splice(j + 1, 0, { start: time.end, end: tempEnd });
}
}
else if (time.start >= A[j].start && time.end <= A[j].end) {
A[j].start = time.end;
}
// delete the whole block
else if (time.start <= A[j].start && time.end >= A[j].end) {
A.splice(j, 1);
}
}
}
console.log(A);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment