UNPKG

nhb-toolbox

Version:

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

59 lines (58 loc) 1.54 kB
var _a; import { $BaseConverter } from './base.js'; import { UNITS } from './constants.js'; export class $Time extends $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 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); } } _a = $Time;