Skip to content

monajs/list-diff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

List Diff(O(N))

✨✨ 对比两个列表的差异,计算最小编辑距离

npm npm

相关文档

安装

$ npm i --save @monajs/list-diff

使用

import listDiff from '@monajs/list-diff'

const oldList = [
    { key: '1', name: 'a' },
    { key: '2', name: 'b' },
    { key: '3', name: 'c' },
    { key: '4', name: 'd' }
]
const newList = [
    { key: '2', name: 'b' },
    { key: '5', name: 'e' },
    { key: '6', name: 'f' },
    { key: '1', name: 'a' },
    { key: '7', name: 'g' },
    { key: '4', name: 'd' }
]

const diff = listDiff(oldList, newList, 'key')
console.log(diff.moves)

output:

[{
    type: 'insert',
    originIndex: null,
    currentIndex: 1,
    item: { key: '5', name: 'e' }
}, {
    type: 'insert',
    originIndex: null,
    currentIndex: 2,
    item: { key: '6', name: 'f' }
}, {
    type: 'move',
    originIndex: 0,
    currentIndex: 3,
    item: { key: '1', name: 'a' }
}, {
    type: 'insert',
    originIndex: null,
    currentIndex: 4,
    item: { key: '7', name: 'g' }
}, {
    type: 'remove',
    originIndex: 2,
    currentIndex: null,
    item: { key: '3', name: 'c' }
}]

联系我

微信:yx12032331

Releases

No releases published

Packages

No packages published