equals.js 920 B

123456789101112131415161718192021222324252627282930313233343536
  1. var hasOwn = require('./hasOwn');
  2. var every = require('./every');
  3. var isObject = require('../lang/isObject');
  4. function defaultCompare(a, b) {
  5. return a === b;
  6. }
  7. // Makes a function to compare the object values from the specified compare
  8. // operation callback.
  9. function makeCompare(callback) {
  10. return function(value, key) {
  11. return hasOwn(this, key) && callback(value, this[key]);
  12. };
  13. }
  14. function checkProperties(value, key) {
  15. return hasOwn(this, key);
  16. }
  17. /**
  18. * Checks if two objects have the same keys and values.
  19. */
  20. function equals(a, b, callback) {
  21. callback = callback || defaultCompare;
  22. if (!isObject(a) || !isObject(b)) {
  23. return callback(a, b);
  24. }
  25. return (every(a, makeCompare(callback), b) &&
  26. every(b, checkProperties, a));
  27. }
  28. module.exports = equals;