value-equal.js 860 B

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