@barchart/common-js
Version:
Library of common JavaScript utilities
318 lines (312 loc) • 9.01 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 DataType_exports = {};
__export(DataType_exports, {
default: () => DataType
});
module.exports = __toCommonJS(DataType_exports);
var assert = __toESM(require("./../../lang/assert.js"));
var is = __toESM(require("./../../lang/is.js"));
var import_AdHoc = __toESM(require("./../../lang/AdHoc.js"));
var import_Day = __toESM(require("./../../lang/Day.js"));
var import_Decimal = __toESM(require("./../../lang/Decimal.js"));
var import_Enum = __toESM(require("./../../lang/Enum.js"));
var import_Timestamp = __toESM(require("./../../lang/Timestamp.js"));
class DataType {
#description;
#enumerationType;
#reviver;
#validator;
#builder;
/**
* @param {string} description
* @param {Function=} enumerationType
* @param {Function=} reviver
* @param {Function=} validator
* @param {Function=} builder
*/
constructor(description, enumerationType, reviver, validator, builder) {
assert.argumentIsRequired(description, "description", String);
assert.argumentIsOptional(enumerationType, "enumerationType", Function);
assert.argumentIsOptional(reviver, "reviver", Function);
assert.argumentIsOptional(validator, "validator", Function);
assert.argumentIsOptional(builder, "builder", Function);
if (enumerationType) {
assert.argumentIsValid(enumerationType, "enumerationType", extendsEnumeration, "is an enumeration");
}
this.#description = description;
this.#enumerationType = enumerationType || null;
let reviverToUse;
if (reviver) {
reviverToUse = reviver;
} else if (enumerationType) {
reviverToUse = (x) => import_Enum.default.fromCode(enumerationType, x);
} else {
reviverToUse = (x) => x;
}
this.#reviver = reviverToUse;
let validatorToUse;
if (validator) {
validatorToUse = validator;
} else {
validatorToUse = (candidate) => true;
}
this.#validator = validatorToUse;
let builderToUse;
if (builder) {
builderToUse = builder;
} else {
builderToUse = (data) => data;
}
this.#builder = builderToUse;
}
/**
* A function that converts data into the desired format.
*
* @public
* @param {*} data
* @returns {*}
*/
convert(data) {
return this.#builder(data);
}
/**
* Description of the data type.
*
* @public
* @returns {string}
*/
get description() {
return this.#description;
}
/**
* The {@Enum} type, if applicable.
*
* @public
* @returns {Function|null}
*/
get enumerationType() {
return this.#enumerationType;
}
/**
* A function which "revives" a value after serialization to JSON.
*
* @public
* @returns {Function}
*/
get reviver() {
return this.#reviver;
}
/**
* A function validates data, returning true or false.
*
* @public
* @returns {Function}
*/
get validator() {
return this.#validator;
}
/**
* Return a {@link DataType} instance for use with an {@link @Enum}.
*
* @public
* @static
* @param {Function} enumerationType - A class that extends {@link Enum}
* @param description - The description
* @returns {DataType}
*/
static forEnum(enumerationType, description) {
return new DataType(description, enumerationType, null, (x) => x instanceof enumerationType, getBuilder(getEnumerationBuilder(enumerationType)));
}
/**
* References a string.
*
* @public
* @static
* @returns {DataType}
*/
static get STRING() {
return dataTypeString;
}
/**
* References a number.
*
* @public
* @static
* @returns {DataType}
*/
static get NUMBER() {
return dataTypeNumber;
}
/**
* References a boolean value.
*
* @public
* @static
* @returns {DataType}
*/
static get BOOLEAN() {
return dataTypeBoolean;
}
/**
* References an object (serialized as JSON).
*
* @public
* @static
* @returns {DataType}
*/
static get OBJECT() {
return dataTypeObject;
}
/**
* References an array.
*
* @public
* @static
* @returns {DataType}
*/
static get ARRAY() {
return dataTypeArray;
}
/**
* References a {@link Decimal} instance.
*
* @public
* @static
* @returns {DataType}
*/
static get DECIMAL() {
return dataTypeDecimal;
}
/**
* References a {@link Day} instance.
*
* @public
* @static
* @returns {DataType}
*/
static get DAY() {
return dataTypeDay;
}
/**
* References a {@link Timestamp} instance.
*
* @public
* @static
* @returns {DataType}
*/
static get TIMESTAMP() {
return dataTypeTimestamp;
}
/**
* References an object whose internal properties are not important (for
* serialization and deserialization purposes).
*
* @public
* @static
* @returns {DataType}
*/
static get AD_HOC() {
return dataTypeAdHoc;
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return `[DataType (description=${this.#description})]`;
}
}
function extendsEnumeration(EnumerationType) {
return is.extension(import_Enum.default, EnumerationType);
}
const dataTypeString = new DataType("String", null, null, is.string);
const dataTypeNumber = new DataType("Number", null, null, is.number);
const dataTypeBoolean = new DataType("Boolean", null, null, is.boolean);
const dataTypeObject = new DataType("Object", null, null, is.object);
const dataTypeArray = new DataType("Array", null, null, is.array);
const dataTypeDecimal = new DataType("Decimal", null, (x) => import_Decimal.default.parse(x), (x) => x instanceof import_Decimal.default, getBuilder(buildDecimal));
const dataTypeDay = new DataType("Day", null, (x) => import_Day.default.parse(x), (x) => x instanceof import_Day.default, getBuilder(buildDay));
const dataTypeTimestamp = new DataType("Timestamp", null, (x) => import_Timestamp.default.parse(x), (x) => x instanceof import_Timestamp.default, getBuilder(buildTimestamp));
const dataTypeAdHoc = new DataType("AdHoc", null, (x) => import_AdHoc.default.parse(x), (x) => x instanceof import_AdHoc.default, getBuilder(buildAdHoc));
function getBuilder(builder) {
return (data) => {
try {
return builder(data);
} catch (e) {
return data;
}
};
}
function buildDecimal(data) {
return new import_Decimal.default(data);
}
function buildDay(data) {
if (data instanceof import_Day.default) {
return new import_Day.default(data.year, data.month, data.day);
} else if (is.date(data)) {
return import_Day.default.fromDate(data);
} else if (is.string(data)) {
return import_Day.default.parse(data);
} else {
return data;
}
}
function buildTimestamp(data) {
return new import_Timestamp.default(data);
}
function buildAdHoc(data) {
if (data instanceof import_AdHoc.default) {
return new import_AdHoc.default(data.data);
} else if (is.object(data)) {
return new import_AdHoc.default(data);
}
}
function getEnumerationBuilder(enumerationType) {
return (data) => {
if (is.string(data)) {
return import_Enum.default.fromCode(enumerationType, data);
} else {
return data;
}
};
}
{
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;
}