@barchart/common-js
Version:
Library of common JavaScript utilities
183 lines (177 loc) • 5.5 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 Enum_exports = {};
__export(Enum_exports, {
default: () => Enum
});
module.exports = __toCommonJS(Enum_exports);
var assert = __toESM(require("./assert.js"));
var is = __toESM(require("./is.js"));
const types = /* @__PURE__ */ new Map();
class Enum {
#code;
#description;
#mapping;
/**
* @param {string} code - The unique code of the enumeration item.
* @param {string} description - A description of the enumeration item.
* @param {number=} mapping - An alternate key value (used when external systems identify enumeration items using integer values).
*/
constructor(code, description, mapping) {
assert.argumentIsRequired(code, "code", String);
assert.argumentIsRequired(description, "description", String);
assert.argumentIsOptional(mapping, "mapping", Number);
if (is.number(mapping)) {
assert.argumentIsValid(mapping, "mapping", is.integer, "must be an integer");
}
this.#code = code;
this.#description = description;
if (is.number(mapping)) {
this.#mapping = mapping;
} else {
this.#mapping = null;
}
const c = this.constructor;
if (!types.has(c)) {
types.set(c, []);
}
const valid = Enum.fromCode(c, this.#code) === null && (this.#mapping === null || Enum.fromMapping(c, this.#mapping) === null);
if (valid) {
types.get(c).push(this);
}
}
/**
* The unique code.
*
* @public
* @returns {string}
*/
get code() {
return this.#code;
}
/**
* The description.
*
* @public
* @returns {string}
*/
get description() {
return this.#description;
}
/**
* An alternate key value (used when external systems identify enumeration items
* using numeric values). This value will not be present for all enumerations.
*
* @public
* @returns {number|null}
*/
get mapping() {
return this.#mapping;
}
/**
* Returns true if the provided {@link Enum} argument is equal
* to the instance.
*
* @public
* @param {Enum} other
* @returns {boolean}
*/
equals(other) {
return other === this || other instanceof Enum && other.constructor === this.constructor && other.code === this.code;
}
/**
* Returns the JSON representation.
*
* @public
* @returns {string}
*/
toJSON() {
return this.code;
}
/**
* Looks up an enumeration item; given the enumeration type and the enumeration
* item's value. If no matching item can be found, a null value is returned.
*
* @public
* @static
* @param {Function} type - The enumeration type.
* @param {string} code - The enumeration item's code.
* @returns {Enum|null}
*/
static fromCode(type, code) {
return Enum.getItems(type).find((x) => x.code === code) || null;
}
/**
* Looks up an enumeration item; given the enumeration type and the enumeration
* item's value. If no matching item can be found, a null value is returned.
*
* @public
* @static
* @param {Function} type - The enumeration type.
* @param {number} mapping - The enumeration item's mapping value.
* @returns {Enum|null}
*/
static fromMapping(type, mapping) {
if (mapping === null) {
return null;
}
return Enum.getItems(type).find((x) => x.mapping === mapping) || null;
}
/**
* Returns the enumeration's items (given an enumeration type).
*
* @public
* @static
* @param {Function} type - The enumeration to list.
* @returns {Array}
*/
static getItems(type) {
return types.get(type) || [];
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[Enum]";
}
}
{
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;
}