intelie / immutable-js-diff

MIT License
261 stars 32 forks source link

exchange position but result asymmetry! #11

Open ascoders opened 8 years ago

ascoders commented 8 years ago

Occurs when complex objects

json1:

{"component":"gaea-layout","props":{"name":"外壳","options":{"width":{"value":"100%","editable":false},"flexGrow":{"value":1,"editable":false},"flexDirection":{"value":"column"}}},"childs":[{"component":"gaea-layout","childs":[{"component":"title","props":{"options":{"text":{"value":"Gaea"}}}}],"props":{"options":{"alignItems":{"value":"stretch"},"flexDirection":{"value":"column"}}}},{"component":"gaea-layout","childs":[{"component":"gaea-layout","props":{"options":{"width":{"value":"50%"},"alignItems":{"value":"center"},"justifyContent":{"value":"center"},"minHeight":{"value":50}}}},{"component":"gaea-layout","props":{"options":{"width":{"value":"50%"},"alignItems":{"value":"center"},"justifyContent":{"value":"center"},"minHeight":{"value":50}}},"childs":[{"component":"gaea-paragraph","props":{"options":{"text":{"value":"支持自由定制组件+细粒度通用组件"},"fontSize":{"value":16}}}}]}],"props":{"options":{"minHeight":{"value":50}}}},{"component":"gaea-paragraph","props":{"options":{"text":{"value":"无限层级嵌套+任意组合"},"fontSize":{"value":16}}}}]}

json2:

{"component":"gaea-layout","props":{"name":"外壳","options":{"width":{"value":"100%","editable":false},"flexGrow":{"value":1,"editable":false},"flexDirection":{"value":"column"}}},"childs":[{"component":"gaea-layout","childs":[{"component":"title","props":{"options":{"text":{"value":"Gaea"}}}}],"props":{"options":{"alignItems":{"value":"stretch"},"flexDirection":{"value":"column"}}}},{"component":"gaea-layout","childs":[{"component":"gaea-layout","props":{"options":{"width":{"value":"50%"},"alignItems":{"value":"center"},"justifyContent":{"value":"center"},"minHeight":{"value":50}}},"childs":[{"component":"gaea-paragraph","props":{"options":{"text":{"value":"无限层级嵌套+任意组合"},"fontSize":{"value":16}}}}]},{"component":"gaea-layout","props":{"options":{"width":{"value":"50%"},"alignItems":{"value":"center"},"justifyContent":{"value":"center"},"minHeight":{"value":50}}},"childs":[{"component":"gaea-paragraph","props":{"options":{"text":{"value":"支持自由定制组件+细粒度通用组件"},"fontSize":{"value":16}}}}]}],"props":{"options":{"minHeight":{"value":50}}}}]}

and run:

var test1 = require('./test1.json')
var test2 = require('./test2.json')
var map1 = Immutable.fromJS(test1)
var map2 = Immutable.fromJS(test2)

console.log(diff(map1, map2).toJS())
console.log(diff(map2, map1).toJS())

here is the result:

bug

The second is correct

ascoders commented 8 years ago

any one help?

pedroteixeira commented 8 years ago

Hi ascoders, the diff function is not a symetrical operation, the produced patch operations are relative to the first object. If you could create a test case, I'll have a look into the issue. best regards!

ascoders commented 8 years ago

hi pedroteixeira, thanks for your help, here is the test case:

git clone https://github.com/ascoders/immutable-js-diff-test-case.git
cd immutable-js-diff-test-case
npm install
node index.js

and you will see this:

image

the second output is correct, i think the first output should like this:

[ { op: 'remove', path: '/childs/2', value: { component: 'gaea-paragraph', props: [Object] } } , { op: 'add', path: '/childs/1/childs/0/childs' },]

ascoders commented 8 years ago

hello

ascoders commented 8 years ago

@pedroteixeira ^^