Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Created April 12, 2018 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iolloyd/529d87f8ffa27f5c8be8d8c906c38f20 to your computer and use it in GitHub Desktop.
Save iolloyd/529d87f8ffa27f5c8be8d8c906c38f20 to your computer and use it in GitHub Desktop.
booking a resource
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