UNPKG

@rashedmakkouk/dev-utils

Version:
72 lines (71 loc) 1.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TIME_SPANS = void 0; /** Utilities */ const isString_1 = __importDefault(require("lodash/isString")); const dayInSeconds = 60 * 60 * 24; /** * Time spans in seconds. */ exports.TIME_SPANS = { /* eslint-disable sort-keys */ ms: 1 / 1000, s: 1, m: 1 * 60, h: 1 * 60 * 60, d: 1 * dayInSeconds, w: 7 * dayInSeconds, y: 365 * dayInSeconds, }; // TODO: Refactor 'ms' implementation; Issue #2. /** * Parses a number representation or a string time period (e.g. 1h, 2d) to Unix * Timestamp. * * - ms: millisecond. * - s: second. * - h: hour. * - d: day. * - w: week. * - m: month. * - y: year. * * @remarks * If supplied 'span' is a number or a string representation of a number, value * is returned as is bypassing conversion. * * @returns Time representation in milliseconds, else parses value as integer. */ function ms(span, options = {}) { try { const { long } = options; if (!span) { return 0; } else if ((0, isString_1.default)(span) && isNaN(Number(span))) { /** Splits time value to `count` and `spanFormat` (e.g. '1h', '15m'). */ const [count, spanFormat] = span .split(/(\d+)/) .filter((value) => !!value); const timeSpan = exports.TIME_SPANS[spanFormat]; if (!timeSpan) { throw Error('Unsupported time span.'); } return parseInt(count, 10) * timeSpan * 1000; } else { return parseInt((0, isString_1.default)(span) ? span : `${span}`, 10); } } catch (error) { /** * - Invalid argument. * - Exception thrown. */ return 0; } } exports.default = ms;