2021-05-24 03:02:07 +00:00
|
|
|
import { SerializationError } from "./Errors"
|
|
|
|
|
|
|
|
|
2021-05-22 01:36:42 +00:00
|
|
|
/**
|
|
|
|
* An half-line of time, defined by a `date` and a boolean `isBefore` indicating if the time before or after the
|
|
|
|
* specified date should be selected.
|
|
|
|
*/
|
|
|
|
export default class TimeRay {
|
|
|
|
isBefore
|
|
|
|
date
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param isBefore - `true` to select times earlier than the date, `false` to select times after the date.
|
|
|
|
* @param date - The date to start measurements from.
|
|
|
|
*/
|
|
|
|
constructor(isBefore, date) {
|
|
|
|
this.isBefore = isBefore
|
|
|
|
this.date = date
|
|
|
|
}
|
|
|
|
|
2021-05-24 03:02:07 +00:00
|
|
|
static rawRegex = /^(?<isBefore>[><]) (?<date>.+)$/
|
|
|
|
|
|
|
|
static fromRaw(data) {
|
|
|
|
const match = this.rawRegex.exec(data)
|
|
|
|
|
|
|
|
if(!match) throw new SerializationError(data)
|
|
|
|
|
|
|
|
const isBefore = match.groups.isBefore === "<"
|
|
|
|
const date = new Date(match.groups.date)
|
|
|
|
return new TimeRay(isBefore, date)
|
|
|
|
}
|
|
|
|
|
2021-05-22 01:36:42 +00:00
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
toString() {
|
|
|
|
return `${this.isBefore ? "<" : ">"} ${this.date.toISOString()}`
|
|
|
|
}
|
2021-05-22 02:32:47 +00:00
|
|
|
|
|
|
|
includes(date) {
|
2021-05-30 15:59:31 +00:00
|
|
|
return Boolean((date > this.date) ^ this.isBefore)
|
2021-05-22 02:32:47 +00:00
|
|
|
}
|
2021-05-22 01:36:42 +00:00
|
|
|
}
|