@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
178 lines • 5.76 kB
JavaScript
/**
* @module TimeSpan
*/
import { parse, format } from "@lukeed/ms";
import {} from "../../serde/contracts/_module.js";
import { TO_MILLISECONDS, } from "../../time-span/contracts/_module.js";
import { UnexpectedError } from "../../utilities/_module.js";
/**
* The `TimeSpan` class is used for representing time interval.
* `TimeSpan` class cannot be negative, if you pass negative number it will be converted to 0.
*
* IMPORT_PATH: `"@daiso-tech/core/time-span"`
* @group Implementations
*/
export class TimeSpan {
milliseconds;
static secondInMilliseconds = 1000;
static minuteInMilliseconds = 60 * TimeSpan.secondInMilliseconds;
static hourInMilliseconds = 60 * TimeSpan.minuteInMilliseconds;
static dayInMilliseconds = 24 * TimeSpan.hourInMilliseconds;
static deserialize(serializedValue) {
return new TimeSpan(serializedValue.timeInMs);
}
constructor(milliseconds = 0) {
this.milliseconds = milliseconds;
this.milliseconds = Math.max(0, this.milliseconds);
}
equals(value) {
return value[TO_MILLISECONDS]() === this.toMilliseconds();
}
gt(value) {
return value[TO_MILLISECONDS]() < this.toMilliseconds();
}
gte(value) {
return value[TO_MILLISECONDS]() <= this.toMilliseconds();
}
lt(value) {
return value[TO_MILLISECONDS]() > this.toMilliseconds();
}
lte(value) {
return value[TO_MILLISECONDS]() >= this.toMilliseconds();
}
serialize() {
return {
version: "1",
timeInMs: this.toMilliseconds(),
};
}
/**
* Converts it to readable string
*/
toString() {
return format(this.milliseconds);
}
static fromMilliseconds(milliseconds) {
return new TimeSpan().addMilliseconds(milliseconds);
}
static fromSeconds(seconds) {
return new TimeSpan().addSeconds(seconds);
}
static fromMinutes(minutes) {
return new TimeSpan().addMinutes(minutes);
}
static fromHours(hours) {
return new TimeSpan().addHours(hours);
}
static fromDays(days) {
return new TimeSpan().addDays(days);
}
static fromTimeSpan(timeSpan) {
return new TimeSpan().addTimeSpan(timeSpan);
}
static fromDateRange({ start = new Date(), end = new Date(), }) {
return new TimeSpan().addMilliseconds(end.getTime() - start.getTime());
}
/**
* Create a `TimeSpan` from `string`
*
* @example
* ```ts
* // Will be 5000 milliseconds.
* TimeSpan.fromStr("5s").toMilliseconds()
* ```
*
* Under the hood, this method leverages [@lukeed/ms](https://www.npmjs.com/package/@lukeed/ms) package to convert various time formats into milliseconds.
* Refer to its documentation for a complete list of supported time formats and units.
*/
static fromStr(timeAsStr) {
const timeInMs = parse(timeAsStr);
if (timeInMs === undefined) {
throw new UnexpectedError("Passed in invalid string format to TimeSpan.fromStr");
}
return new TimeSpan().addMilliseconds(timeInMs);
}
addMilliseconds(milliseconds) {
return new TimeSpan(this.toMilliseconds() + milliseconds);
}
addSeconds(seconds) {
return this.addMilliseconds(TimeSpan.secondInMilliseconds * seconds);
}
addMinutes(minutes) {
return this.addMilliseconds(TimeSpan.minuteInMilliseconds * minutes);
}
addHours(hours) {
return this.addMilliseconds(TimeSpan.hourInMilliseconds * hours);
}
addDays(days) {
return this.addMilliseconds(TimeSpan.dayInMilliseconds * days);
}
addTimeSpan(timeSpan) {
return this.addMilliseconds(timeSpan[TO_MILLISECONDS]());
}
subtractMilliseconds(milliseconds) {
return new TimeSpan(this.toMilliseconds() - milliseconds);
}
subtractSeconds(seconds) {
return this.subtractMilliseconds(TimeSpan.secondInMilliseconds * seconds);
}
subtractMinutes(minutes) {
return this.subtractMilliseconds(TimeSpan.minuteInMilliseconds * minutes);
}
subtractHours(hours) {
return this.subtractMilliseconds(TimeSpan.hourInMilliseconds * hours);
}
subtractDays(days) {
return this.subtractMilliseconds(TimeSpan.dayInMilliseconds * days);
}
subtractTimeSpan(timeSpan) {
return this.subtractMilliseconds(timeSpan[TO_MILLISECONDS]());
}
multiply(value) {
return new TimeSpan(Math.round(value * this.toMilliseconds()));
}
divide(value) {
return new TimeSpan(Math.round(this.toMilliseconds() / value));
}
[TO_MILLISECONDS]() {
return this.milliseconds;
}
toMilliseconds() {
return this[TO_MILLISECONDS]();
}
toSeconds() {
return Math.floor(this.milliseconds / TimeSpan.secondInMilliseconds);
}
toMinutes() {
return Math.floor(this.milliseconds / TimeSpan.minuteInMilliseconds);
}
toHours() {
return Math.floor(this.milliseconds / TimeSpan.hourInMilliseconds);
}
toDays() {
return Math.floor(this.milliseconds / TimeSpan.dayInMilliseconds);
}
/**
* Will return endDate relative to a given `startDate` argument.
*
* @default
* ```ts
* new Date()
* ```
*/
toEndDate(startDate = new Date()) {
return new Date(startDate.getTime() + this.toMilliseconds());
}
/**
* Will return startDate relative to a given `endDate` argument.
*
* @default
* ```ts
* new Date()
* ```
*/
toStartDate(endDate = new Date()) {
return new Date(endDate.getTime() - this.toMilliseconds());
}
}
//# sourceMappingURL=time-span.js.map