Created
April 12, 2018 14:42
-
-
Save iolloyd/529d87f8ffa27f5c8be8d8c906c38f20 to your computer and use it in GitHub Desktop.
booking a resource
This file contains hidden or 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
const flatten = arrays => [].concat.apply([], arrays); | |
const bookTime = (resource, time) => { | |
const newResource = resource.map(t => { | |
return (t.start < time.start && time.end < t.end) | |
? [{start: t.start, end: time.start}, {start: time.end, end: t.end}] | |
: t; | |
}); | |
return flatten(newResource); | |
}; | |
const makeBooking = (resource, time) => { | |
const newResource = bookTime(resource, time); | |
return { | |
ok: newResource.length === resource.length + 1, | |
resource: newResource | |
} | |
}; | |
const testResource = [ | |
{start: 1, end: 9} | |
]; | |
const booking = {start: 2, end: 8}; | |
const result = makeBooking(testResource, booking); | |
// No output is a good thing :-) | |
// | |
console.assert(result.ok === true); | |
console.assert(result.resource[0].start === 1); | |
console.assert(result.resource[0].end === 2); | |
console.assert(result.resource[1].start === 8); | |
console.assert(result.resource[1].end === 9); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment