UNPKG

@barchart/common-js

Version:
316 lines (310 loc) 9.23 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Time_exports = {}; __export(Time_exports, { default: () => Time }); module.exports = __toCommonJS(Time_exports); var assert = __toESM(require("./assert.js")); var is = __toESM(require("./is.js")); const SECONDS_PER_MINUTE = 60; const MINUTES_PER_HOUR = 60; const HOURS_PER_DAY = 24; class Time { #hours; #minutes; #seconds; /** * @param {number} hours * @param {number} minutes * @param {number} seconds */ constructor(hours, minutes, seconds) { if (!Time.validate(hours, minutes, seconds)) { throw new Error(`Unable to instantiate [ Time ], input is invalid [ ${hours} ], [ ${minutes} ], [ ${seconds} ]`); } this.#hours = hours; this.#minutes = minutes; this.#seconds = seconds; } /** * The hours (0–23). * * @public * @returns {number} */ get hours() { return this.#hours; } /** * The minutes (0–59). * * @public * @returns {number} */ get minutes() { return this.#minutes; } /** * The seconds (0–59). * * @public * @returns {number} */ get seconds() { return this.#seconds; } /** * @public * @param {*} seconds * @returns {Time} */ addSeconds(seconds) { assert.argumentIsValid(seconds, "seconds", is.integer, "must be an integer"); let negative = seconds < 0; let secondsToAdd = seconds % SECONDS_PER_MINUTE; let minutesToAdd = seconds / SECONDS_PER_MINUTE % MINUTES_PER_HOUR; let hoursToAdd = seconds / (SECONDS_PER_MINUTE * MINUTES_PER_HOUR) % HOURS_PER_DAY; if (negative) { minutesToAdd = Math.ceil(minutesToAdd); hoursToAdd = Math.ceil(hoursToAdd); } else { minutesToAdd = Math.floor(minutesToAdd); hoursToAdd = Math.floor(hoursToAdd); } let secondsShifted = this.#seconds + secondsToAdd; if (negative && secondsShifted < 0) { secondsShifted += SECONDS_PER_MINUTE; minutesToAdd--; } if (!negative && !(secondsShifted < SECONDS_PER_MINUTE)) { secondsShifted -= SECONDS_PER_MINUTE; minutesToAdd++; } let minutesShifted = this.#minutes + minutesToAdd; if (negative && minutesShifted < 0) { minutesShifted += MINUTES_PER_HOUR; hoursToAdd--; } if (!negative && !(minutesShifted < MINUTES_PER_HOUR)) { minutesShifted -= MINUTES_PER_HOUR; hoursToAdd++; } let hoursShifted = (this.#hours + hoursToAdd) % HOURS_PER_DAY; if (hoursShifted < 0) { hoursShifted += HOURS_PER_DAY; } return new Time(hoursShifted, minutesShifted, secondsShifted); } /** * Returns a new {@link Time} instance with some number of seconds subtracted. * * @public * @param {number} seconds * @returns {Time} */ subtractSeconds(seconds) { return this.addSeconds(~seconds + 1); } /** * Returns a new {@link Time} instance with some number of minutes added. * * @public * @param {number} minutes * @returns {Time} */ addMinutes(minutes) { return this.addSeconds(minutes * SECONDS_PER_MINUTE); } /** * Returns a new {@link Time} instance with some number of minutes subtracted. * * @public * @param {number} minutes * @returns {Time} */ subtractMinutes(minutes) { return this.addMinutes(~minutes + 1); } /** * Returns a new {@link Time} instance with some number of minutes added. * * @public * @param {number} hours * @returns {Time} */ addHours(hours) { return this.addMinutes(hours * MINUTES_PER_HOUR); } /** * Returns a new {@link Time} instance with some number of minutes subtracted. * * @public * @param {number} hours * @returns {Time} */ subtractHours(hours) { return this.addHours(~hours + 1); } /** * Indicates if the current {@link Time} instance is before another time. * * @public * @param {Time} other * @returns {boolean} */ getIsBefore(other) { assert.argumentIsRequired(other, "other", Time, "Time"); return this.hours < other.hours || this.hours === other.hours && this.minutes < other.minutes || this.hours === other.hours && this.minutes === other.minutes && this.seconds < other.seconds; } /** * Indicates if the current {@link Time} instance is after another time. * * @public * @param {Time} other * @returns {boolean} */ getIsAfter(other) { assert.argumentIsRequired(other, "other", Time, "Time"); return !this.getIsBefore(other) && !this.getIsEqual(other); } /** * Indicates if the current {@link Time} instance is the same as another time. * * @public * @param {Time} other * @returns {boolean} */ getIsEqual(other) { assert.argumentIsRequired(other, "other", Time, "Time"); return this.#hours === other.hours && this.#minutes === other.minutes && this.#seconds === other.seconds; } /** * Outputs the time as the formatted string: {hh}:{mm}:{ss}. * * @public * @returns {string} */ format() { return `${leftPad(this.#hours, 2, "0")}:${leftPad(this.#minutes, 2, "0")}:${leftPad(this.#seconds, 2, "0")}`; } /** * Returns the JSON representation. * * @public * @returns {string} */ toJSON() { return this.format(); } /** * Returns true if the hours, minutes, and seconds combination is valid. * * @public * @static * @param {number} hours * @param {number} minutes * @param {number} seconds * @returns {boolean} */ static validate(hours, minutes, seconds) { return Number.isInteger(hours) && Number.isInteger(minutes) && Number.isInteger(seconds) && hours >= 0 && hours < HOURS_PER_DAY && minutes >= 0 && minutes < MINUTES_PER_HOUR && seconds >= 0 && seconds < SECONDS_PER_MINUTE; } /** * Parses a string in the format "hh:mm:ss" and returns a Time instance. * * @public * @static * @param {string} time * @returns {Time} */ static parse(time) { assert.argumentIsRequired(time, "time", String); const match = time.match(regex); if (match === null) { throw new Error(`Unable to parse [ Time ], invalid format [ ${time} ]`); } const hours = parseInt(match[1]); const minutes = parseInt(match[2]); const seconds = match[4] ? parseInt(match[4]) : 0; return new Time(hours, minutes, seconds); } /** * Creates a {@link Time} from the hours, minutes, and seconds properties (in local time) * of the {@link Date} argument. * * @public * @static * @param {Date} date * @returns {Time} */ static fromDate(date) { assert.argumentIsRequired(date, "date", Date); return new Time(date.getHours(), date.getMinutes(), date.getSeconds()); } /** * Creates a {@link Time} from the hours, minutes, and seconds properties (in UTC) * of the {@link Date} argument. * * @public * @static * @param {Date} date * @returns {Time} */ static fromDateUtc(date) { assert.argumentIsRequired(date, "date", Date); return new Time(date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()); } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return "[Time]"; } } const regex = /^([0-2]?[0-9]):([0-5][0-9])(:([0-5][0-9]))?$/i; function leftPad(value, digits, character) { const string = value.toString(); const padding = digits - string.length; return `${character.repeat(padding)}${string}`; } { const cjsExports = module.exports; const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports; if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) { Object.keys(cjsExports).forEach((key) => { if (key !== 'default' && key !== '__esModule') { cjsDefaultExport[key] = cjsExports[key]; } }); } module.exports = cjsDefaultExport; }