Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array unpatch get wrong result #295

Open
yangwch opened this issue Aug 6, 2020 · 0 comments
Open

array unpatch get wrong result #295

yangwch opened this issue Aug 6, 2020 · 0 comments

Comments

@yangwch
Copy link

yangwch commented Aug 6, 2020

my code

// config
const instance = create({
  objectHash(obj, index) {
    if (typeof obj._id !== 'undefined') {
      return obj._id;
    }
    if (typeof obj.id !== 'undefined') {
      return obj.id;
    }
    if (typeof obj.name !== 'undefined') {
      return obj.name;
    }
    return `$$index:${index}`;
  },
  cloneDiffValues: true,
  arrays: {
    // default true, detect items moved inside the array
    // (otherwise they will be registered as remove+add)
    detectMove: true,
    // default false, the value of items moved is not included in deltas
    includeValueOnMove: false,
  },
});

const comps = [
  { id: 1, style: { zIndex: 1 } },
  { id: 2, style: { zIndex: 2 } },
  { id: 3, style: { zIndex: 3 } },
  { id: 4, style: { zIndex: 4 } },
  { id: 5, style: { zIndex: 5 } },
  { id: 6, style: { zIndex: 6 } },
  { id: 7, style: { zIndex: 7 } },
  { id: 8, style: { zIndex: 8 } },
  { id: 9, style: { zIndex: 9 } },
];
const orderedComps = [
  { id: 7, style: { zIndex: 1 } },
  { id: 8, style: { zIndex: 2 } },
  { id: 9, style: { zIndex: 3 } },
  { id: 1, style: { zIndex: 4 } },
  { id: 2, style: { zIndex: 5 } },
  { id: 3, style: { zIndex: 6 } },
  { id: 4, style: { zIndex: 7 } },
  { id: 5, style: { zIndex: 8 } },
  { id: 6, style: { zIndex: 9 } },
];
const delta = instance.diff(comps, orderedComps);
const undoComps = instance.unpatch(orderedComps, delta);
console.log('isEq', isEqual(undoComps, comps), undoComps)

result

isEq false [
  {"id":1,"style":{"zIndex":4}},
  {"id":2,"style":{"zIndex":1}},
  {"id":3,"style":{"zIndex":3}},
  {"id":4,"style":{"zIndex":4}},
  {"id":5,"style":{"zIndex":8}},
  {"id":6,"style":{"zIndex":5}},
  {"id":7,"style":{"zIndex":7}},
  {"id":8,"style":{"zIndex":6}},
  {"id":9,"style":{"zIndex":9}}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant