@barchart/common-js
Version:
Library of common JavaScript utilities
132 lines (130 loc) • 5.41 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 assert_exports = {};
__export(assert_exports, {
areEqual: () => areEqual,
areNotEqual: () => areNotEqual,
argumentIsArray: () => argumentIsArray,
argumentIsOptional: () => argumentIsOptional,
argumentIsRequired: () => argumentIsRequired,
argumentIsValid: () => argumentIsValid
});
module.exports = __toCommonJS(assert_exports);
var is = __toESM(require("./is.js"));
const nativeTypes = [String, Number, Function, Boolean, Date, Array, Object, RegExp];
function checkArgumentType(variable, variableName, type, typeDescription, index) {
if (type === String) {
if (!is.string(variable)) {
throwInvalidTypeError(variableName, "string", index);
}
} else if (type === Number) {
if (!is.number(variable)) {
throwInvalidTypeError(variableName, "number", index);
}
} else if (type === Function) {
if (!is.fn(variable)) {
throwInvalidTypeError(variableName, "function", index);
}
} else if (type === Boolean) {
if (!is.boolean(variable)) {
throwInvalidTypeError(variableName, "boolean", index);
}
} else if (type === Date) {
if (!is.date(variable)) {
throwInvalidTypeError(variableName, "date", index);
}
} else if (type === RegExp) {
if (!is.regexp(variable)) {
throwInvalidTypeError(variableName, "RegExp", index);
}
} else if (type === Array) {
if (!is.array(variable)) {
throwInvalidTypeError(variableName, "array", index);
}
} else if (!(variable instanceof (type || Object))) {
throwInvalidTypeError(variableName, typeDescription, index);
}
}
function throwInvalidTypeError(variableName, typeDescription, index) {
let message;
if (typeof index === "number") {
message = `The argument [ ${variableName || "unspecified"} ], at index [ ${index.toString()} ] must be a [ ${typeDescription || "unknown"} ]`;
} else {
message = `The argument [ ${variableName || "unspecified"} ] must be a [ ${typeDescription || "Object"} ]`;
}
throw new Error(message);
}
function throwCustomValidationError(variableName, predicateDescription) {
throw new Error(`The argument [ ${variableName || "unspecified"} ] failed a validation check [ ${predicateDescription || "No description available"} ]`);
}
function argumentIsRequired(variable, variableName, type, typeDescription) {
checkArgumentType(variable, variableName, type, typeDescription);
}
function argumentIsOptional(variable, variableName, type, typeDescription, predicate, predicateDescription) {
if (variable === null || variable === void 0) {
return;
}
checkArgumentType(variable, variableName, type, typeDescription);
if (is.fn(predicate) && !predicate(variable)) {
throwCustomValidationError(variableName, predicateDescription);
}
}
function argumentIsArray(variable, variableName, itemConstraint, itemConstraintDescription) {
argumentIsRequired(variable, variableName, Array);
if (itemConstraint) {
let itemValidator;
if (nativeTypes.includes(itemConstraint)) {
itemValidator = (value, index) => checkArgumentType(value, variableName, itemConstraint, itemConstraintDescription, index);
} else {
itemValidator = (value, index) => {
if (itemConstraint.prototype !== void 0 && value instanceof itemConstraint) {
return;
}
itemConstraint(value, `${variableName}[${index}]`);
};
}
variable.forEach((v, i) => {
itemValidator(v, i);
});
}
}
function argumentIsValid(variable, variableName, predicate, predicateDescription) {
if (!predicate(variable)) {
throwCustomValidationError(variableName, predicateDescription);
}
}
function areEqual(a, b, descriptionA, descriptionB) {
if (a !== b) {
throw new Error(`The objects must be equal [${descriptionA || a.toString()}] and [${descriptionB || b.toString()}]`);
}
}
function areNotEqual(a, b, descriptionA, descriptionB) {
if (a === b) {
throw new Error(`The objects cannot be equal [${descriptionA || a.toString()}] and [${descriptionB || b.toString()}]`);
}
}