UNPKG

@toreda/time

Version:

Simple, small footprint library for common time operations and converting between units of time.

43 lines (42 loc) 1.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeUtils = void 0; const defaults_1 = require("../defaults"); const supported_1 = require("./unit/supported"); const MIN_DECIMALS = 0; const MAX_DECIMALS = 100; class TimeUtils { static withinSafeRange(n) { return n >= Number.MIN_SAFE_INTEGER && n <= Number.MAX_SAFE_INTEGER; } /** * Check whether timeConvert can convert between the provided units. * @param from * @param to * @param value * @returns */ static canConvert(from, to, value) { if (!(0, supported_1.timeUnitSupported)(from) || !(0, supported_1.timeUnitSupported)(to)) { return false; } if (typeof value !== 'number' || !isFinite(value)) { return false; } return TimeUtils.withinSafeRange(value); } static resolveDecimals(decimals) { if (typeof decimals !== 'number' || !isFinite(decimals)) { return defaults_1.Defaults.Math.Precision.Base; } if (decimals < MIN_DECIMALS || decimals > MAX_DECIMALS) { return defaults_1.Defaults.Math.Precision.Base; } return Math.floor(decimals); } static roundToDecimals(value, decimals) { const factor = Math.pow(10, decimals); return Math.round(value * factor) / factor; } } exports.TimeUtils = TimeUtils;