Path: blob/master/node_modules/assert/build/internal/util/comparisons.js
1129 views
// Currently in sync with Node.js lib/internal/util/comparisons.js1// https://github.com/nodejs/node/commit/112cc7c27551254aa2b17098fb774867f05ed0d92'use strict';34function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }56function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }78function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }910function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }1112function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }1314var regexFlagsSupported = /a/g.flags !== undefined;1516var arrayFromSet = function arrayFromSet(set) {17var array = [];18set.forEach(function (value) {19return array.push(value);20});21return array;22};2324var arrayFromMap = function arrayFromMap(map) {25var array = [];26map.forEach(function (value, key) {27return array.push([key, value]);28});29return array;30};3132var objectIs = Object.is ? Object.is : require('object-is');33var objectGetOwnPropertySymbols = Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols : function () {34return [];35};36var numberIsNaN = Number.isNaN ? Number.isNaN : require('is-nan');3738function uncurryThis(f) {39return f.call.bind(f);40}4142var hasOwnProperty = uncurryThis(Object.prototype.hasOwnProperty);43var propertyIsEnumerable = uncurryThis(Object.prototype.propertyIsEnumerable);44var objectToString = uncurryThis(Object.prototype.toString);4546var _require$types = require('util/').types,47isAnyArrayBuffer = _require$types.isAnyArrayBuffer,48isArrayBufferView = _require$types.isArrayBufferView,49isDate = _require$types.isDate,50isMap = _require$types.isMap,51isRegExp = _require$types.isRegExp,52isSet = _require$types.isSet,53isNativeError = _require$types.isNativeError,54isBoxedPrimitive = _require$types.isBoxedPrimitive,55isNumberObject = _require$types.isNumberObject,56isStringObject = _require$types.isStringObject,57isBooleanObject = _require$types.isBooleanObject,58isBigIntObject = _require$types.isBigIntObject,59isSymbolObject = _require$types.isSymbolObject,60isFloat32Array = _require$types.isFloat32Array,61isFloat64Array = _require$types.isFloat64Array;6263function isNonIndex(key) {64if (key.length === 0 || key.length > 10) return true;6566for (var i = 0; i < key.length; i++) {67var code = key.charCodeAt(i);68if (code < 48 || code > 57) return true;69} // The maximum size for an array is 2 ** 32 -1.707172return key.length === 10 && key >= Math.pow(2, 32);73}7475function getOwnNonIndexProperties(value) {76return Object.keys(value).filter(isNonIndex).concat(objectGetOwnPropertySymbols(value).filter(Object.prototype.propertyIsEnumerable.bind(value)));77} // Taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js78// original notice:7980/*!81* The buffer module from node.js, for the browser.82*83* @author Feross Aboukhadijeh <[email protected]> <http://feross.org>84* @license MIT85*/868788function compare(a, b) {89if (a === b) {90return 0;91}9293var x = a.length;94var y = b.length;9596for (var i = 0, len = Math.min(x, y); i < len; ++i) {97if (a[i] !== b[i]) {98x = a[i];99y = b[i];100break;101}102}103104if (x < y) {105return -1;106}107108if (y < x) {109return 1;110}111112return 0;113}114115var ONLY_ENUMERABLE = undefined;116var kStrict = true;117var kLoose = false;118var kNoIterator = 0;119var kIsArray = 1;120var kIsSet = 2;121var kIsMap = 3; // Check if they have the same source and flags122123function areSimilarRegExps(a, b) {124return regexFlagsSupported ? a.source === b.source && a.flags === b.flags : RegExp.prototype.toString.call(a) === RegExp.prototype.toString.call(b);125}126127function areSimilarFloatArrays(a, b) {128if (a.byteLength !== b.byteLength) {129return false;130}131132for (var offset = 0; offset < a.byteLength; offset++) {133if (a[offset] !== b[offset]) {134return false;135}136}137138return true;139}140141function areSimilarTypedArrays(a, b) {142if (a.byteLength !== b.byteLength) {143return false;144}145146return compare(new Uint8Array(a.buffer, a.byteOffset, a.byteLength), new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0;147}148149function areEqualArrayBuffers(buf1, buf2) {150return buf1.byteLength === buf2.byteLength && compare(new Uint8Array(buf1), new Uint8Array(buf2)) === 0;151}152153function isEqualBoxedPrimitive(val1, val2) {154if (isNumberObject(val1)) {155return isNumberObject(val2) && objectIs(Number.prototype.valueOf.call(val1), Number.prototype.valueOf.call(val2));156}157158if (isStringObject(val1)) {159return isStringObject(val2) && String.prototype.valueOf.call(val1) === String.prototype.valueOf.call(val2);160}161162if (isBooleanObject(val1)) {163return isBooleanObject(val2) && Boolean.prototype.valueOf.call(val1) === Boolean.prototype.valueOf.call(val2);164}165166if (isBigIntObject(val1)) {167return isBigIntObject(val2) && BigInt.prototype.valueOf.call(val1) === BigInt.prototype.valueOf.call(val2);168}169170return isSymbolObject(val2) && Symbol.prototype.valueOf.call(val1) === Symbol.prototype.valueOf.call(val2);171} // Notes: Type tags are historical [[Class]] properties that can be set by172// FunctionTemplate::SetClassName() in C++ or Symbol.toStringTag in JS173// and retrieved using Object.prototype.toString.call(obj) in JS174// See https://tc39.github.io/ecma262/#sec-object.prototype.tostring175// for a list of tags pre-defined in the spec.176// There are some unspecified tags in the wild too (e.g. typed array tags).177// Since tags can be altered, they only serve fast failures178//179// Typed arrays and buffers are checked by comparing the content in their180// underlying ArrayBuffer. This optimization requires that it's181// reasonable to interpret their underlying memory in the same way,182// which is checked by comparing their type tags.183// (e.g. a Uint8Array and a Uint16Array with the same memory content184// could still be different because they will be interpreted differently).185//186// For strict comparison, objects should have187// a) The same built-in type tags188// b) The same prototypes.189190191function innerDeepEqual(val1, val2, strict, memos) {192// All identical values are equivalent, as determined by ===.193if (val1 === val2) {194if (val1 !== 0) return true;195return strict ? objectIs(val1, val2) : true;196} // Check more closely if val1 and val2 are equal.197198199if (strict) {200if (_typeof(val1) !== 'object') {201return typeof val1 === 'number' && numberIsNaN(val1) && numberIsNaN(val2);202}203204if (_typeof(val2) !== 'object' || val1 === null || val2 === null) {205return false;206}207208if (Object.getPrototypeOf(val1) !== Object.getPrototypeOf(val2)) {209return false;210}211} else {212if (val1 === null || _typeof(val1) !== 'object') {213if (val2 === null || _typeof(val2) !== 'object') {214// eslint-disable-next-line eqeqeq215return val1 == val2;216}217218return false;219}220221if (val2 === null || _typeof(val2) !== 'object') {222return false;223}224}225226var val1Tag = objectToString(val1);227var val2Tag = objectToString(val2);228229if (val1Tag !== val2Tag) {230return false;231}232233if (Array.isArray(val1)) {234// Check for sparse arrays and general fast path235if (val1.length !== val2.length) {236return false;237}238239var keys1 = getOwnNonIndexProperties(val1, ONLY_ENUMERABLE);240var keys2 = getOwnNonIndexProperties(val2, ONLY_ENUMERABLE);241242if (keys1.length !== keys2.length) {243return false;244}245246return keyCheck(val1, val2, strict, memos, kIsArray, keys1);247} // [browserify] This triggers on certain types in IE (Map/Set) so we don't248// wan't to early return out of the rest of the checks. However we can check249// if the second value is one of these values and the first isn't.250251252if (val1Tag === '[object Object]') {253// return keyCheck(val1, val2, strict, memos, kNoIterator);254if (!isMap(val1) && isMap(val2) || !isSet(val1) && isSet(val2)) {255return false;256}257}258259if (isDate(val1)) {260if (!isDate(val2) || Date.prototype.getTime.call(val1) !== Date.prototype.getTime.call(val2)) {261return false;262}263} else if (isRegExp(val1)) {264if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {265return false;266}267} else if (isNativeError(val1) || val1 instanceof Error) {268// Do not compare the stack as it might differ even though the error itself269// is otherwise identical.270if (val1.message !== val2.message || val1.name !== val2.name) {271return false;272}273} else if (isArrayBufferView(val1)) {274if (!strict && (isFloat32Array(val1) || isFloat64Array(val1))) {275if (!areSimilarFloatArrays(val1, val2)) {276return false;277}278} else if (!areSimilarTypedArrays(val1, val2)) {279return false;280} // Buffer.compare returns true, so val1.length === val2.length. If they both281// only contain numeric keys, we don't need to exam further than checking282// the symbols.283284285var _keys = getOwnNonIndexProperties(val1, ONLY_ENUMERABLE);286287var _keys2 = getOwnNonIndexProperties(val2, ONLY_ENUMERABLE);288289if (_keys.length !== _keys2.length) {290return false;291}292293return keyCheck(val1, val2, strict, memos, kNoIterator, _keys);294} else if (isSet(val1)) {295if (!isSet(val2) || val1.size !== val2.size) {296return false;297}298299return keyCheck(val1, val2, strict, memos, kIsSet);300} else if (isMap(val1)) {301if (!isMap(val2) || val1.size !== val2.size) {302return false;303}304305return keyCheck(val1, val2, strict, memos, kIsMap);306} else if (isAnyArrayBuffer(val1)) {307if (!areEqualArrayBuffers(val1, val2)) {308return false;309}310} else if (isBoxedPrimitive(val1) && !isEqualBoxedPrimitive(val1, val2)) {311return false;312}313314return keyCheck(val1, val2, strict, memos, kNoIterator);315}316317function getEnumerables(val, keys) {318return keys.filter(function (k) {319return propertyIsEnumerable(val, k);320});321}322323function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {324// For all remaining Object pairs, including Array, objects and Maps,325// equivalence is determined by having:326// a) The same number of owned enumerable properties327// b) The same set of keys/indexes (although not necessarily the same order)328// c) Equivalent values for every corresponding key/index329// d) For Sets and Maps, equal contents330// Note: this accounts for both named and indexed properties on Arrays.331if (arguments.length === 5) {332aKeys = Object.keys(val1);333var bKeys = Object.keys(val2); // The pair must have the same number of owned properties.334335if (aKeys.length !== bKeys.length) {336return false;337}338} // Cheap key test339340341var i = 0;342343for (; i < aKeys.length; i++) {344if (!hasOwnProperty(val2, aKeys[i])) {345return false;346}347}348349if (strict && arguments.length === 5) {350var symbolKeysA = objectGetOwnPropertySymbols(val1);351352if (symbolKeysA.length !== 0) {353var count = 0;354355for (i = 0; i < symbolKeysA.length; i++) {356var key = symbolKeysA[i];357358if (propertyIsEnumerable(val1, key)) {359if (!propertyIsEnumerable(val2, key)) {360return false;361}362363aKeys.push(key);364count++;365} else if (propertyIsEnumerable(val2, key)) {366return false;367}368}369370var symbolKeysB = objectGetOwnPropertySymbols(val2);371372if (symbolKeysA.length !== symbolKeysB.length && getEnumerables(val2, symbolKeysB).length !== count) {373return false;374}375} else {376var _symbolKeysB = objectGetOwnPropertySymbols(val2);377378if (_symbolKeysB.length !== 0 && getEnumerables(val2, _symbolKeysB).length !== 0) {379return false;380}381}382}383384if (aKeys.length === 0 && (iterationType === kNoIterator || iterationType === kIsArray && val1.length === 0 || val1.size === 0)) {385return true;386} // Use memos to handle cycles.387388389if (memos === undefined) {390memos = {391val1: new Map(),392val2: new Map(),393position: 0394};395} else {396// We prevent up to two map.has(x) calls by directly retrieving the value397// and checking for undefined. The map can only contain numbers, so it is398// safe to check for undefined only.399var val2MemoA = memos.val1.get(val1);400401if (val2MemoA !== undefined) {402var val2MemoB = memos.val2.get(val2);403404if (val2MemoB !== undefined) {405return val2MemoA === val2MemoB;406}407}408409memos.position++;410}411412memos.val1.set(val1, memos.position);413memos.val2.set(val2, memos.position);414var areEq = objEquiv(val1, val2, strict, aKeys, memos, iterationType);415memos.val1.delete(val1);416memos.val2.delete(val2);417return areEq;418}419420function setHasEqualElement(set, val1, strict, memo) {421// Go looking.422var setValues = arrayFromSet(set);423424for (var i = 0; i < setValues.length; i++) {425var val2 = setValues[i];426427if (innerDeepEqual(val1, val2, strict, memo)) {428// Remove the matching element to make sure we do not check that again.429set.delete(val2);430return true;431}432}433434return false;435} // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using436// Sadly it is not possible to detect corresponding values properly in case the437// type is a string, number, bigint or boolean. The reason is that those values438// can match lots of different string values (e.g., 1n == '+00001').439440441function findLooseMatchingPrimitives(prim) {442switch (_typeof(prim)) {443case 'undefined':444return null;445446case 'object':447// Only pass in null as object!448return undefined;449450case 'symbol':451return false;452453case 'string':454prim = +prim;455// Loose equal entries exist only if the string is possible to convert to456// a regular number and not NaN.457// Fall through458459case 'number':460if (numberIsNaN(prim)) {461return false;462}463464}465466return true;467}468469function setMightHaveLoosePrim(a, b, prim) {470var altValue = findLooseMatchingPrimitives(prim);471if (altValue != null) return altValue;472return b.has(altValue) && !a.has(altValue);473}474475function mapMightHaveLoosePrim(a, b, prim, item, memo) {476var altValue = findLooseMatchingPrimitives(prim);477478if (altValue != null) {479return altValue;480}481482var curB = b.get(altValue);483484if (curB === undefined && !b.has(altValue) || !innerDeepEqual(item, curB, false, memo)) {485return false;486}487488return !a.has(altValue) && innerDeepEqual(item, curB, false, memo);489}490491function setEquiv(a, b, strict, memo) {492// This is a lazily initiated Set of entries which have to be compared493// pairwise.494var set = null;495var aValues = arrayFromSet(a);496497for (var i = 0; i < aValues.length; i++) {498var val = aValues[i]; // Note: Checking for the objects first improves the performance for object499// heavy sets but it is a minor slow down for primitives. As they are fast500// to check this improves the worst case scenario instead.501502if (_typeof(val) === 'object' && val !== null) {503if (set === null) {504set = new Set();505} // If the specified value doesn't exist in the second set its an not null506// object (or non strict only: a not matching primitive) we'll need to go507// hunting for something thats deep-(strict-)equal to it. To make this508// O(n log n) complexity we have to copy these values in a new set first.509510511set.add(val);512} else if (!b.has(val)) {513if (strict) return false; // Fast path to detect missing string, symbol, undefined and null values.514515if (!setMightHaveLoosePrim(a, b, val)) {516return false;517}518519if (set === null) {520set = new Set();521}522523set.add(val);524}525}526527if (set !== null) {528var bValues = arrayFromSet(b);529530for (var _i = 0; _i < bValues.length; _i++) {531var _val = bValues[_i]; // We have to check if a primitive value is already532// matching and only if it's not, go hunting for it.533534if (_typeof(_val) === 'object' && _val !== null) {535if (!setHasEqualElement(set, _val, strict, memo)) return false;536} else if (!strict && !a.has(_val) && !setHasEqualElement(set, _val, strict, memo)) {537return false;538}539}540541return set.size === 0;542}543544return true;545}546547function mapHasEqualEntry(set, map, key1, item1, strict, memo) {548// To be able to handle cases like:549// Map([[{}, 'a'], [{}, 'b']]) vs Map([[{}, 'b'], [{}, 'a']])550// ... we need to consider *all* matching keys, not just the first we find.551var setValues = arrayFromSet(set);552553for (var i = 0; i < setValues.length; i++) {554var key2 = setValues[i];555556if (innerDeepEqual(key1, key2, strict, memo) && innerDeepEqual(item1, map.get(key2), strict, memo)) {557set.delete(key2);558return true;559}560}561562return false;563}564565function mapEquiv(a, b, strict, memo) {566var set = null;567var aEntries = arrayFromMap(a);568569for (var i = 0; i < aEntries.length; i++) {570var _aEntries$i = _slicedToArray(aEntries[i], 2),571key = _aEntries$i[0],572item1 = _aEntries$i[1];573574if (_typeof(key) === 'object' && key !== null) {575if (set === null) {576set = new Set();577}578579set.add(key);580} else {581// By directly retrieving the value we prevent another b.has(key) check in582// almost all possible cases.583var item2 = b.get(key);584585if (item2 === undefined && !b.has(key) || !innerDeepEqual(item1, item2, strict, memo)) {586if (strict) return false; // Fast path to detect missing string, symbol, undefined and null587// keys.588589if (!mapMightHaveLoosePrim(a, b, key, item1, memo)) return false;590591if (set === null) {592set = new Set();593}594595set.add(key);596}597}598}599600if (set !== null) {601var bEntries = arrayFromMap(b);602603for (var _i2 = 0; _i2 < bEntries.length; _i2++) {604var _bEntries$_i = _slicedToArray(bEntries[_i2], 2),605key = _bEntries$_i[0],606item = _bEntries$_i[1];607608if (_typeof(key) === 'object' && key !== null) {609if (!mapHasEqualEntry(set, a, key, item, strict, memo)) return false;610} else if (!strict && (!a.has(key) || !innerDeepEqual(a.get(key), item, false, memo)) && !mapHasEqualEntry(set, a, key, item, false, memo)) {611return false;612}613}614615return set.size === 0;616}617618return true;619}620621function objEquiv(a, b, strict, keys, memos, iterationType) {622// Sets and maps don't have their entries accessible via normal object623// properties.624var i = 0;625626if (iterationType === kIsSet) {627if (!setEquiv(a, b, strict, memos)) {628return false;629}630} else if (iterationType === kIsMap) {631if (!mapEquiv(a, b, strict, memos)) {632return false;633}634} else if (iterationType === kIsArray) {635for (; i < a.length; i++) {636if (hasOwnProperty(a, i)) {637if (!hasOwnProperty(b, i) || !innerDeepEqual(a[i], b[i], strict, memos)) {638return false;639}640} else if (hasOwnProperty(b, i)) {641return false;642} else {643// Array is sparse.644var keysA = Object.keys(a);645646for (; i < keysA.length; i++) {647var key = keysA[i];648649if (!hasOwnProperty(b, key) || !innerDeepEqual(a[key], b[key], strict, memos)) {650return false;651}652}653654if (keysA.length !== Object.keys(b).length) {655return false;656}657658return true;659}660}661} // The pair must have equivalent values for every corresponding key.662// Possibly expensive deep test:663664665for (i = 0; i < keys.length; i++) {666var _key = keys[i];667668if (!innerDeepEqual(a[_key], b[_key], strict, memos)) {669return false;670}671}672673return true;674}675676function isDeepEqual(val1, val2) {677return innerDeepEqual(val1, val2, kLoose);678}679680function isDeepStrictEqual(val1, val2) {681return innerDeepEqual(val1, val2, kStrict);682}683684module.exports = {685isDeepEqual: isDeepEqual,686isDeepStrictEqual: isDeepStrictEqual687};688689