Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
@egjs/list-differ
Advanced tools
A module that checks the diff when values are added, removed, or changed in an array.
@egjs/list-differ is a lightweight JavaScript library designed to efficiently compute the differences between two lists. It is particularly useful for applications that need to update the DOM or other data structures based on changes in a list, such as in virtual DOM implementations or state management libraries.
Compute Differences Between Lists
This feature allows you to compute the differences between two lists. The `update` method returns an object that describes the changes needed to transform the old list into the new list.
const { ListDiffer } = require('@egjs/list-differ');
const oldList = ['a', 'b', 'c'];
const newList = ['b', 'c', 'd'];
const differ = new ListDiffer();
const result = differ.update(oldList, newList);
console.log(result);
Track Insertions and Deletions
This feature allows you to track which items have been added or removed from the list. The `added` and `removed` properties of the result object provide the indices of the added and removed items, respectively.
const { ListDiffer } = require('@egjs/list-differ');
const oldList = ['a', 'b', 'c'];
const newList = ['b', 'c', 'd'];
const differ = new ListDiffer();
const result = differ.update(oldList, newList);
console.log(result.added); // [2]
console.log(result.removed); // [0]
Track Moved Items
This feature allows you to track items that have been moved within the list. The `maintained` property of the result object provides the indices of the items that have been moved.
const { ListDiffer } = require('@egjs/list-differ');
const oldList = ['a', 'b', 'c'];
const newList = ['c', 'a', 'b'];
const differ = new ListDiffer();
const result = differ.update(oldList, newList);
console.log(result.maintained); // [2, 0, 1]
The 'diff' package provides a way to compute the differences between two sequences, including lists and strings. It offers a more general-purpose diffing algorithm compared to @egjs/list-differ, which is specifically optimized for list diffing.
The 'fast-diff' package is a lightweight library for computing the differences between two strings. While it is optimized for string diffing, it can also be adapted for list diffing, though it may not be as efficient as @egjs/list-differ for that specific use case.
The 'deep-diff' package allows you to compute the differences between two JavaScript objects, including nested structures. It is more versatile than @egjs/list-differ but may be overkill if you only need to diff flat lists.
➕➖🔄 A module that checks the diff when values are added, removed, or changed in an array.
$ npm i @egjs/list-differ
<script src="//naver.github.io/egjs-list-differ/release/latest/dist/list-differ.min.js"></script>
Package | Version | Description |
---|---|---|
@egjs/children-differ | A module that checks diff in DOM children. | |
@egjs/react-children-differ | React port of @egjs/children-differ | |
@egjs/react-children-differ | Angular port of @egjs/children-differ | |
@egjs/vue-children-differ | Vue.js port of @egjs/children-differ |
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);
prevList
and list
with different indexes from prevList
and list
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]>)changed | pureChanged: | |
---|---|---|
[[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]] | [[[3, 0], [2, 1], [1, 3]] | |
prevList | ||
process | - | |
list |
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 |
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 | |
---|---|
prevList | |
process | |
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);
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]);
});
maintaind => added | |
---|---|
prevList | |
process | |
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);
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]);
});
See CONTRIBUTING.md.
Please file an Issue.
@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.
FAQs
A module that checks the diff when values are added, removed, or changed in an array.
The npm package @egjs/list-differ receives a total of 114,437 weekly downloads. As such, @egjs/list-differ popularity was classified as popular.
We found that @egjs/list-differ demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.