_equalByTag.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var Symbol = require('./_Symbol'),
  2. Uint8Array = require('./_Uint8Array'),
  3. eq = require('./eq'),
  4. equalArrays = require('./_equalArrays'),
  5. mapToArray = require('./_mapToArray'),
  6. setToArray = require('./_setToArray');
  7. /** Used to compose bitmasks for value comparisons. */
  8. var COMPARE_PARTIAL_FLAG = 1,
  9. COMPARE_UNORDERED_FLAG = 2;
  10. /** `Object#toString` result references. */
  11. var boolTag = '[object Boolean]',
  12. dateTag = '[object Date]',
  13. errorTag = '[object Error]',
  14. mapTag = '[object Map]',
  15. numberTag = '[object Number]',
  16. regexpTag = '[object RegExp]',
  17. setTag = '[object Set]',
  18. stringTag = '[object String]',
  19. symbolTag = '[object Symbol]';
  20. var arrayBufferTag = '[object ArrayBuffer]',
  21. dataViewTag = '[object DataView]';
  22. /** Used to convert symbols to primitives and strings. */
  23. var symbolProto = Symbol ? Symbol.prototype : undefined,
  24. symbolValueOf = symbolProto ? symbolProto.valueOf : undefined;
  25. /**
  26. * A specialized version of `baseIsEqualDeep` for comparing objects of
  27. * the same `toStringTag`.
  28. *
  29. * **Note:** This function only supports comparing values with tags of
  30. * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
  31. *
  32. * @private
  33. * @param {Object} object The object to compare.
  34. * @param {Object} other The other object to compare.
  35. * @param {string} tag The `toStringTag` of the objects to compare.
  36. * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.
  37. * @param {Function} customizer The function to customize comparisons.
  38. * @param {Function} equalFunc The function to determine equivalents of values.
  39. * @param {Object} stack Tracks traversed `object` and `other` objects.
  40. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  41. */
  42. function equalByTag(object, other, tag, bitmask, customizer, equalFunc, stack) {
  43. switch (tag) {
  44. case dataViewTag:
  45. if ((object.byteLength != other.byteLength) ||
  46. (object.byteOffset != other.byteOffset)) {
  47. return false;
  48. }
  49. object = object.buffer;
  50. other = other.buffer;
  51. case arrayBufferTag:
  52. if ((object.byteLength != other.byteLength) ||
  53. !equalFunc(new Uint8Array(object), new Uint8Array(other))) {
  54. return false;
  55. }
  56. return true;
  57. case boolTag:
  58. case dateTag:
  59. case numberTag:
  60. // Coerce booleans to `1` or `0` and dates to milliseconds.
  61. // Invalid dates are coerced to `NaN`.
  62. return eq(+object, +other);
  63. case errorTag:
  64. return object.name == other.name && object.message == other.message;
  65. case regexpTag:
  66. case stringTag:
  67. // Coerce regexes to strings and treat strings, primitives and objects,
  68. // as equal. See http://www.ecma-international.org/ecma-262/7.0/#sec-regexp.prototype.tostring
  69. // for more details.
  70. return object == (other + '');
  71. case mapTag:
  72. var convert = mapToArray;
  73. case setTag:
  74. var isPartial = bitmask & COMPARE_PARTIAL_FLAG;
  75. convert || (convert = setToArray);
  76. if (object.size != other.size && !isPartial) {
  77. return false;
  78. }
  79. // Assume cyclic values are equal.
  80. var stacked = stack.get(object);
  81. if (stacked) {
  82. return stacked == other;
  83. }
  84. bitmask |= COMPARE_UNORDERED_FLAG;
  85. // Recursively compare objects (susceptible to call stack limits).
  86. stack.set(object, other);
  87. var result = equalArrays(convert(object), convert(other), bitmask, customizer, equalFunc, stack);
  88. stack['delete'](object);
  89. return result;
  90. case symbolTag:
  91. if (symbolValueOf) {
  92. return symbolValueOf.call(object) == symbolValueOf.call(other);
  93. }
  94. }
  95. return false;
  96. }
  97. module.exports = equalByTag;