@barchart/common-js
Version:
Library of common JavaScript utilities
215 lines (209 loc) • 5.97 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 Timestamp_exports = {};
__export(Timestamp_exports, {
default: () => Timestamp
});
module.exports = __toCommonJS(Timestamp_exports);
var assert = __toESM(require("./assert.js"));
var is = __toESM(require("./is.js"));
const MILLISECONDS_PER_SECOND = 1e3;
class Timestamp {
#timestamp;
/**
* @param {number} timestamp
*/
constructor(timestamp) {
assert.argumentIsValid(timestamp, "timestamp", is.large, "is an integer");
this.#timestamp = timestamp;
}
/**
* The timestamp (milliseconds since epoch).
*
* @public
* @returns {number}
*/
get timestamp() {
return this.#timestamp;
}
/**
* Returns a new {@link Timestamp} instance shifted forward by a specific
* number of milliseconds.
*
* @public
* @param {number} milliseconds
* @returns {Timestamp}
*/
add(milliseconds) {
assert.argumentIsRequired(milliseconds, "milliseconds", Number);
return new Timestamp(this.#timestamp + milliseconds);
}
/**
* Returns a new {@link Timestamp} instance shifted backwards by a specific
* number of milliseconds.
*
* @public
* @param {number} milliseconds
* @returns {Timestamp}
*/
subtract(milliseconds) {
assert.argumentIsRequired(milliseconds, "milliseconds", Number);
return new Timestamp(this.#timestamp - milliseconds);
}
/**
* Returns a new {@link Timestamp} instance shifted forward by a specific
* number of seconds.
*
* @public
* @param {number} seconds
* @returns {Timestamp}
*/
addSeconds(seconds) {
assert.argumentIsRequired(seconds, "seconds", Number);
return this.add(seconds * MILLISECONDS_PER_SECOND);
}
/**
* Returns a new {@link Timestamp} instance shifted backwards by a specific
* number of seconds.
*
* @public
* @param {number} seconds
* @returns {Timestamp}
*/
subtractSeconds(seconds) {
assert.argumentIsRequired(seconds, "seconds", Number);
return this.subtract(seconds * MILLISECONDS_PER_SECOND);
}
/**
* Indicates if the current {@link Timestamp} instance occurs before another timestamp.
*
* @public
* @param {Timestamp} other
* @returns {boolean}
*/
getIsBefore(other) {
return Timestamp.compareTimestamps(this, other) < 0;
}
/**
* Indicates if the current {@link Timestamp} instance occurs after another timestamp.
*
* @public
* @param {Timestamp} other
* @returns {boolean}
*/
getIsAfter(other) {
return Timestamp.compareTimestamps(this, other) > 0;
}
/**
* Indicates if another {@link Timestamp} refers to the same moment.
*
* @public
* @param {Timestamp} other
* @returns {boolean}
*/
getIsEqual(other) {
return Timestamp.compareTimestamps(this, other) === 0;
}
/**
* Returns the JSON representation.
*
* @public
* @returns {number}
*/
toJSON() {
return this.timestamp;
}
/**
* Clones a {@link Timestamp} instance.
*
* @public
* @static
* @param {Timestamp} other
* @returns {Timestamp}
*/
static clone(other) {
assert.argumentIsRequired(other, "other", Timestamp, "Timestamp");
return new Timestamp(other.#timestamp);
}
/**
* Parses the value emitted by {@link Timestamp#toJSON}.
*
* @public
* @static
* @param {number} value
* @returns {Timestamp}
*/
static parse(value) {
return new Timestamp(value);
}
/**
* Returns a new {@link Timestamp} instance, representing the current moment.
*
* @public
* @static
* @returns {Timestamp}
*/
static now() {
return new Timestamp((/* @__PURE__ */ new Date()).getTime());
}
/**
* A comparator function for {@link Timestamp} instances.
*
* @public
* @static
* @param {Timestamp} a
* @param {Timestamp} b
* @returns {number}
*/
static compareTimestamps(a, b) {
assert.argumentIsRequired(a, "a", Timestamp, "Timestamp");
assert.argumentIsRequired(b, "b", Timestamp, "Timestamp");
return a.timestamp - b.timestamp;
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[Timestamp]";
}
}
{
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;
}