nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
97 lines (96 loc) • 4.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.relativeTimePlugin = void 0;
const constants_1 = require("../constants");
/** * Plugin to inject `relative time` related methods */
const relativeTimePlugin = (ChronosClass) => {
ChronosClass.prototype.getRelativeYear = function (time) {
const inputDate = ChronosClass[constants_1.INTERNALS].internalDate(this);
const now = ChronosClass[constants_1.INTERNALS].toNewDate(this, time);
let years = inputDate.getFullYear() - now.getFullYear();
const noYearMonthDay = now.getMonth() < inputDate.getMonth() ||
(now.getMonth() === inputDate.getMonth() &&
now.getDate() < inputDate.getDate());
if (noYearMonthDay) {
years--;
}
return years;
};
ChronosClass.prototype.getRelativeMonth = function (time) {
const now = ChronosClass[constants_1.INTERNALS].toNewDate(this, time);
const inputDate = ChronosClass[constants_1.INTERNALS].internalDate(this);
let months = (inputDate.getFullYear() - now.getFullYear()) * 12 +
(inputDate.getMonth() - now.getMonth());
const hasNotHadMonthDay = now.getDate() < inputDate.getDate();
if (hasNotHadMonthDay) {
months--;
}
return months;
};
ChronosClass.prototype.getRelativeWeek = function (time) {
const relativeDays = this.getRelativeDay(time);
return Math.floor(relativeDays / 7);
};
ChronosClass.prototype.getRelativeDay = function (time) {
const today = ChronosClass[constants_1.INTERNALS].toNewDate(this, time);
// Set the time of today to 00:00:00 for comparison purposes
today.setHours(0, 0, 0, 0);
// Normalize the input date to 00:00:00
const inputDate = ChronosClass[constants_1.INTERNALS].internalDate(this);
inputDate.setHours(0, 0, 0, 0);
const diffTime = inputDate.getTime() - today.getTime();
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
return diffDays;
};
ChronosClass.prototype.getRelativeHour = function (time) {
const diff = ChronosClass[constants_1.INTERNALS].internalDate(this).getTime() -
ChronosClass[constants_1.INTERNALS].toNewDate(this, time).getTime();
return Math.floor(diff / (1000 * 60 * 60));
};
ChronosClass.prototype.getRelativeMinute = function (time) {
const diff = ChronosClass[constants_1.INTERNALS].internalDate(this).getTime() -
ChronosClass[constants_1.INTERNALS].toNewDate(this, time).getTime();
return Math.floor(diff / (1000 * 60));
};
ChronosClass.prototype.getRelativeSecond = function (time) {
const diff = ChronosClass[constants_1.INTERNALS].internalDate(this).getTime() -
ChronosClass[constants_1.INTERNALS].toNewDate(this, time).getTime();
return Math.floor(diff / 1000);
};
ChronosClass.prototype.getRelativeMilliSecond = function (time) {
return (ChronosClass[constants_1.INTERNALS].internalDate(this).getTime() -
ChronosClass[constants_1.INTERNALS].toNewDate(this, time).getTime());
};
ChronosClass.prototype.compare = function (unit = 'minute', time) {
switch (unit) {
case 'year':
return this.getRelativeYear(time);
case 'month':
return this.getRelativeMonth(time);
case 'day':
return this.getRelativeDay(time);
case 'week':
return this.getRelativeWeek(time);
case 'hour':
return this.getRelativeHour(time);
case 'minute':
return this.getRelativeMinute(time);
case 'second':
return this.getRelativeSecond(time);
case 'millisecond':
return this.getRelativeMilliSecond(time);
default:
throw new Error(`Unsupported time unit: ${unit}`);
}
};
ChronosClass.prototype.isToday = function () {
return this.getRelativeDay() === 0;
};
ChronosClass.prototype.isTomorrow = function () {
return this.getRelativeDay() === 1;
};
ChronosClass.prototype.isYesterday = function () {
return this.getRelativeDay() === -1;
};
};
exports.relativeTimePlugin = relativeTimePlugin;