UNPKG

4.5

Version:

Monadic test assertions

94 lines 2.88 kB
"use strict"; var functionName_1 = require("./functionName"); var type_1 = require("./type"); // slightly modified version of ramda's R.equals function isEqual(a, b) { return _isEqual(a, b, [], []); } exports.isEqual = isEqual; function _isEqual(a, b, stackA, stackB) { if (a === b) return true; if (type_1.type(a) !== type_1.type(b)) return false; // tslint:disable-next-line if (a == null || b == null) return false; if (typeof a.equals === 'function' || typeof b.equals === 'function') return typeof a.equals === 'function' && a.equals(b) && typeof b.equals === 'function' && b.equals(a); switch (type_1.type(a)) { case 'Arguments': case 'Array': case 'Object': if (typeof a.constructor === 'function' && functionName_1.functionName(a.constructor) === 'Promise') return a === b; break; case 'Boolean': case 'Number': case 'String': if (!(typeof a === typeof b && a.valueOf() === b.valueOf())) return false; break; case 'Date': if (a.valueOf() !== b.valueOf()) return false; break; case 'Error': return a.name === b.name && a.message === b.message; case 'RegExp': if (!(a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline && a.sticky === b.sticky && a.unicode === b.unicode)) { return false; } break; case 'Map': case 'Set': if (!_isEqual(Array.from(a.entries()), Array.from(b.entries()), stackA, stackB)) return false; break; case 'Int8Array': case 'Uint8Array': case 'Uint8ClampedArray': case 'Int16Array': case 'Uint16Array': case 'Int32Array': case 'Uint32Array': case 'Float32Array': case 'Float64Array': break; case 'ArrayBuffer': break; default: // Values of other types are only equal if identical. return false; } var keysA = Object.keys(a); if (keysA.length !== Object.keys(b).length) return false; var idx = stackA.length - 1; while (idx >= 0) { if (stackA[idx] === a) return stackB[idx] === b; idx -= 1; } stackA.push(a); stackB.push(b); idx = keysA.length - 1; while (idx >= 0) { var key = keysA[idx]; if (!(b[key] && _isEqual(b[key], a[key], stackA, stackB))) return false; idx -= 1; } stackA.pop(); stackB.pop(); return true; } ; //# sourceMappingURL=isEqual.js.map