date-clashes
A simple API for detecting overlapping dates
Installation
npm install date-clashes --save
Usage
var myDates = [
{ s: , e: , id: 1 },
{ s: , e: , id: 2 },
...
];
var DateClash = require("date-clashes");
var clash = new DateClash.Clash(date => { return { start: date.s, end: date.e, id: date.id } });
var clashes = clash.flatten(myDates);
var clashes = clash.flatten(myDates, { startDay: 1 , endDay: 0 });
{
start: Tue Sep 15 2015 00:00:00 GMT+0800 (W. Australia Standard Time),
end: Thu Sep 24 2015 00:00:00 GMT+0800 (W. Australia Standard Time),
'1': { date: Tue Sep 15 2015 00:00:00 GMT+0800 (W. Australia Standard Time),
clashes: [ ... ] },
...
'10': { date: Tue Sep 24 2015 00:00:00 GMT+0800 (W. Australia Standard Time),
clashes: [ ... ] },
}
interface Clashes {
start: Date;
end: Date;
[dayNumber: number]: { date: Date, clashes: Range[] }
}
interface Range {
start: Date;
end: Date;
[key: string]: any;
}
API
Constructor
constructor(rangeGetter?: RangeGetter);
RangeGetter
A function that takes an object and returns an object that date-clashes
can understand (Range
)
The object must contain a start
and end
property which must be Date
objects.
Example:
function rangeGetter(myObject) {
return {
start: new Date(myObject.startDate),
end: new Date(myObject.endDate),
id: myObject.id,
firstName: myObject.firstName,
lastName: myObject.lastName
};
}
var clash = new DateClash.Clash(rangeGetter);
Flatten
The function that does all of the work.
Takes an array of objects that your rangeGetter
function can parse.
function flatten(dates: Array<any>, options?: Options) => Clashes;
Options
An optional object that has two optional properties:
startDay
and endDay
These options will determine the day number of outer-extremities of the returned Clashes
object.
{
startDay: number,
endDay: number
}
License
MIT