• Stars
    star
    176
  • Rank 217,053 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Diff two lists in O(n).

list-diff

build codecov.io npm version Dependency Status

js-standard-style

Introduction

Diff two lists in time O(n). I The algorithm finding the minimal amount of moves is Levenshtein distance which is O(n*m). This algorithm is not the best but is enougth for front-end DOM list manipulation.

This project is mostly influenced by virtual-dom algorithm.

Install

$ npm install list-diff2 --save

Usage

var diff = require("list-diff2")
var oldList = [{id: "a"}, {id: "b"}, {id: "c"}, {id: "d"}, {id: "e"}]
var newList = [{id: "c"}, {id: "a"}, {id: "b"}, {id: "e"}, {id: "f"}]

var moves = diff(oldList, newList, "id")
// `moves` is a sequence of actions (remove or insert): 
// type 0 is removing, type 1 is inserting
// moves: [
//   {index: 3, type: 0},
//   {index: 0, type: 1, item: {id: "c"}}, 
//   {index: 3, type: 0}, 
//   {index: 4, type: 1, item: {id: "f"}}
//  ]

moves.moves.forEach(function(move) {
  if (move.type === 0) {
    oldList.splice(move.index, 1) // type 0 is removing
  } else {
    oldList.splice(move.index, 0, move.item) // type 1 is inserting
  }
})

// now `oldList` is equal to `newList`
// [{id: "c"}, {id: "a"}, {id: "b"}, {id: "e"}, {id: "f"}]
console.log(oldList) 

License

The MIT License (MIT)

Copyright (c) 2015 Livoras

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

blog

Too young, too simple. Sometimes, naive.
Ruby
3,545
star
2

simple-virtual-dom

Basic virtual-dom algorithm
JavaScript
1,698
star
3

MVW-demos

Sample Codes for Model-View-Whatever Patterns
JavaScript
104
star
4

nestscript

A script nested in JavaScript, dynamically run code in environment without `eval` and `new Function`.
JavaScript
82
star
5

virtual-template

Combining Template Engine and Virtual-DOM
JavaScript
76
star
6

wx-arduino

wx-arduino
Python
35
star
7

amini

Amini 是一个 Angular 风格的微信小程序开发库
TypeScript
22
star
8

feb

Simple Front-end Development Workflow
JavaScript
20
star
9

home

new home
JavaScript
20
star
10

backbone.Composite

A plugin for backbone.js. Use composite pattern for backbone's views.
JavaScript
14
star
11

interview

12
star
12

stereojs

for communication between browser‘s tabs
JavaScript
10
star
13

genea

Genetic Algorithm for JavaScript
JavaScript
6
star
14

de-mini

突破微信小程序限制,动态加载代码。
4
star
15

swagger-mocker

Create Mock API Server from Swagger 2.0 JSON file.
Go
4
star
16

QLearning

A simple QLeaning Agent in Golang
Go
3
star
17

feifanote

Extremely Pure Online Notebook for Everyone (Front-end).
JavaScript
3
star
18

nestscript-demo

使用 nestscript 编译的开源伪 3D 游戏
JavaScript
3
star
19

easydict

The simplest dictionary on Linux.
Python
3
star
20

ts-mixins

Mixins In TypeScript with Code Suggestions.
2
star
21

shortest-path-in-diagram

shortest-path-in-diagram
TypeScript
2
star
22

saber

Developing desktop like JavaScript Application integrating knockoutjs, jQuery and requirejs.
JavaScript
2
star
23

too-lazy

Too Lazy Too Write Markup Language.
TypeScript
2
star
24

shooting-chicken

Epic HTML5 Game By Livoras
JavaScript
2
star
25

go-web-utilities

Golang Web Utilities Leverage on Gin
Go
1
star
26

pauo

TypeScript Runtime base on Golang without V8
1
star
27

outlet.js

A Bridge Between JavaScript and HTML.
1
star
28

vue-boilerplate

A boilerplate for building large app with vuejs.
CoffeeScript
1
star
29

neuron

神经元实验
TypeScript
1
star
30

meta-draw

Framework for drawing everything.
1
star
31

easorm

Database without pain.
1
star
32

auto-ad

Python
1
star
33

min-app-store

Wechat Mini App Store
JavaScript
1
star
34

polygon-line-intersection

polygon-line-intersection
1
star
35

tween-scroller

jQuery Tween Scroller Plugin
CoffeeScript
1
star
36

iqld-ad-picture-gen

TypeScript
1
star
37

wechat-picture-slide

wechat-picture-slide
JavaScript
1
star
38

wxminiapp2vue

Parser wxml to angular template
TypeScript
1
star
39

ragejs

RageJS, a JavaScript MVVM framework.
JavaScript
1
star