basant307/AI_Governance_Project
048
1import { isBuffer } from './isBuffer.mjs';2import { isPlainObject } from './isPlainObject.mjs';3import { getSymbols } from '../compat/_internal/getSymbols.mjs';4import { getTag } from '../compat/_internal/getTag.mjs';5import { functionTag, regexpTag, symbolTag, dateTag, booleanTag, numberTag, stringTag, objectTag, errorTag, dataViewTag, arrayBufferTag, float64ArrayTag, float32ArrayTag, bigInt64ArrayTag, int32ArrayTag, int16ArrayTag, int8ArrayTag, bigUint64ArrayTag, uint32ArrayTag, uint16ArrayTag, uint8ClampedArrayTag, uint8ArrayTag, arrayTag, setTag, mapTag, argumentsTag } from '../compat/_internal/tags.mjs';6import { isEqualsSameValueZero } from '../_internal/isEqualsSameValueZero.mjs';7 8function isEqualWith(a, b, areValuesEqual) {9 return isEqualWithImpl(a, b, undefined, undefined, undefined, undefined, areValuesEqual);10}11function isEqualWithImpl(a, b, property, aParent, bParent, stack, areValuesEqual) {12 const result = areValuesEqual(a, b, property, aParent, bParent, stack);13 if (result !== undefined) {14 return result;15 }16 if (typeof a === typeof b) {17 switch (typeof a) {18 case 'bigint':19 case 'string':20 case 'boolean':21 case 'symbol':22 case 'undefined': {23 return a === b;24 }25 case 'number': {26 return a === b || Object.is(a, b);27 }28 case 'function': {29 return a === b;30 }31 case 'object': {32 return areObjectsEqual(a, b, stack, areValuesEqual);33 }34 }35 }36 return areObjectsEqual(a, b, stack, areValuesEqual);37}38function areObjectsEqual(a, b, stack, areValuesEqual) {39 if (Object.is(a, b)) {40 return true;41 }42 let aTag = getTag(a);43 let bTag = getTag(b);44 if (aTag === argumentsTag) {45 aTag = objectTag;46 }47 if (bTag === argumentsTag) {48 bTag = objectTag;49 }50 if (aTag !== bTag) {51 return false;52 }53 switch (aTag) {54 case stringTag:55 return a.toString() === b.toString();56 case numberTag: {57 const x = a.valueOf();58 const y = b.valueOf();59 return isEqualsSameValueZero(x, y);60 }61 case booleanTag:62 case dateTag:63 case symbolTag:64 return Object.is(a.valueOf(), b.valueOf());65 case regexpTag: {66 return a.source === b.source && a.flags === b.flags;67 }68 case functionTag: {69 return a === b;70 }71 }72 stack = stack ?? new Map();73 const aStack = stack.get(a);74 const bStack = stack.get(b);75 if (aStack != null && bStack != null) {76 return aStack === b;77 }78 stack.set(a, b);79 stack.set(b, a);80 try {81 switch (aTag) {82 case mapTag: {83 if (a.size !== b.size) {84 return false;85 }86 for (const [key, value] of a.entries()) {87 if (!b.has(key) || !isEqualWithImpl(value, b.get(key), key, a, b, stack, areValuesEqual)) {88 return false;89 }90 }91 return true;92 }93 case setTag: {94 if (a.size !== b.size) {95 return false;96 }97 const aValues = Array.from(a.values());98 const bValues = Array.from(b.values());99 for (let i = 0; i < aValues.length; i++) {100 const aValue = aValues[i];101 const index = bValues.findIndex(bValue => {102 return isEqualWithImpl(aValue, bValue, undefined, a, b, stack, areValuesEqual);103 });104 if (index === -1) {105 return false;106 }107 bValues.splice(index, 1);108 }109 return true;110 }111 case arrayTag:112 case uint8ArrayTag:113 case uint8ClampedArrayTag:114 case uint16ArrayTag:115 case uint32ArrayTag:116 case bigUint64ArrayTag:117 case int8ArrayTag:118 case int16ArrayTag:119 case int32ArrayTag:120 case bigInt64ArrayTag:121 case float32ArrayTag:122 case float64ArrayTag: {123 if (isBuffer(a) !== isBuffer(b)) {124 return false;125 }126 if (a.length !== b.length) {127 return false;128 }129 for (let i = 0; i < a.length; i++) {130 if (!isEqualWithImpl(a[i], b[i], i, a, b, stack, areValuesEqual)) {131 return false;132 }133 }134 return true;135 }136 case arrayBufferTag: {137 if (a.byteLength !== b.byteLength) {138 return false;139 }140 return areObjectsEqual(new Uint8Array(a), new Uint8Array(b), stack, areValuesEqual);141 }142 case dataViewTag: {143 if (a.byteLength !== b.byteLength || a.byteOffset !== b.byteOffset) {144 return false;145 }146 return areObjectsEqual(new Uint8Array(a), new Uint8Array(b), stack, areValuesEqual);147 }148 case errorTag: {149 return a.name === b.name && a.message === b.message;150 }151 case objectTag: {152 const areEqualInstances = areObjectsEqual(a.constructor, b.constructor, stack, areValuesEqual) ||153 (isPlainObject(a) && isPlainObject(b));154 if (!areEqualInstances) {155 return false;156 }157 const aKeys = [...Object.keys(a), ...getSymbols(a)];158 const bKeys = [...Object.keys(b), ...getSymbols(b)];159 if (aKeys.length !== bKeys.length) {160 return false;161 }162 for (let i = 0; i < aKeys.length; i++) {163 const propKey = aKeys[i];164 const aProp = a[propKey];165 if (!Object.hasOwn(b, propKey)) {166 return false;167 }168 const bProp = b[propKey];169 if (!isEqualWithImpl(aProp, bProp, propKey, a, b, stack, areValuesEqual)) {170 return false;171 }172 }173 return true;174 }175 default: {176 return false;177 }178 }179 }180 finally {181 stack.delete(a);182 stack.delete(b);183 }184}185 186export { isEqualWith };187 