@rslike/cmp
Version:
JavaScript Comparison package without undefined behavior!
209 lines (205 loc) • 6.31 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
compare: () => compare,
equals: () => equals,
kCompare: () => kCompare,
kEquals: () => kEquals,
kPartialEquals: () => kPartialEquals,
partialEquals: () => partialEquals
});
module.exports = __toCommonJS(src_exports);
// src/symbols.ts
var kCompare = Symbol.for("Symbol.compare");
var kEquals = Symbol.for("Symbol.equals");
var kPartialEquals = Symbol.for("Symbol.partialEquals");
// src/utils.ts
var import_std = require("@rslike/std");
function compare(a, b, compareFn) {
if (typeof a[kCompare] === "function") {
return a[kCompare](b);
}
if (typeof b[kCompare] === "function") {
return b[kCompare](a);
}
if (compareFn) {
if (typeof compareFn !== "function") {
throw new import_std.UndefinedBehaviorError(
`compareFn argument should be a function. Got "${typeof compareFn}"`,
{
cause: {
value: compareFn,
type: typeof compareFn
}
}
);
}
const res = compareFn(a, b);
if (typeof res !== "number") {
throw new import_std.UndefinedBehaviorError(
`"compareFn" function should return number. Got "${typeof res}"`,
{ cause: { value: res, type: typeof res } }
);
}
}
throw new import_std.UndefinedBehaviorError(
'At least 1 argument should implement "Symbol.compare" trait'
);
}
function partialEquals(a, b, equalityFn) {
if (typeof a[kPartialEquals] === "function") {
const res = Reflect.apply(a[kPartialEquals], a, [b]);
if (typeof res === "boolean") {
return res;
}
throw new import_std.UndefinedBehaviorError(
`Cannot call partialCompare for imcoming first argument. "Symbol.partialEquals" trait should returns boolean result. Got "${typeof res}"`,
{
cause: {
first: a,
typeFirst: typeof a,
ctorFirst: a?.constructor,
result: res,
typeResult: typeof res,
second: b,
typeSecond: typeof b,
ctorSecond: b?.constructor
}
}
);
}
if (typeof b[kPartialEquals] === "function") {
const res = Reflect.apply(b[kPartialEquals], b, [a]);
if (typeof res === "boolean") {
return res;
}
throw new import_std.UndefinedBehaviorError(
`Cannot call partialCompare for imcoming second argument. "Symbol.partialEquals" trait should returns boolean result. Got "${typeof res}"`,
{
cause: {
first: a,
typeFirst: typeof a,
ctorFirst: a?.constructor,
result: res,
typeResult: typeof res,
second: b,
typeSecond: typeof b,
ctorSecond: b?.constructor
}
}
);
}
if (equalityFn) {
if (typeof equalityFn === "function") {
const res = equalityFn(a, b);
if (typeof res === "boolean") {
return res;
}
throw new import_std.UndefinedBehaviorError(
`equalityFn should returns boolean. Got "${typeof res}"`,
{
cause: { value: res, type: typeof res }
}
);
}
throw new import_std.UndefinedBehaviorError(
`equalityFn argument should be a function. Got "${typeof equalityFn}"`,
{
cause: { value: equalityFn, type: typeof equalityFn }
}
);
}
return a == b;
}
function equals(a, b, equalityFn) {
if (typeof a === "object" && a !== null && kEquals in a && typeof a[kEquals] === "function") {
const res = Reflect.apply(a[kEquals], a, [b]);
if (typeof res === "boolean") {
return res;
}
throw new import_std.UndefinedBehaviorError(
`Cannot call equals for imcoming first argument. "Symbol.equals" trait should returns boolean result. Got "${typeof res}"`,
{
cause: {
first: a,
typeFirst: typeof a,
ctorFirst: a?.constructor,
result: res,
typeResult: typeof res,
second: b,
typeSecond: typeof b,
ctorSecond: b?.constructor
}
}
);
}
if (typeof b === "object" && b !== null && kEquals in b && typeof b[kEquals] === "function") {
const res = Reflect.apply(b[kEquals], b, [a]);
if (typeof res === "boolean") {
return res;
}
throw new import_std.UndefinedBehaviorError(
`Cannot call equals for imcoming second argument. "Symbol.equals" trait should returns boolean result. Got "${typeof res}"`,
{
cause: {
first: a,
typeFirst: typeof a,
ctorFirst: a?.constructor,
result: res,
typeResult: typeof res,
second: b,
typeSecond: typeof b,
ctorSecond: b?.constructor
}
}
);
}
if (equalityFn) {
if (typeof equalityFn === "function") {
const res = equalityFn(a, b);
if (typeof res === "boolean") {
return res;
}
throw new import_std.UndefinedBehaviorError(
`equalityFn should returns boolean. Got "${typeof res}"`,
{
cause: { value: res, type: typeof res }
}
);
}
throw new import_std.UndefinedBehaviorError(
`equalityFn argument should be a function. Got "${typeof equalityFn}"`,
{
cause: { value: equalityFn, type: typeof equalityFn }
}
);
}
return a === b;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
compare,
equals,
kCompare,
kEquals,
kPartialEquals,
partialEquals
});