# egjs-list-differ **Repository Path**: mirrors_naver/egjs-list-differ ## Basic Information - **Project Name**: egjs-list-differ - **Description**: ➕➖🔄 A module that checks the diff when values are added, removed, or changed in an array. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-25 - **Last Updated**: 2026-05-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README

@egjs/list-differ

version Travis (.org) Coveralls github npm downloads per month GitHub

➕➖🔄 A module that checks the diff when values are added, removed, or changed in an array.

## ⚙️ Installation ```sh $ npm i @egjs/list-differ ``` ```html ``` ## 📖 Documentation * See [**Documentation**](https://naver.github.io/egjs-list-differ/release/latest/doc/index.html) page. * [Introducing ListDiffer to track changes in data and track changes](https://medium.com/naver-fe-platform/introducing-listdiffer-to-track-changes-in-data-27793f0c6f4a)([한국어](https://medium.com/naver-fe-platform/%EB%8D%B0%EC%9D%B4%ED%84%B0%EC%9D%98-%EB%B3%80%ED%99%94%EB%A5%BC-%EC%95%8C%EC%95%84%EB%82%B4%EA%B3%A0-%EB%B3%80%ED%99%94-%EA%B3%BC%EC%A0%95%EC%9D%84-%EC%B6%94%EC%A0%81-%ED%95%98%EB%8A%94-listdiffer-9c3f1d770542)) * [How to Make Cross Framework Component](https://medium.com/naver-fe-platform/how-to-make-cross-framework-component-ee76d76708b1)([한국어](https://medium.com/naver-fe-platform/cross-framework-component%EB%A5%BC-%EB%A7%8C%EB%93%9C%EB%8A%94-%EB%B0%A9%EB%B2%95-234b3fece353)) ## 📦 Packages |Package|Version|Description| |:-----:|:-----:|:-----:| |[**@egjs/children-differ**](https://github.com/naver/egjs-children-differ)|version|A module that checks diff in DOM children.| |[**@egjs/react-children-differ**](https://github.com/naver/egjs-children-differ/blob/master/packages/react-children-differ/README.md)|version|React [React](https://reactjs.org/) port of @egjs/children-differ| |[**@egjs/ngx-children-differ**](https://github.com/naver/egjs-children-differ/blob/master/packages/ngx-children-differ/README.md)|version|Angular [Angular](https://angular.io/) port of @egjs/children-differ| |[**@egjs/vue-children-differ**](https://github.com/naver/egjs-children-differ/blob/master/packages/vue-children-differ/README.md)|version|Vue.js [Vue.js](https://vuejs.org/v2/guide/index.html) port of @egjs/children-differ| ## 🏃 How to use ### checks the diff in array ```js import ListDiffer, { diff } from "@egjs/list-differ"; // script => eg.ListDiffer // Value is key const differ = new ListDiffer([1, 2, 3, 4, 5, 6, 7], e => e); // const result = diff([1, 2, 3, 4, 5, 6, 7], [4, 3, 8, 2, 1, 7], e => e); const result = differ.update([4, 3, 8, 2, 1, 7]); // List before update // [1, 2, 3, 4, 5, 6, 7] console.log(result.prevList); // Updated list // [4, 3, 8, 2, 1, 7] console.log(result.list); // Index array of values added to `list` // [2] console.log(result.added); // Index array of values removed in `prevList` // [5, 4] console.log(result.removed); // An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list` // [[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]] console.log(result.changed); // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>) // [[0, 3], [0, 2], [0, 1]] console.log(result.ordered); // An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved // [[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]] console.log(result.maintained); ``` ### What is changed? * **changed**: An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list` ||**changed**| |---:|---| ||[[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]]|[[[3, 0], [2, 1], [1, 3]]|| |prevList|![prev_list](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_prev_list.png)| |list|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_list.png)| ### What is ordered? An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>) ||removed -> ordered -> added| |---:|---| |prevList|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/prev_list.png)| |removed
[5, 4]|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/removed.png)| |ordered|[[0, 3], [0, 2], [0, 1]]| ||![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_added.gif)| |ordered[0]
[3 => 0]| ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_ordered0.png)| |ordered[1]
[3 => 1]| ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_ordered1.png)| |ordered[2]
[3 => 2]| ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_ordered2.png)| |added
[2]|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_added.png)| |list|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/list.png)| ## Data Sync Examples ```js import ListDiffer, { diff } from "@egjs/list-differ"; const prevList = [1, 2, 3, 4, 5, 6, 7]; const list = [4, 3, 8, 2, 1, 7]; // const differ = new ListDiffer(prevList, e => e); // const result = differ.update(list); const result = diff(prevList, list, e => e); ``` ### removed => ordered => added ||removed => ordered => added| |---|---| |prevList|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/prev_list.png)| |process|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_before_animation.gif)| |list|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/list.png)| * Synchronize List ```js const nextList = prevList.slice(); result.removed.forEach(index => { nextList.splice(index, 1); }); result.ordered.forEach(([from, to], i) => { const element = nextList.splice(from, 1)[0]; nextList.splice(to, 0, element); }); result.added.forEach(index => { nextList.splice(index, 0, list[index]); }); // `nextList` is the same as `list`. console.log(nextList); ``` * Synchronize DOM ```js const parentElement = document.querySelector(".parent"); const children = parentElement.children; result.removed.forEach(index => { children[index].remove(); }); result.ordered.forEach(([from, to]) => { parentElement.insertBefore(children[from], children[from < to ? to + 1 : to]); }); result.added.forEach(index => { parentElement.insertBefore(document.createElement("div"), children[index]); }); ``` ### removed => maintained => added ||maintained => added| |---|---| |prevList|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/prev_list.png)| |process|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/maintained_with_added.gif)| |list|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/list.png)| * Synchronize List ```js const nextList: number[] = []; result.removed.forEach(index => { // There is a remove operation for the index. // prevList[index]; }); result.maintained.forEach(([from, to]) => { nextList[to] = list[to]; }); result.added.forEach(index => { nextList[index] = list[index]; }); // `nextList` is the same as `list`. console.log(nextList); ``` * Synchronize DOM ```js const parentElement = document.querySelector(".parent"); const children = parentElement.children; const prevChildren: Element[] = [].slice.call(parentElement.children); result.removed.forEach(index => { prevChildren[index].remove(); }); result.maintained.forEach(([from, to]) => { parentElement.appendChild(prevChildren[from]); }); result.added.forEach(index => { parentElement.insertBefore(document.createElement("div"), children[index]); }); ``` ## 🙌 Contributing See [CONTRIBUTING.md](https://github.com/naver/egjs-list-differ/blob/master/CONTRIBUTING.md). ## 📝 Feedback Please file an [Issue](https://github.com/naver/egjs-list-differ/issues). ## 📜 License @egjs/list-differ is released under the [MIT license](https://github.com/naver/egjs-list-differ/blob/master/LICENSE). ``` Copyright (c) 2019-present NAVER Corp. 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. ```