UNPKG

nhb-toolbox

Version:

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

63 lines (62 loc) 1.67 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.$Time = void 0; const base_1 = require("./base"); const constants_1 = require("./constants"); class $Time extends base_1.$BaseConverter { static #factors = { nanosecond: 1e-9, microsecond: 1e-6, millisecond: 1e-3, second: 1, minute: 60, hour: 3600, day: 86400, week: 604800, month: 2_629_746, year: 31_556_952, decade: 315_569_520, century: 3_155_695_200, millennium: 31_556_952_000, }; constructor(value, unit) { super(value, unit); } #toSeconds() { return this.value * _a.#factors[this.unit]; } to(target) { const baseInSeconds = this.#toSeconds(); return baseInSeconds / _a.#factors[target]; } toAll() { const inSeconds = this.#toSeconds(); const result = {}; for (const unit of constants_1.UNITS.time) { result[unit] = inSeconds / _a.#factors[unit]; } return result; } formatTo(target, options) { const value = this.to(target); const shortLabels = { nanosecond: 'ns', microsecond: 'µs', millisecond: 'ms', second: 's', minute: 'min', hour: 'h', day: 'd', week: 'wk', month: 'mo', year: 'yr', decade: 'dec', century: 'cen', millennium: 'mil', }; return this.$formatTo(value, target, shortLabels, options); } } exports.$Time = $Time; _a = $Time;