equals.js

  1. /**
  2. * @function equals
  3. * @description Autocurried function that checks for equality of 2 items
  4. * @param {*} x - Left side of the equality check
  5. * @param {*} y - Right side of the equality check
  6. * @return {boolean}
  7. * @example
  8. *
  9. * const nan = Number.NaN
  10. * equal(nan, nan) // returns true
  11. *
  12. * const arr1 = [0, 1, 2, 3]
  13. * const arr2 = [null, 1, 2, 3]
  14. * equals(arr1, arr2) // returns false
  15. *
  16. * equals(new Object({'a': 0, 'b': 1}), {'a': 0, 'b': 1}) // true
  17. *
  18. */
  19. import curry from './curry'
  20. import exists from './exists'
  21. import identical from './identical'
  22. import type from './type'
  23. import not from './not'
  24. import or from './or'
  25. import isObject from './object/is'
  26. import isArray from './array/is'
  27. import isBoolean from './boolean/is'
  28. import isNumber from './number/is'
  29. import isString from './string/is'
  30. import isDate from './date/is'
  31. import equalsArray from './array/equals'
  32. import equalsObject from './object/equals'
  33. export default curry(function equals (x, y) {
  34. if (identical(x, y)) return true
  35. if (type(x) !== type(y)) return false
  36. if (or(not(exists(x)), not(exists(y)))) return false
  37. if (x.constructor !== y.constructor) return false
  38. if (isString(x) || isBoolean(x) || isNumber(x) || isDate(x)) {
  39. return identical(x.valueOf(), y.valueOf())
  40. }
  41. if (isArray(x)) {
  42. return equalsArray(x, y)
  43. } else if (isObject(x)) {
  44. return equalsObject(x, y)
  45. }
  46. return false
  47. })