UNPKG

@onesy/date

Version:

Time and date utils library

47 lines (46 loc) 2.04 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const OnesyDate_1 = __importStar(require("./OnesyDate")); function ago(onesyDate = OnesyDate_1.default.onesyDate, withSufix = true) { const now = new OnesyDate_1.default(); if (onesyDate.valid && onesyDate.milliseconds < now.milliseconds) { let value = ''; const ago_ = { milliseconds: now.milliseconds - onesyDate.milliseconds, }; ago_['second'] = Math.floor(ago_['milliseconds'] / 1e3); ago_['minute'] = Math.floor(ago_['second'] / 6e1); ago_['hour'] = Math.floor(ago_['minute'] / 6e1); ago_['day'] = Math.floor(ago_['hour'] / 24); ago_['month'] = Math.floor(ago_['day'] / 30); ago_['year'] = Math.floor(ago_['month'] / 12); OnesyDate_1.units.forEach(unit => { if (ago_[unit] > 0) value = ago_[unit] === 1 ? `a${unit === 'hour' ? 'n' : ''} ${unit}` : `${ago_[unit]} ${unit}s`; if (unit === 'second' && ago_[unit] < 14) value = `a few seconds`; }); return `${value}${withSufix ? ' ago' : ''}`; } } exports.default = ago;