isEqual.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var baseIsEqual = require('../internal/baseIsEqual'),
  2. bindCallback = require('../internal/bindCallback');
  3. /**
  4. * Performs a deep comparison between two values to determine if they are
  5. * equivalent. If `customizer` is provided it's invoked to compare values.
  6. * If `customizer` returns `undefined` comparisons are handled by the method
  7. * instead. The `customizer` is bound to `thisArg` and invoked with up to
  8. * three arguments: (value, other [, index|key]).
  9. *
  10. * **Note:** This method supports comparing arrays, booleans, `Date` objects,
  11. * numbers, `Object` objects, regexes, and strings. Objects are compared by
  12. * their own, not inherited, enumerable properties. Functions and DOM nodes
  13. * are **not** supported. Provide a customizer function to extend support
  14. * for comparing other values.
  15. *
  16. * @static
  17. * @memberOf _
  18. * @alias eq
  19. * @category Lang
  20. * @param {*} value The value to compare.
  21. * @param {*} other The other value to compare.
  22. * @param {Function} [customizer] The function to customize value comparisons.
  23. * @param {*} [thisArg] The `this` binding of `customizer`.
  24. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  25. * @example
  26. *
  27. * var object = { 'user': 'fred' };
  28. * var other = { 'user': 'fred' };
  29. *
  30. * object == other;
  31. * // => false
  32. *
  33. * _.isEqual(object, other);
  34. * // => true
  35. *
  36. * // using a customizer callback
  37. * var array = ['hello', 'goodbye'];
  38. * var other = ['hi', 'goodbye'];
  39. *
  40. * _.isEqual(array, other, function(value, other) {
  41. * if (_.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/)) {
  42. * return true;
  43. * }
  44. * });
  45. * // => true
  46. */
  47. function isEqual(value, other, customizer, thisArg) {
  48. customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined;
  49. var result = customizer ? customizer(value, other) : undefined;
  50. return result === undefined ? baseIsEqual(value, other, customizer) : !!result;
  51. }
  52. module.exports = isEqual;