@qudtlib/core
Version:
Data model for QUDTLib
120 lines (119 loc) • 4.39 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZERO = exports.ONE = exports.isNullish = exports.findInIterable = exports.checkInteger = exports.arrayContains = exports.arrayMax = exports.arrayMin = exports.arrayCountEqualElements = exports.arrayEqualsIgnoreOrdering = exports.arrayEquals = exports.arrayDeduplicate = exports.compareUsingEquals = exports.StringComparator = exports.NumberComparator = exports.BooleanComparator = exports.getLastIriElement = void 0;
const decimal_js_1 = require("decimal.js");
function getLastIriElement(iri) {
return iri.replaceAll(/.+\/([^\/]+)/g, "$1");
}
exports.getLastIriElement = getLastIriElement;
const BooleanComparator = (left, right) => (left === right ? 0 : left ? 1 : -1);
exports.BooleanComparator = BooleanComparator;
const NumberComparator = (left, right) => (left < right ? -1 : left == right ? 0 : 1);
exports.NumberComparator = NumberComparator;
const StringComparator = (left, right) => (left < right ? -1 : left === right ? 0 : 1);
exports.StringComparator = StringComparator;
function compareUsingEquals(a, b) {
return a.equals(b);
}
exports.compareUsingEquals = compareUsingEquals;
function arrayDeduplicate(arr, cmp = (a, b) => a === b) {
if (!arr || !arr.length || arr.length === 0) {
return arr;
}
return arr.reduce((prev, cur) => prev.some((p) => cmp(p, cur)) ? prev : [...prev, cur], []);
}
exports.arrayDeduplicate = arrayDeduplicate;
function arrayEquals(left, right, cmp = (a, b) => a === b) {
return (!!left &&
!!right &&
left.length === right.length &&
left.every((e, i) => cmp(e, right[i])));
}
exports.arrayEquals = arrayEquals;
function arrayEqualsIgnoreOrdering(left, right, cmp = (a, b) => a === b) {
if (!!left && !!right && left.length === right.length) {
const unmatched = Array.from({ length: left.length }, (v, i) => i);
outer: for (let i = 0; i < left.length; i++) {
for (let j = 0; j < unmatched.length; j++) {
if (cmp(left[i], right[unmatched[j]])) {
unmatched.splice(j, 1);
continue outer;
}
}
return false;
}
return true;
}
return false;
}
exports.arrayEqualsIgnoreOrdering = arrayEqualsIgnoreOrdering;
function arrayCountEqualElements(left, right, cmp = (a, b) => a === b) {
if (!!left && !!right) {
const unmatched = Array.from({ length: left.length }, (v, i) => i);
outer: for (let i = 0; i < left.length; i++) {
for (let j = 0; j < unmatched.length; j++) {
if (cmp(left[i], right[unmatched[j]])) {
unmatched.splice(j, 1);
continue outer;
}
}
}
return left.length - unmatched.length;
}
return 0;
}
exports.arrayCountEqualElements = arrayCountEqualElements;
function arrayMin(arr, cmp) {
if (!arr || !arr?.length) {
throw "array is undefined or empty";
}
let min = undefined;
for (const elem of arr) {
if (typeof min === "undefined" || cmp(min, elem) > 0) {
min = elem;
}
}
if (typeof min === "undefined") {
throw "no minimum found";
}
return min;
}
exports.arrayMin = arrayMin;
function arrayMax(arr, cmp) {
return arrayMin(arr, (left, right) => -1 * cmp(left, right));
}
exports.arrayMax = arrayMax;
function arrayContains(arr, toFind, cmp = (a, b) => a === b) {
if (!arr) {
throw "array is undefined";
}
for (const elem of arr) {
if (cmp(elem, toFind)) {
return true;
}
}
return false;
}
exports.arrayContains = arrayContains;
function checkInteger(arg, argName) {
if (!Number.isInteger(arg)) {
throw `${argName} must be integer, ${arg} (type ${typeof arg}) is not`;
}
}
exports.checkInteger = checkInteger;
function findInIterable(iterable, predicate) {
for (const elem of iterable) {
if (predicate(elem)) {
return elem;
}
}
return undefined;
}
exports.findInIterable = findInIterable;
function isNullish(val) {
return typeof val === "undefined" || val === null;
}
exports.isNullish = isNullish;
exports.ONE = new decimal_js_1.Decimal("1");
exports.ZERO = new decimal_js_1.Decimal("0");
//# sourceMappingURL=utils.js.map
;