value-equal.js 877 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. function valueOf(obj) {
  3. return obj.valueOf ? obj.valueOf() : Object.prototype.valueOf.call(obj);
  4. }
  5. function valueEqual(a, b) {
  6. // Test for strict equality first.
  7. if (a === b) return true;
  8. // Otherwise, if either of them == null they are not equal.
  9. if (a == null || b == null) return false;
  10. if (Array.isArray(a)) {
  11. return (
  12. Array.isArray(b) &&
  13. a.length === b.length &&
  14. a.every(function(item, index) {
  15. return valueEqual(item, b[index]);
  16. })
  17. );
  18. }
  19. if (typeof a === 'object' || typeof b === 'object') {
  20. var aValue = valueOf(a);
  21. var bValue = valueOf(b);
  22. if (aValue !== a || bValue !== b) return valueEqual(aValue, bValue);
  23. return Object.keys(Object.assign({}, a, b)).every(function(key) {
  24. return valueEqual(a[key], b[key]);
  25. });
  26. }
  27. return false;
  28. }
  29. module.exports = valueEqual;