UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

90 lines (89 loc) 3.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.relativeTimePlugin = void 0; const constants_1 = require("../constants"); const relativeTimePlugin = ($Chronos) => { const { toNewDate } = $Chronos[constants_1.INTERNALS]; $Chronos.prototype.getRelativeYear = function (time) { const $date = this.toDate(); const now = toNewDate(this, time); let years = $date.getFullYear() - now.getFullYear(); const noYearMonthDay = now.getMonth() < $date.getMonth() || (now.getMonth() === $date.getMonth() && now.getDate() < $date.getDate()); if (noYearMonthDay) { years--; } return years; }; $Chronos.prototype.getRelativeMonth = function (time) { const $date = this.toDate(); const now = toNewDate(this, time); let months = ($date.getFullYear() - now.getFullYear()) * 12 + ($date.getMonth() - now.getMonth()); const hasNotHadMonthDay = now.getDate() < $date.getDate(); if (hasNotHadMonthDay) { months--; } return months; }; $Chronos.prototype.getRelativeWeek = function (time) { const relativeDays = this.getRelativeDay(time); return Math.floor(relativeDays / 7); }; $Chronos.prototype.getRelativeDay = function (time) { const now = toNewDate(this, time); now.setHours(0, 0, 0, 0); const $date = new Date(this.toDate()); $date.setHours(0, 0, 0, 0); const diffTime = $date.getTime() - now.getTime(); const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24)); return diffDays; }; $Chronos.prototype.getRelativeHour = function (time) { const diff = this.getTimeStamp() - toNewDate(this, time).getTime(); return Math.floor(diff / (1000 * 60 * 60)); }; $Chronos.prototype.getRelativeMinute = function (time) { const diff = this.getTimeStamp() - toNewDate(this, time).getTime(); return Math.floor(diff / (1000 * 60)); }; $Chronos.prototype.getRelativeSecond = function (time) { const diff = this.getTimeStamp() - toNewDate(this, time).getTime(); return Math.floor(diff / 1000); }; $Chronos.prototype.getRelativeMilliSecond = function (time) { return this.getTimeStamp() - toNewDate(this, time).getTime(); }; $Chronos.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 RangeError(`Unsupported time unit: ${unit}`); } }; $Chronos.prototype.isToday = function () { return this.getRelativeDay() === 0; }; $Chronos.prototype.isTomorrow = function () { return this.getRelativeDay() === 1; }; $Chronos.prototype.isYesterday = function () { return this.getRelativeDay() === -1; }; }; exports.relativeTimePlugin = relativeTimePlugin;