UNPKG

meows

Version:
65 lines (64 loc) 2.6 kB
"use strict"; // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ // Date and Time Object.defineProperty(exports, "__esModule", { value: true }); /** * Convert an integer from 1 to 12 to a month. * @example * numberToMonth(1) // => January * numberToMonth(12) // => December */ function numberToMonth(int) { if (int <= 0 || 12 < int || !Number.isSafeInteger(int)) { throw new TypeError(`number_to_month() requires an integer between 1 to 12, but received: ${int}.`); } return { 1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December', }[int]; } exports.numberToMonth = numberToMonth; /** A utility to store multiple timestamps for performance timing. * @example * const timer = new Timer() * timer.start('fetch starts') * timer.next('fetch resolved') * timer.end('awaiting done') */ class Timer { constructor() { /** The full state of the Timer object. */ this._log = []; /** A list of Unix timestamps. */ this._times = () => this._log.map(x => x.time); /** Add a midpoint to timer with optional label. */ this.next = (label = 'NEXT') => this._log.push({ label, time: Date.now() }); /** Start timer with optional label. */ this.start = (label = 'START') => this.next(label); /** Add endpoint to timer with optional label. */ this.end = (label = 'END') => this.next(label); /** range :: () → [integer tF - t0] || NaN if fewer than 2 timestamps */ this.range = (arr = this._log) => arr.length > 1 ? arr[arr.length - 1].time - arr[0].time : NaN; /** delta :: () → [ integer list of velocities ] || [] if fewer than 2 timestamps */ this.delta = ([head, ...tail] = this._times()) => this._log.length ? this._times() .reduce((state, n, i, arr) => state.concat(n - arr[i - 1]), []) .slice(1) : []; /** Returns array of relative difference of time from start, with head as initial timestamp. */ this.relative = ([head, ...tail] = this._times()) => this._log.length ? tail.reduce((state, n) => state.concat(n - head), [head]) : []; } } exports.Timer = Timer;