UNPKG

@toreda/time

Version:

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

242 lines (241 loc) 8.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeData = void 0; const strong_types_1 = require("@toreda/strong-types"); const log_1 = require("@toreda/log"); const type_1 = require("./check/type"); const convert_1 = require("./convert"); const make_1 = require("./make"); const valid_1 = require("./valid"); /** * Internal state data created and wrapped by Time instances. */ class TimeData { constructor(units, value, log) { this.units = (0, strong_types_1.strongMake)(units); this.value = (0, strong_types_1.floatMake)(0, value); this.log = this.makeLog(log); } makeLog(log) { let classLog; if (!log || !(0, strong_types_1.typeMatch)(log, log_1.Log)) { classLog = new log_1.Log(); } else { classLog = log; } return classLog.makeLog('TimeData'); } /** * Get the current time value in instance's native time unit. * @returns */ get() { return this.value(); } set(caller, input) { const fnLog = this.log.makeLog('set'); if (input === null || input === undefined) { fnLog.error(`input arg is missing.`); return caller; } if (typeof input === 'number') { this.value(input); return caller; } if (!(0, valid_1.timeValid)(input)) { fnLog.error(`time arg failed validity check.`); return caller; } const updated = (0, convert_1.timeConvert)(input.units(), this.units(), input()); this.value(updated); return caller; } /** * Add number input to current value. * @param caller Time instance calling this method. * @param input number value to be added. * @returns Returns the Time instance which invoked the function * to support method chaining. */ addNumber(caller, input) { if (typeof input !== 'number') { const fnLog = this.log.makeLog('addNumber'); fnLog.error(`input arg is not a number.`); return caller; } const total = this.value() + input; this.set(caller, total); return caller; } /** * Subtract number input from the current value. * @param caller Time instance calling this method. * @param input number value to be subtracted. * @returns Returns the Time instance which invoked the function * to support method chaining. */ subNumber(caller, input) { if (typeof input !== 'number') { const fnLog = this.log.makeLog('subNumber'); fnLog.error(`input arg is not a number.`); return caller; } return this.addNumber(caller, input * -1); } /** * Get numeric unit value from a Time or number input. Time inputs are converted from * their native TimeUnit to the TimeUnit specified by the `convertTo` arg. * @param convertTo * @param input * @returns */ getUnitValue(convertTo, input) { if (input === null || input === undefined) { return null; } if (typeof input === 'number') { return input; } if (input.type !== 'Time') { return null; } return (0, convert_1.timeConvert)(input.units(), convertTo, input()); } /** * Convert value from specified units into object's native time units, then * subtract it from the current value. * @param caller Time instance calling this method. * @param input number value to be added. * @returns Returns the Time instance which invoked the function * to support method chaining. */ subUnit(caller, units, value, decimals) { const fnLog = this.log.makeLog('subUnit'); if (!units) { fnLog.error(`units arg missing.`); return caller; } if (typeof value !== 'number') { fnLog.error(`units arg is not a number.`); return caller; } const converted = (0, convert_1.timeConvert)(units, this.units(), value, decimals); if (converted === null) { fnLog.error(`bad timeConvert result for value.`); return caller; } return this.subNumber(caller, converted); } /** * Convert value from provided unit type into object's native time units and * add it to the current value. * @param caller Time instance calling this function to be returned by function. * @param units Time Unit of provided value. * @param value Value to be converted and added to current time. * @param decimals Number of decimals to include in final added value. * @returns */ addUnit(caller, units, value, decimals) { const fnLog = this.log.makeLog('addUnit'); if (value === null || value === undefined) { fnLog.error(`value arg missing`); return caller; } const converted = (0, convert_1.timeConvert)(units, this.units(), value, decimals); if (converted === null) { fnLog.error(`bad timeConvert result for value.`); return caller; } return this.addNumber(caller, converted); } /** * Invert current value's sign. * @param caller * @param posOnly * @returns */ invert(caller, posOnly) { const value = this.get(); // When posOnly flag is set only positive values // will be inverted. if (posOnly === true && value < 0) { return caller; } this.set(caller, value * -1); return caller; } timeSinceTime(target) { const fnLog = this.log.makeLog('timeSinceTime'); if (!(0, type_1.timeCheckType)(target)) { fnLog.error(`target arg did not pass time check type test. target is not a valid Time instance.`); return null; } const since = (0, convert_1.timeConvert)(target.units(), this.units(), target()); if (since === null) { fnLog.error(`bad timeConvert result for target.`); return null; } return this.timeSinceNumber(since); } timeSinceNumber(target) { const fnLog = this.log.makeLog('timeSinceNumber'); if (typeof target !== 'number') { fnLog.error(`target arg is not a number.`); return null; } if (target === 0) { return (0, make_1.timeMake)(this.units(), 0); } const result = this.value() - target; return (0, make_1.timeMake)(this.units(), result); } /** * Get time object containing time left until target time. May return * negative vamlue when target time is in the past. The returned time * object's time left value uses the same time units as the calling instance. * @param time * @returns */ timeUntilTime(time) { const fnLog = this.log.makeLog('timeUntilTime'); if (!(0, type_1.timeCheckType)(time)) { fnLog.error(`time arg did not pass type check and is not a valid Time instance.`); return null; } const target = (0, convert_1.timeConvert)(time.units(), 's', time()); if (target === null) { fnLog.error(`Bad timeConvertresult for time arg.`); return null; } return this.timeUntilNumber(target); } /** * Get time remaining until target unix timestamp. * @param target * @returns */ timeUntilNumber(target) { const fnLog = this.log.makeLog(`timeUntilNumber`); if (typeof target !== 'number') { fnLog.error(`target arg is not a number.`); return null; } if (target === 0) { return (0, make_1.timeMake)('s', 0); } const result = target - this.value(); return (0, make_1.timeMake)(this.units(), result); } /** * Reset internal state variables to their initial values. * @param caller * @returns */ reset(caller) { this.value.reset(); this.log.debug(`TimeData reset complete`); return caller; } } exports.TimeData = TimeData;