@technobuddha/library
Version:
A large library of useful functions
59 lines (58 loc) • 2.18 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.shallowEquals = void 0;
function sameValue(x, y) {
if (x === y)
return x !== 0 || 1 / x === 1 / y;
// eslint-disable-next-line no-self-compare
return x !== x && y !== y;
}
/**
* Compare two object for equality. Testing goes one level deep.
*
* @param objA First object to compare
* @param objB Second object to compare
* @param exclude Array of key names to exclude from the comparison
* @returns true if the two objects have the same members
*/
function shallowEquals(objA, objB, exclude) {
var e_1, _a;
if (exclude === void 0) { exclude = []; }
if (sameValue(objA, objB))
return true;
if (objA === null || objA === undefined || objB === null || objB === undefined)
return false;
var hash = new Set(exclude);
var keysA = Object.keys(objA).filter(function (key) { return !hash.has(key); });
var keysB = Object.keys(objB).filter(function (key) { return !hash.has(key); });
if (keysA.length !== keysB.length)
return false;
try {
for (var keysA_1 = __values(keysA), keysA_1_1 = keysA_1.next(); !keysA_1_1.done; keysA_1_1 = keysA_1.next()) {
var key = keysA_1_1.value;
if (!Object.prototype.hasOwnProperty.call(objB, key) || !sameValue(objA[key], objB[key]))
return false;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (keysA_1_1 && !keysA_1_1.done && (_a = keysA_1.return)) _a.call(keysA_1);
}
finally { if (e_1) throw e_1.error; }
}
return true;
}
exports.shallowEquals = shallowEquals;
exports.default = shallowEquals;