@barchart/common-js
Version:
Library of common JavaScript utilities
222 lines (216 loc) • 6.72 kB
JavaScript
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 Timespan_exports = {};
__export(Timespan_exports, {
default: () => Timespan
});
module.exports = __toCommonJS(Timespan_exports);
var assert = __toESM(require("./assert.js"));
var is = __toESM(require("./is.js"));
const MILLISECONDS_PER_SECOND = 1e3;
const SECONDS_PER_MINUTE = 60;
const MINUTES_PER_HOUR = 60;
const HOURS_PER_DAY = 24;
const MAX_HOURS = 23;
const MAX_MINUTES = 59;
const MAX_SECONDS = 59;
class Timespan {
#start;
#end;
/**
* @param {number} start
* @param {number} end
*/
constructor(start, end) {
assert.argumentIsValid(start, "start", is.large, "is an integer");
assert.argumentIsValid(end, "end", is.large, "is an integer");
if (start > end) {
throw new Error('The "start" parameter cannot be after the "end" parameter');
}
this.#start = start;
this.#end = end;
}
/**
* The start time (as milliseconds since epoch).
*
* @public
* @returns {number}
*/
get start() {
return this.#start;
}
/**
* The start time (as milliseconds since epoch).
*
* @public
* @returns {number}
*/
get end() {
return this.#end;
}
/**
* The total number of days between the start and end times (rounded down to the nearest integer).
*
* @public
* @returns {number}
*/
get days() {
return Math.floor(this.milliseconds / (MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY));
}
/**
* The total number of hours between the start and end times (rounded down to the nearest integer).
*
* @public
* @returns {number}
*/
get hours() {
return Math.floor(this.milliseconds / (MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE * MINUTES_PER_HOUR));
}
/**
* The total number of minutes between the start and end times (rounded down to the nearest integer).
*
* @public
* @returns {number}
*/
get minutes() {
return Math.floor(this.milliseconds / (MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE));
}
/**
* The total number of seconds between the start and end times (rounded down to the nearest integer).
*
* @public
* @returns {number}
*/
get seconds() {
return Math.floor(this.milliseconds / MILLISECONDS_PER_SECOND);
}
/**
* The total number of milliseconds between the start and end times.
*
* @public
* @returns {number}
*/
get milliseconds() {
return this.#end - this.#start;
}
/**
* Returns the duration between the start and end times as days, hours, minutes, seconds, and
* milliseconds.
*
* @public
* @param {boolean} days
* @param {boolean} hours
* @param {boolean} minutes
* @param {boolean} seconds
* @returns {{days: number, hours: number, minutes: *, seconds: number, milliseconds: number}}
*/
getDuration(days, hours, minutes, seconds) {
assert.argumentIsOptional(days, "days", Boolean);
assert.argumentIsOptional(hours, "hours", Boolean);
assert.argumentIsOptional(seconds, "minutes", Boolean);
assert.argumentIsOptional(seconds, "seconds", Boolean);
let milliseconds = this.milliseconds;
let d = 0, h = 0, m = 0, s = 0;
if (days) {
const factor = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY;
d = Math.floor(milliseconds / factor);
milliseconds = milliseconds - d * factor;
}
if (hours) {
const factor = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
h = Math.floor(milliseconds / factor);
if (days && h > MAX_HOURS) {
h = 23;
}
milliseconds = milliseconds - h * factor;
}
if (minutes) {
const factor = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
m = Math.floor(milliseconds / factor);
if (hours && m > MAX_MINUTES) {
m = MAX_MINUTES;
}
milliseconds = milliseconds - m * factor;
}
if (seconds) {
const factor = MILLISECONDS_PER_SECOND;
s = Math.floor(milliseconds / factor);
if (minutes && s > MAX_SECONDS) {
s = MAX_SECONDS;
}
milliseconds = milliseconds - s * factor;
}
return { days: d, hours: h, minutes: m, seconds: s, milliseconds };
}
/**
* Creates a new {@link Timespan} instance from dates.
*
* @public
* @static
* @param {Date} start
* @param {Date} end
* @returns {Timespan}
*/
static fromDates(start, end) {
assert.argumentIsRequired(start, "start", Date, "Date");
assert.argumentIsRequired(end, "end", Date, "Date");
return new Timespan(start.getTime(), end.getTime());
}
/**
* Returns the JSON representation.
*
* @public
* @returns {object}
*/
toJSON() {
const start = this.#start;
const end = this.#end;
return { start, end };
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[Timespan]";
}
}
{
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;
}