shallowequal.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { toRaw } from 'vue';
  2. function shallowEqual(objA, objB, compare, compareContext) {
  3. let ret = compare ? compare.call(compareContext, objA, objB) : void 0;
  4. if (ret !== void 0) {
  5. return !!ret;
  6. }
  7. if (objA === objB) {
  8. return true;
  9. }
  10. if (typeof objA !== 'object' || !objA || typeof objB !== 'object' || !objB) {
  11. return false;
  12. }
  13. const keysA = Object.keys(objA);
  14. const keysB = Object.keys(objB);
  15. if (keysA.length !== keysB.length) {
  16. return false;
  17. }
  18. const bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
  19. // Test for A's keys different from B.
  20. for (let idx = 0; idx < keysA.length; idx++) {
  21. const key = keysA[idx];
  22. if (!bHasOwnProperty(key)) {
  23. return false;
  24. }
  25. const valueA = objA[key];
  26. const valueB = objB[key];
  27. ret = compare ? compare.call(compareContext, valueA, valueB, key) : void 0;
  28. if (ret === false || ret === void 0 && valueA !== valueB) {
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34. export default function (value, other) {
  35. return shallowEqual(toRaw(value), toRaw(other));
  36. }