@barchart/common-js
Version:
Library of common JavaScript utilities
129 lines (123 loc) • 4.1 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 FailureReasonItem_exports = {};
__export(FailureReasonItem_exports, {
default: () => FailureReasonItem
});
module.exports = __toCommonJS(FailureReasonItem_exports);
var assert = __toESM(require("./../../lang/assert.js"));
var attributes = __toESM(require("./../../lang/attributes.js"));
var import_FailureType = __toESM(require("./FailureType.js"));
class FailureReasonItem {
#type;
#data;
/**
* @param {FailureType} type
* @param {object=} data
*/
constructor(type, data) {
assert.argumentIsRequired(type, "type", import_FailureType.default, "FailureType");
this.#type = type;
this.#data = data || null;
}
/**
* The {@link FailureType} of the item.
*
* @public
* @returns {FailureType}
*/
get type() {
return this.#type;
}
/**
* The data.
*
* @public
* @return {object}
*/
get data() {
return this.#data;
}
/**
* Formats a human-readable message, describing the failure.
*
* @public
* @param {object=} root - Root data from the {@link FailureReason}.
* @returns {string}
*/
format(root) {
return this.#type.template.replace(tokenRegex, (full, ignored, casing, token) => {
let tokenToUse;
let dataToRead;
if (token.startsWith(rootPrefix)) {
tokenToUse = token.slice(rootLength);
dataToRead = root;
} else {
tokenToUse = token;
dataToRead = this.#data;
}
let replacement = attributes.read(dataToRead, tokenToUse);
if (replacement) {
if (casing === "l") {
replacement = `${replacement.slice(0, 1).toLowerCase()}${replacement.slice(1)}`;
} else if (casing === "u") {
replacement = `${replacement.slice(0, 1).toUpperCase()}${replacement.slice(1)}`;
} else if (casing === "U") {
replacement = `${replacement.toUpperCase()}`;
} else if (casing === "L") {
replacement = `${replacement.toLowerCase()}`;
}
}
return replacement;
});
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[FailureReasonItem]";
}
}
const tokenRegex = /{(([U|L|l|u])\|)?([a-zA-Z.]*)}/g;
const rootPrefix = "root.";
const rootLength = rootPrefix.length;
{
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;
}