@egjs/list-differ

中文文档
1.0.1发布于 2023/04/07MITTypeScriptESM + CJS零依赖近 30 天下载 347.7万

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

原始使用文档
文档目录14 节
  1. @egjs/list-differ
  2. ⚙️ Installation
  3. 📖 Documentation
  4. 📦 Packages
  5. 🏃 How to use
  6. checks the diff in array
  7. What is changed?
  8. What is ordered?
  9. Data Sync Examples
  10. removed => ordered => added
  11. removed => maintaind => added
  12. 🙌 Contributing
  13. 📝 Feedback
  14. 📜 License

@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

$ npm i @egjs/list-differ
<script src="//naver.github.io/egjs-list-differ/release/latest/dist/list-differ.min.js"></script>

📖 Documentation

📦 Packages

PackageVersionDescription
@egjs/children-differversionA module that checks diff in DOM children.
@egjs/react-children-differversionReact React port of @egjs/children-differ
@egjs/ngx-children-differversionAngular Angular port of @egjs/children-differ
@egjs/vue-children-differversionVue.js Vue.js port of @egjs/children-differ

🏃 How to use

checks the diff in array

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, 6, 2, 1, 7], e => e);
const result = differ.update([4, 3, 6, 2, 1, 7]);
// List before update
// [1, 2, 3, 4, 5, 6, 7]
console.log(result.prevList);
// Updated list
// [4, 3, 6, 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);
// The subset of `changed` and an array of index pairs that moved data directly. Indicate an array of absolute index pairs of `ordered`.(Formatted by: Array<[index of prevList, index of list]>)
// [[3, 0], [2, 1], [1, 3]]
console.log(result.pureChanged);
// An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)
// [[3, 0], [3, 1], [3, 2]]
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
  • pureChanged: The subset of changed and an array of index pairs that moved data directly. Indicate an array of absolute index pairs of ordered.(Formatted by: Array<[index of prevList, index of list]>)
changedpureChanged:
[[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]][[[3, 0], [2, 1], [1, 3]]
prevListprev_list
process

-

list

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
removed
[5, 4]
ordered[[3, 0], [3, 1], [3, 2]]
ordered[0]
[3 => 0]
ordered[1]
[3 => 1]
ordered[2]
[3 => 2]
added
[2]
list

Data Sync Examples

import ListDiffer, { diff } from "@egjs/list-differ";

const prevList = [1, 2, 3, 4, 5, 6, 7];
const list = [4, 3, 6, 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
process
list
  • Synchronize List
const nextList = prevList.slice();
result.removed.forEach(index => {
  nextList.splice(index, 1);
});
result.ordered.forEach(([from, to], i) => {
  nextList.splice(from, 1);
  nextList.splice(to, 0, list[result.pureChanged[i][1]]);
});
result.added.forEach(index => {
  nextList.splice(index, 0, list[index]);
});
// `nextList` is the same as `list`.
console.log(nextList);
  • Synchronize DOM
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 => maintaind => added

maintaind => added
prevList
process
list
  • Synchronize List
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
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.

📝 Feedback

Please file an Issue.

📜 License

@egjs/list-differ is released under the MIT 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.

版本与发布记录

@egjs/list-differ 共发布 6 个版本,首次发布于 2019/05/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2023/04/073 年前
1.0.1-beta.0beta2023/04/063 年前
1.0.02019/08/057 年前
0.1.02019/07/317 年前
0.0.12019/05/107 年前
0.0.02019/05/087 年前

关于 @egjs/list-differ 的常见问题

@egjs/list-differ 是什么?

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

如何安装 @egjs/list-differ?

在项目目录运行 npm install @egjs/list-differ,或使用 pnpm add / yarn add / bun add。

@egjs/list-differ 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@egjs/list-differ 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@egjs/list-differ 可以商用吗?

@egjs/list-differ 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@egjs/list-differ 的下载量有多少?

根据 npm 官方下载统计,@egjs/list-differ 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,477,435 次,日均约 115,915 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@egjs/list-differ 还在维护吗?

最新版本 1.0.1 发布于 2023/04/07(3 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/05/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。