event-time-utils
A collection of utilities based around events with begins/ends timestamps
Installation
Install the package with npm:
npm install event-time-utils
Usage
These utilities are intended to be used with any objects that have a begins
/ ends
timestamp,
formatted as a millisecond integer.
chronoEventsComparer
chronoEventsComparer
is used for sorting events by start time:
let events = title: 'Dinner' begins: + ends: + title: 'Breakfast' begins: + ends: + title: 'Lunch' begins: + ends: + let sortedEvents = ...events console// [{title: 'Breakfast', ...}, {title: 'Lunch', ...}, {title: 'Dinner', ...}]
activeTime
activeTime
tells you the amount of total time spent within a set of events, given a begins
and
ends
bound. Overlapping times are only counted once.
For this function to work, events require a key-serialisable unique id
attribute. The time value
is returned as a simplified two decimal place string count of hours.
let events = id: 1 title: 'Eat Breakfast' begins: + ends: + id: 2 title: 'Shower' begins: + ends: + id: 3 title: 'Brush Teeth' begins: + ends: + let dayBegins = +let dayEnds = +let time = console// '0.75'
eventsInRange
eventsInRange
returns a filtered set of events that fall within a specified begins
and ends
bound.
let events = title: 'Lunch Yesterday' begins: + ends: + title: 'Lunch Today' begins: + ends: + title: 'Lunch Tomorrow' begins: + ends: + let dayBegins = +let dayEnds = +let todaysEvents = console// [{title: 'Lunch Today', ...}]
stackEvents
stackEvents
returns a mutated set of events with an additional stackIndex
attribute, with any
overlapping events placed on a higher stack.
The input events can be supplied with their own stackIndex
attribute, so all other events will
stack around them.
Optionally, you can provide a margin
to which events will stack even if they don't overlap.
let events = title: 'Eat Breakfast' begins: + ends: + title: 'Shower' begins: + ends: + title: 'Brush Teeth' begins: + ends: + let stackedEvents = console// shower stack: 0, brush teeth stack: 1
nearestTime
nearestTime
rounds a given time value to the nearest given time unit.
let vagueTime = +let roundedTime = console// 2018-05-15T07:07:00+10:00 rounded to: 2018-05-15T07:00:00+10:00