value-equal.js 1.1 KB

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