earljs
Version:
Ergonomic, modern and type-safe assertion library
83 lines (82 loc) • 2.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isEqualUnknown = void 0;
const matchers_1 = require("../matchers");
const getCanonicalType_1 = require("./getCanonicalType");
const isEqualMap_1 = require("./isEqualMap");
const isEqualNumber_1 = require("./isEqualNumber");
const isEqualObject_1 = require("./isEqualObject");
const isEqualSet_1 = require("./isEqualSet");
const rules_1 = require("./rules");
function isEqualUnknown(value, valueStack, other, otherStack, options) {
if (other instanceof matchers_1.Matcher) {
return other.check(value);
}
for (const rule of rules_1.smartEqRules) {
const ruleResult = rule(value, other, options.ignorePrototypes);
if (ruleResult) {
return ruleResult.result === 'success';
}
}
const type = (0, getCanonicalType_1.getCanonicalType)(value);
const otherType = (0, getCanonicalType_1.getCanonicalType)(other);
if (type !== otherType) {
return false;
}
switch (type) {
case 'null':
case 'undefined':
case 'boolean':
case 'bigint':
case 'string':
case 'symbol':
case 'Function':
case 'Promise':
case 'WeakMap':
case 'WeakSet':
return value === other;
case 'number':
return (0, isEqualNumber_1.isEqualNumber)(value, other, options);
}
// This check is so late because of isEqualNumber
if (value === other) {
return true;
}
const valueIndex = valueStack.indexOf(value);
const otherIndex = otherStack.indexOf(other);
if (valueIndex !== -1 || otherIndex !== -1) {
return valueIndex === otherIndex;
}
if (!options.ignorePrototypes) {
if (Object.getPrototypeOf(value) !== Object.getPrototypeOf(other)) {
return false;
}
}
if (type === 'Array') {
if (value.length !== other.length) {
return false;
}
}
else if (type === 'Set') {
if (!(0, isEqualSet_1.isEqualSet)(value, other)) {
return false;
}
}
else if (type === 'Map') {
if (!(0, isEqualMap_1.isEqualMap)(value, valueStack, other, otherStack, options)) {
return false;
}
}
else if (type === 'Date' || type === 'String' || type === 'Number' || type === 'Boolean') {
if (value.valueOf() !== other.valueOf()) {
return false;
}
}
else if (type === 'RegExp') {
if (value.toString() !== other.toString()) {
return false;
}
}
return (0, isEqualObject_1.isEqualObject)(value, valueStack, other, otherStack, options, type);
}
exports.isEqualUnknown = isEqualUnknown;
;