Socket
Socket
Sign inDemoInstall

@egjs/list-differ

Package Overview
Dependencies
0
Maintainers
9
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1-beta.0 to 1.0.1

declaration/Link.d.ts

2

declaration/HashMap.d.ts
export default class HashMap<T> {
private object;
get(key: number | string): T | undefined;
get(key: number | string): T;
set(key: number | string, value: T): void;
}
import { DiffResult, ListFormat } from "./types";
/**
* A module that checks diff when values are added, removed, or changed in an array.
* @ko 배열 또는 오브젝트에서 값이 추가되거나 삭제되거나 순서가 변경사항을 체크하는 모듈입니다.
* @memberof eg
*/
declare class ListDiffer<T> {
private findKeyCallback?;
private list;
/**
* @param - Initializing Data Array. <ko> 초기 설정할 데이터 배열.</ko>
* @param - This callback function returns the key of the item. <ko> 아이템의 키를 반환하는 콜백 함수입니다.</ko>
* @example
* import ListDiffer from "@egjs/list-differ";
* // script => eg.ListDiffer
* const differ = new ListDiffer([0, 1, 2, 3, 4, 5], e => e);
* const result = differ.update([7, 8, 0, 4, 3, 6, 2, 1]);
* // List before update
* // [1, 2, 3, 4, 5]
* console.log(result.prevList);
* // Updated list
* // [4, 3, 6, 2, 1]
* console.log(result.list);
* // Index array of values added to `list`.
* // [0, 1, 5]
* console.log(result.added);
* // Index array of values removed in `prevList`.
* // [5]
* console.log(result.removed);
* // An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list`.
* // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
* 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]>)
* // [[4, 3], [3, 4], [2, 6]]
* console.log(result.pureChanged);
* // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)
* // [[4, 1], [4, 2], [4, 3]]
* console.log(result.ordered);
* // An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved.
* // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
* console.log(result.maintained);
*/
constructor(list?: ListFormat<T>, findKeyCallback?: (e: T, i: number, arr: T[]) => number | string);
/**
* Update list.
* @ko 리스트를 업데이트를 합니다.
* @param - List to update <ko> 업데이트할 리스트 </ko>
* @return - Returns the results of an update from `prevList` to `list`.<ko> `prevList`에서 `list`로 업데이트한 결과를 반환한다. </ko>
*/
update(list: ListFormat<T>): DiffResult<T>;
}
export default ListDiffer;

@@ -1,6 +0,6 @@

export default class PolyMap<T, U> {
export default class PolyMap<T> {
private keys;
private values;
get(key: T): U;
set(key: T, value: U): void;
get(key: T): number;
set(key: T, value: number): void;
}

@@ -1,21 +0,16 @@

import { MaintainedRecord, CurrentRecord, OrderedRecord, PrevRecord } from "./types";
export default class Result<T = any> {
prevList: T[];
list: T[];
added: CurrentRecord<T>[];
removed: PrevRecord<T>[];
changed: MaintainedRecord<T>[];
maintained: MaintainedRecord<T>[];
private orderPriority;
added: number[];
removed: number[];
changed: number[][];
maintained: number[][];
private changedBeforeAdded;
private fixed;
private cacheOrdered;
constructor(prevList: T[], list: T[], added: CurrentRecord<T>[], removed: PrevRecord<T>[], changed: MaintainedRecord<T>[], maintained: MaintainedRecord<T>[], orderPriority: number[]);
readonly ordered: OrderedRecord<T>[];
private cachePureChanged;
constructor(prevList: T[], list: T[], added: number[], removed: number[], changed: number[][], maintained: number[][], changedBeforeAdded: number[][], fixed: boolean[]);
readonly ordered: number[][];
readonly pureChanged: number[][];
private caculateOrdered;
forEachAdded(fn: (record: CurrentRecord<T>) => void): void;
forEachAddedRight(fn: (record: CurrentRecord<T>) => void): void;
forEachRemoved(fn: (record: PrevRecord<T>) => void): void;
forEachChanged(fn: (record: MaintainedRecord<T>) => void): void;
forEachOrdered(fn: (record: OrderedRecord<T>) => void): void;
forEachMaintained(fn: (record: MaintainedRecord<T>) => void): void;
forEachMaintainedRight(fn: (record: MaintainedRecord<T>) => void): void;
}

@@ -1,6 +0,6 @@

export interface MapInterface<T, U> {
export interface MapInteface<T, U> {
get(key: T): U | undefined;
set(key: T, value: U): any;
}
export declare type MapConstructor<T, U> = new () => MapInterface<T, U>;
export declare type MapConstructor<T, U> = new () => MapInteface<T, U>;
export interface ListFormat<T = any> {

@@ -10,42 +10,23 @@ [index: number]: T;

}
export interface PrevRecord<T> {
prevItem: T;
prevIndex: number;
}
export interface CurrentRecord<T> {
currentItem: T;
currentIndex: number;
}
export interface MaintainedRecord<T> {
prevItem: T;
currentItem: T;
prevIndex: number;
currentIndex: number;
}
export interface OrderedRecord<T> {
prevItem: T;
currentItem: T;
anchor: T | null;
prevIndex: number;
currentIndex: number;
beforeOrderIndex: number;
afterOrderIndex: number;
anchorIndex: number;
}
declare type EachMethod<T> = (fn: (record: T) => void) => void;
/**
* @typedef
* @memberof eg.ListDiffer
* @property - List before update <ko>업데이트하기 전 데이터</ko>
* @property - Updated list <ko>업데이트하는 데이터</ko>
* @property - Index array of values added to `list` <ko>`list`에서 추가되는 데이터의 인덱스 배열</ko>
* @property - Index array of values removed in `prevList` <ko>`prevList`에서 제거되는 데이터의 인덱스 배열</ko>
* @property - An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list`<ko>이전 리스트`prevList`와 지금 리스트`list`에서 위치가 다른 `prevList`와 `list`의 인덱스 배열들</ko>
* @property - An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>) <ko>데이터를 추가하기 전 `list`를 동기화할 수 있는 정렬되는 인덱스 배열들(형태: Array<[이전 인덱스, 다음 인덱스]>) </ko>
* @property - 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]>)<ko>`changed`의 부분집합으로 데이터를 추가하기 전 직접 움직이는 데이터의 인덱스 배열들. `ordered`의 절대적인 인덱스 배열들을 나타내기도 한다. (형태: Array<[prevList인덱스, list인덱스]>)</ko>
* @property - An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved<ko>추가/삭제 되지 않아 데이터가 보존된 `prevList`와 `list`의 인덱스 배열들</ko>
*/
export interface DiffResult<T> {
prevList: T[];
list: T[];
added: CurrentRecord<T>[];
removed: PrevRecord<T>[];
changed: MaintainedRecord<T>[];
ordered: OrderedRecord<T>[];
maintained: MaintainedRecord<T>[];
forEachAdded: EachMethod<CurrentRecord<T>>;
forEachAddedRight: EachMethod<CurrentRecord<T>>;
forEachRemoved: EachMethod<PrevRecord<T>>;
forEachChanged: EachMethod<MaintainedRecord<T>>;
forEachOrdered: EachMethod<OrderedRecord<T>>;
forEachMaintained: EachMethod<MaintainedRecord<T>>;
added: number[];
removed: number[];
changed: number[][];
ordered: number[][];
pureChanged: number[][];
maintained: number[][];
}
export {};
import { DiffResult } from "./types";
export declare function diff<T>(prevList: T[], list: T[], findKeyCallback?: (e: T, i: number, arr: T[]) => unknown): DiffResult<T>;
/**
*
* @memberof eg.ListDiffer
* @static
* @function
* @param - Previous List <ko> 이전 목록 </ko>
* @param - List to Update <ko> 업데이트 할 목록 </ko>
* @param - This callback function returns the key of the item. <ko> 아이템의 키를 반환하는 콜백 함수입니다.</ko>
* @return - Returns the diff between `prevList` and `list` <ko> `prevList`와 `list`의 다른 점을 반환한다.</ko>
* @example
* import { diff } from "@egjs/list-differ";
* // script => eg.ListDiffer.diff
* const result = diff([0, 1, 2, 3, 4, 5], [7, 8, 0, 4, 3, 6, 2, 1], e => e);
* // List before update
* // [1, 2, 3, 4, 5]
* console.log(result.prevList);
* // Updated list
* // [4, 3, 6, 2, 1]
* console.log(result.list);
* // Index array of values added to `list`
* // [0, 1, 5]
* console.log(result.added);
* // Index array of values removed in `prevList`
* // [5]
* console.log(result.removed);
* // An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list`
* // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
* 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]>)
* // [[4, 3], [3, 4], [2, 6]]
* console.log(result.pureChanged);
* // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)
* // [[4, 1], [4, 2], [4, 3]]
* console.log(result.ordered);
* // An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved
* // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
* console.log(result.maintained);
*/
export declare function diff<T>(prevList: T[], list: T[], findKeyCallback?: (e: T, i: number, arr: T[]) => any): DiffResult<T>;

@@ -7,3 +7,3 @@ /*

repository: https://github.com/naver/egjs-list-differ
version: 1.0.0-snapshot
version: 1.0.1
*/

@@ -15,4 +15,28 @@ /*

*/
var SUPPORT_MAP = typeof Map === "function";
var PolyMap =
/*#__PURE__*/
function () {
function PolyMap() {
this.keys = [];
this.values = [];
}
var __proto = PolyMap.prototype;
__proto.get = function (key) {
return this.values[this.keys.indexOf(key)];
};
__proto.set = function (key, value) {
var keys = this.keys;
var values = this.values;
var prevIndex = keys.indexOf(key);
var index = prevIndex === -1 ? keys.length : prevIndex;
keys[index] = key;
values[index] = value;
};
return PolyMap;
}();
/*

@@ -48,145 +72,94 @@ egjs-list-differ

*/
var PolyMap =
var SUPPORT_MAP = typeof Map === "function";
/*
egjs-list-differ
Copyright (c) 2019-present NAVER Corp.
MIT license
*/
var Link =
/*#__PURE__*/
function () {
function PolyMap() {
this.keys = [];
this.values = [];
}
function Link() {}
var __proto = PolyMap.prototype;
var __proto = Link.prototype;
__proto.get = function (key) {
return this.values[this.keys.indexOf(key)];
__proto.connect = function (prevLink, nextLink) {
this.prev = prevLink;
this.next = nextLink;
prevLink && (prevLink.next = this);
nextLink && (nextLink.prev = this);
};
__proto.set = function (key, value) {
var keys = this.keys;
var values = this.values;
var prevIndex = keys.indexOf(key);
var index = prevIndex === -1 ? keys.length : prevIndex;
keys[index] = key;
values[index] = value;
__proto.disconnect = function () {
// In double linked list, diconnect the interconnected relationship.
var prevLink = this.prev;
var nextLink = this.next;
prevLink && (prevLink.next = nextLink);
nextLink && (nextLink.prev = prevLink);
};
return PolyMap;
}();
__proto.getIndex = function () {
var link = this;
var index = -1;
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
while (link) {
link = link.prev;
++index;
}
return t;
return index;
};
return __assign.apply(this, arguments);
};
return Link;
}();
/**
* Find a subsequence of a given sequence in which the subsequence's elements are in increasing order,
* and in which the subsequence is as long as possible.
* @returns Longest increasing subsequence of input array
* @example
* LIS([1, 8, 2, 3]) === [1, 2, 3]
* LIS([5, 4, 3, 1, 2]) === [1, 2]
*/
/*
egjs-list-differ
Copyright (c) 2019-present NAVER Corp.
MIT license
*/
function LIS(arr) {
if (arr.length === 0) {
return [];
}
function orderChanged(changed, fixed) {
// It is roughly in the order of these examples.
// 4, 6, 0, 2, 1, 3, 5, 7
var fromLinks = []; // 0, 1, 2, 3, 4, 5, 6, 7
var table = [];
var path = [];
arr.forEach(function (_, i) {
table[i] = 1;
path[i] = -1;
});
arr.forEach(function (_, i) {
for (var j = i - 1; j > -1; j -= 1) {
if (table[j] + 1 > table[i] && arr[j] <= arr[i]) {
table[i] = table[j] + 1;
path[i] = j;
}
}
});
var maxIndex = 0;
table.forEach(function (x, i) {
if (x > table[maxIndex]) {
maxIndex = i;
}
});
var res = [];
var toLinks = [];
changed.forEach(function (_a) {
var from = _a[0],
to = _a[1];
var link = new Link();
fromLinks[from] = link;
toLinks[to] = link;
}); // `fromLinks` are connected to each other by double linked list.
while (maxIndex !== -1) {
res.push(arr[maxIndex]);
maxIndex = path[maxIndex];
}
return res.reverse();
}
function orderChanged(priorityList, maintained) {
var confirmed = {};
var ordered = [];
var length = priorityList.length;
LIS(priorityList).forEach(function (x) {
confirmed[x] = true;
fromLinks.forEach(function (link, i) {
link.connect(fromLinks[i - 1]);
});
return changed.filter(function (_, i) {
return !fixed[i];
}).map(function (_a, i) {
var from = _a[0],
to = _a[1];
for (var from = 0; from < length; from += 1) {
var priority = priorityList[from];
if (confirmed[priority]) {
continue;
if (from === to) {
return [0, 0];
}
var to = 0;
var fromLink = fromLinks[from];
var toLink = toLinks[to - 1];
var fromIndex = fromLink.getIndex(); // Disconnect the link connected to `fromLink`.
while (to < length) {
if (confirmed[priorityList[to]] && priorityList[to] > priority) {
break;
}
fromLink.disconnect(); // Connect `fromLink` to the right of `toLink`.
to += 1;
if (!toLink) {
fromLink.connect(undefined, fromLinks[0]);
} else {
fromLink.connect(toLink, toLink.next);
}
to < from && (to += 1);
confirmed[priority] = true;
var record = maintained[priority];
var anchorRecord = to < length ? maintained[priorityList[to]] : null;
ordered.push(__assign({
beforeOrderIndex: from,
afterOrderIndex: to - 1
}, record, {
anchorIndex: anchorRecord ? anchorRecord.currentIndex : -1,
anchor: anchorRecord ? anchorRecord.currentItem : null
}));
priorityList.splice(from, 1);
priorityList.splice(to - 1, 0, priority);
if (to > from) {
from -= 1;
}
}
return ordered;
var toIndex = fromLink.getIndex();
return [fromIndex, toIndex];
});
}

@@ -197,3 +170,3 @@

function () {
function Result(prevList, list, added, removed, changed, maintained, orderPriority) {
function Result(prevList, list, added, removed, changed, maintained, changedBeforeAdded, fixed) {
this.prevList = prevList;

@@ -205,3 +178,4 @@ this.list = list;

this.maintained = maintained;
this.orderPriority = orderPriority;
this.changedBeforeAdded = changedBeforeAdded;
this.fixed = fixed;
}

@@ -221,62 +195,36 @@

});
Object.defineProperty(__proto, "pureChanged", {
get: function () {
if (!this.cachePureChanged) {
this.caculateOrdered();
}
return this.cachePureChanged;
},
enumerable: true,
configurable: true
});
__proto.caculateOrdered = function () {
var ordered = orderChanged(this.orderPriority.slice(), this.maintained);
this.cacheOrdered = ordered;
};
var ordered = orderChanged(this.changedBeforeAdded, this.fixed);
var changed = this.changed;
var pureChanged = [];
this.cacheOrdered = ordered.filter(function (_a, i) {
var from = _a[0],
to = _a[1];
var _b = changed[i],
fromBefore = _b[0],
toBefore = _b[1];
__proto.forEachAdded = function (fn) {
this.added.forEach(function (record) {
return fn(record);
if (from !== to) {
pureChanged.push([fromBefore, toBefore]);
return true;
}
});
this.cachePureChanged = pureChanged;
};
__proto.forEachAddedRight = function (fn) {
var added = this.added;
added.forEach(function (_, i) {
var record = added[added.length - 1 - i];
fn(record);
});
};
__proto.forEachRemoved = function (fn) {
this.removed.forEach(function (record) {
return fn(record);
});
};
__proto.forEachChanged = function (fn) {
this.changed.forEach(function (record) {
return fn(record);
});
};
__proto.forEachOrdered = function (fn) {
this.ordered.forEach(function (record) {
return fn(record);
});
};
__proto.forEachMaintained = function (fn) {
this.maintained.forEach(function (record) {
return fn(record);
});
};
__proto.forEachMaintainedRight = function (fn) {
var maintained = this.maintained;
maintained.forEach(function (_, i) {
var record = maintained[maintained.length - 1 - i];
fn(record);
});
};
return Result;
}();
/*
egjs-list-differ
Copyright (c) 2019-present NAVER Corp.
MIT license
*/
/**

@@ -310,2 +258,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]>)
* // [[4, 3], [3, 4], [2, 6]]
* console.log(result.pureChanged);
* // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)

@@ -329,3 +280,2 @@ * // [[4, 1], [4, 2], [4, 3]]

var maintained = [];
var changed = [];
var prevKeys = prevList.map(callback);

@@ -335,9 +285,11 @@ var keys = list.map(callback);

var keyMap = new mapClass();
var changedBeforeAdded = [];
var fixed = [];
var removedMap = {};
var orderPriority = [];
var changed = [];
var addedCount = 0;
var removedCount = 0; // Add prevKeys and keys to the hashmap.
prevKeys.forEach(function (key, prevIndex) {
prevKeyMap.set(key, prevIndex);
prevKeys.forEach(function (key, prevListIndex) {
prevKeyMap.set(key, prevListIndex);
});

@@ -348,3 +300,3 @@ keys.forEach(function (key, listIndex) {

prevKeys.forEach(function (key, prevIndex) {
prevKeys.forEach(function (key, prevListIndex) {
var listIndex = keyMap.get(key); // In prevList, but not in list, it is removed.

@@ -354,6 +306,3 @@

++removedCount;
removed.push({
prevItem: prevList[prevIndex],
prevIndex: prevIndex
});
removed.push(prevListIndex);
} else {

@@ -364,30 +313,16 @@ removedMap[listIndex] = removedCount;

keys.forEach(function (key, currentIndex) {
var prevIndex = prevKeyMap.get(key);
var currentItem = list[currentIndex]; // In list, but not in prevList, it is added.
keys.forEach(function (key, listIndex) {
var prevListIndex = prevKeyMap.get(key); // In list, but not in prevList, it is added.
if (typeof prevIndex === "undefined") {
added.push({
currentItem: currentItem,
currentIndex: currentIndex
});
if (typeof prevListIndex === "undefined") {
added.push(listIndex);
++addedCount;
} else {
var prevItem = prevList[prevIndex];
maintained.push({
prevItem: prevItem,
currentItem: currentItem,
prevIndex: prevIndex,
currentIndex: currentIndex
});
removedCount = removedMap[currentIndex] || 0;
orderPriority[prevIndex - removedCount] = currentIndex - addedCount;
maintained.push([prevListIndex, listIndex]);
removedCount = removedMap[listIndex] || 0;
changedBeforeAdded.push([prevListIndex - removedCount, listIndex - addedCount]);
fixed.push(listIndex === prevListIndex);
if (prevIndex !== currentIndex) {
changed.push({
prevItem: prevItem,
currentItem: currentItem,
prevIndex: prevIndex,
currentIndex: currentIndex
});
if (prevListIndex !== listIndex) {
changed.push([prevListIndex, listIndex]);
}

@@ -398,3 +333,3 @@ }

removed.reverse();
return new Result(prevList, list, added, removed, changed, maintained, orderPriority);
return new Result(prevList, list, added, removed, changed, maintained, changedBeforeAdded, fixed);
}

@@ -434,2 +369,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]>)
* // [[4, 3], [3, 4], [2, 6]]
* console.log(result.pureChanged);
* // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)

@@ -436,0 +374,0 @@ * // [[4, 1], [4, 2], [4, 3]]

@@ -7,3 +7,3 @@ /*

repository: https://github.com/naver/egjs-list-differ
version: 1.0.0-snapshot
version: 1.0.1
*/

@@ -21,4 +21,28 @@ (function (global, factory) {

*/
var SUPPORT_MAP = typeof Map === "function";
var PolyMap =
/*#__PURE__*/
function () {
function PolyMap() {
this.keys = [];
this.values = [];
}
var __proto = PolyMap.prototype;
__proto.get = function (key) {
return this.values[this.keys.indexOf(key)];
};
__proto.set = function (key, value) {
var keys = this.keys;
var values = this.values;
var prevIndex = keys.indexOf(key);
var index = prevIndex === -1 ? keys.length : prevIndex;
keys[index] = key;
values[index] = value;
};
return PolyMap;
}();
/*

@@ -54,145 +78,94 @@ egjs-list-differ

*/
var PolyMap =
var SUPPORT_MAP = typeof Map === "function";
/*
egjs-list-differ
Copyright (c) 2019-present NAVER Corp.
MIT license
*/
var Link =
/*#__PURE__*/
function () {
function PolyMap() {
this.keys = [];
this.values = [];
}
function Link() {}
var __proto = PolyMap.prototype;
var __proto = Link.prototype;
__proto.get = function (key) {
return this.values[this.keys.indexOf(key)];
__proto.connect = function (prevLink, nextLink) {
this.prev = prevLink;
this.next = nextLink;
prevLink && (prevLink.next = this);
nextLink && (nextLink.prev = this);
};
__proto.set = function (key, value) {
var keys = this.keys;
var values = this.values;
var prevIndex = keys.indexOf(key);
var index = prevIndex === -1 ? keys.length : prevIndex;
keys[index] = key;
values[index] = value;
__proto.disconnect = function () {
// In double linked list, diconnect the interconnected relationship.
var prevLink = this.prev;
var nextLink = this.next;
prevLink && (prevLink.next = nextLink);
nextLink && (nextLink.prev = prevLink);
};
return PolyMap;
}();
__proto.getIndex = function () {
var link = this;
var index = -1;
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
while (link) {
link = link.prev;
++index;
}
return t;
return index;
};
return __assign.apply(this, arguments);
};
return Link;
}();
/**
* Find a subsequence of a given sequence in which the subsequence's elements are in increasing order,
* and in which the subsequence is as long as possible.
* @returns Longest increasing subsequence of input array
* @example
* LIS([1, 8, 2, 3]) === [1, 2, 3]
* LIS([5, 4, 3, 1, 2]) === [1, 2]
*/
/*
egjs-list-differ
Copyright (c) 2019-present NAVER Corp.
MIT license
*/
function LIS(arr) {
if (arr.length === 0) {
return [];
}
function orderChanged(changed, fixed) {
// It is roughly in the order of these examples.
// 4, 6, 0, 2, 1, 3, 5, 7
var fromLinks = []; // 0, 1, 2, 3, 4, 5, 6, 7
var table = [];
var path = [];
arr.forEach(function (_, i) {
table[i] = 1;
path[i] = -1;
});
arr.forEach(function (_, i) {
for (var j = i - 1; j > -1; j -= 1) {
if (table[j] + 1 > table[i] && arr[j] <= arr[i]) {
table[i] = table[j] + 1;
path[i] = j;
}
}
});
var maxIndex = 0;
table.forEach(function (x, i) {
if (x > table[maxIndex]) {
maxIndex = i;
}
});
var res = [];
var toLinks = [];
changed.forEach(function (_a) {
var from = _a[0],
to = _a[1];
var link = new Link();
fromLinks[from] = link;
toLinks[to] = link;
}); // `fromLinks` are connected to each other by double linked list.
while (maxIndex !== -1) {
res.push(arr[maxIndex]);
maxIndex = path[maxIndex];
}
return res.reverse();
}
function orderChanged(priorityList, maintained) {
var confirmed = {};
var ordered = [];
var length = priorityList.length;
LIS(priorityList).forEach(function (x) {
confirmed[x] = true;
fromLinks.forEach(function (link, i) {
link.connect(fromLinks[i - 1]);
});
return changed.filter(function (_, i) {
return !fixed[i];
}).map(function (_a, i) {
var from = _a[0],
to = _a[1];
for (var from = 0; from < length; from += 1) {
var priority = priorityList[from];
if (confirmed[priority]) {
continue;
if (from === to) {
return [0, 0];
}
var to = 0;
var fromLink = fromLinks[from];
var toLink = toLinks[to - 1];
var fromIndex = fromLink.getIndex(); // Disconnect the link connected to `fromLink`.
while (to < length) {
if (confirmed[priorityList[to]] && priorityList[to] > priority) {
break;
}
fromLink.disconnect(); // Connect `fromLink` to the right of `toLink`.
to += 1;
if (!toLink) {
fromLink.connect(undefined, fromLinks[0]);
} else {
fromLink.connect(toLink, toLink.next);
}
to < from && (to += 1);
confirmed[priority] = true;
var record = maintained[priority];
var anchorRecord = to < length ? maintained[priorityList[to]] : null;
ordered.push(__assign({
beforeOrderIndex: from,
afterOrderIndex: to - 1
}, record, {
anchorIndex: anchorRecord ? anchorRecord.currentIndex : -1,
anchor: anchorRecord ? anchorRecord.currentItem : null
}));
priorityList.splice(from, 1);
priorityList.splice(to - 1, 0, priority);
if (to > from) {
from -= 1;
}
}
return ordered;
var toIndex = fromLink.getIndex();
return [fromIndex, toIndex];
});
}

@@ -203,3 +176,3 @@

function () {
function Result(prevList, list, added, removed, changed, maintained, orderPriority) {
function Result(prevList, list, added, removed, changed, maintained, changedBeforeAdded, fixed) {
this.prevList = prevList;

@@ -211,3 +184,4 @@ this.list = list;

this.maintained = maintained;
this.orderPriority = orderPriority;
this.changedBeforeAdded = changedBeforeAdded;
this.fixed = fixed;
}

@@ -227,62 +201,36 @@

});
Object.defineProperty(__proto, "pureChanged", {
get: function () {
if (!this.cachePureChanged) {
this.caculateOrdered();
}
return this.cachePureChanged;
},
enumerable: true,
configurable: true
});
__proto.caculateOrdered = function () {
var ordered = orderChanged(this.orderPriority.slice(), this.maintained);
this.cacheOrdered = ordered;
};
var ordered = orderChanged(this.changedBeforeAdded, this.fixed);
var changed = this.changed;
var pureChanged = [];
this.cacheOrdered = ordered.filter(function (_a, i) {
var from = _a[0],
to = _a[1];
var _b = changed[i],
fromBefore = _b[0],
toBefore = _b[1];
__proto.forEachAdded = function (fn) {
this.added.forEach(function (record) {
return fn(record);
if (from !== to) {
pureChanged.push([fromBefore, toBefore]);
return true;
}
});
this.cachePureChanged = pureChanged;
};
__proto.forEachAddedRight = function (fn) {
var added = this.added;
added.forEach(function (_, i) {
var record = added[added.length - 1 - i];
fn(record);
});
};
__proto.forEachRemoved = function (fn) {
this.removed.forEach(function (record) {
return fn(record);
});
};
__proto.forEachChanged = function (fn) {
this.changed.forEach(function (record) {
return fn(record);
});
};
__proto.forEachOrdered = function (fn) {
this.ordered.forEach(function (record) {
return fn(record);
});
};
__proto.forEachMaintained = function (fn) {
this.maintained.forEach(function (record) {
return fn(record);
});
};
__proto.forEachMaintainedRight = function (fn) {
var maintained = this.maintained;
maintained.forEach(function (_, i) {
var record = maintained[maintained.length - 1 - i];
fn(record);
});
};
return Result;
}();
/*
egjs-list-differ
Copyright (c) 2019-present NAVER Corp.
MIT license
*/
/**

@@ -316,2 +264,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]>)
* // [[4, 3], [3, 4], [2, 6]]
* console.log(result.pureChanged);
* // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)

@@ -335,3 +286,2 @@ * // [[4, 1], [4, 2], [4, 3]]

var maintained = [];
var changed = [];
var prevKeys = prevList.map(callback);

@@ -341,9 +291,11 @@ var keys = list.map(callback);

var keyMap = new mapClass();
var changedBeforeAdded = [];
var fixed = [];
var removedMap = {};
var orderPriority = [];
var changed = [];
var addedCount = 0;
var removedCount = 0; // Add prevKeys and keys to the hashmap.
prevKeys.forEach(function (key, prevIndex) {
prevKeyMap.set(key, prevIndex);
prevKeys.forEach(function (key, prevListIndex) {
prevKeyMap.set(key, prevListIndex);
});

@@ -354,3 +306,3 @@ keys.forEach(function (key, listIndex) {

prevKeys.forEach(function (key, prevIndex) {
prevKeys.forEach(function (key, prevListIndex) {
var listIndex = keyMap.get(key); // In prevList, but not in list, it is removed.

@@ -360,6 +312,3 @@

++removedCount;
removed.push({
prevItem: prevList[prevIndex],
prevIndex: prevIndex
});
removed.push(prevListIndex);
} else {

@@ -370,30 +319,16 @@ removedMap[listIndex] = removedCount;

keys.forEach(function (key, currentIndex) {
var prevIndex = prevKeyMap.get(key);
var currentItem = list[currentIndex]; // In list, but not in prevList, it is added.
keys.forEach(function (key, listIndex) {
var prevListIndex = prevKeyMap.get(key); // In list, but not in prevList, it is added.
if (typeof prevIndex === "undefined") {
added.push({
currentItem: currentItem,
currentIndex: currentIndex
});
if (typeof prevListIndex === "undefined") {
added.push(listIndex);
++addedCount;
} else {
var prevItem = prevList[prevIndex];
maintained.push({
prevItem: prevItem,
currentItem: currentItem,
prevIndex: prevIndex,
currentIndex: currentIndex
});
removedCount = removedMap[currentIndex] || 0;
orderPriority[prevIndex - removedCount] = currentIndex - addedCount;
maintained.push([prevListIndex, listIndex]);
removedCount = removedMap[listIndex] || 0;
changedBeforeAdded.push([prevListIndex - removedCount, listIndex - addedCount]);
fixed.push(listIndex === prevListIndex);
if (prevIndex !== currentIndex) {
changed.push({
prevItem: prevItem,
currentItem: currentItem,
prevIndex: prevIndex,
currentIndex: currentIndex
});
if (prevListIndex !== listIndex) {
changed.push([prevListIndex, listIndex]);
}

@@ -404,3 +339,3 @@ }

removed.reverse();
return new Result(prevList, list, added, removed, changed, maintained, orderPriority);
return new Result(prevList, list, added, removed, changed, maintained, changedBeforeAdded, fixed);
}

@@ -440,2 +375,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]>)
* // [[4, 3], [3, 4], [2, 6]]
* console.log(result.pureChanged);
* // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)

@@ -442,0 +380,0 @@ * // [[4, 1], [4, 2], [4, 3]]

@@ -7,5 +7,5 @@ /*

repository: https://github.com/naver/egjs-list-differ
version: 1.0.0-snapshot
version: 1.0.1
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):((e=e||self).eg=e.eg||{},e.eg.ListDiffer=t())}(this,function(){"use strict";var g="function"==typeof Map,m=function(){function e(){this.object={}}var t=e.prototype;return t.get=function(e){return this.object[e]},t.set=function(e,t){this.object[e]=t},e}(),y=function(){function e(){this.keys=[],this.values=[]}var t=e.prototype;return t.get=function(e){return this.values[this.keys.indexOf(e)]},t.set=function(e,t){var n=this.keys,r=this.values,i=n.indexOf(e),o=-1===i?n.length:i;n[o]=e,r[o]=t},e}(),h=function(){return(h=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};var I=function(){function e(e,t,n,r,i,o,c){this.prevList=e,this.list=t,this.added=n,this.removed=r,this.changed=i,this.maintained=o,this.orderPriority=c}var t=e.prototype;return Object.defineProperty(t,"ordered",{get:function(){return this.cacheOrdered||this.caculateOrdered(),this.cacheOrdered},enumerable:!0,configurable:!0}),t.caculateOrdered=function(){var e=function(e,t){var n={},r=[],i=e.length;(function(r){if(0===r.length)return[];var i=[],o=[];r.forEach(function(e,t){i[t]=1,o[t]=-1}),r.forEach(function(e,t){for(var n=t-1;-1<n;n-=1)i[n]+1>i[t]&&r[n]<=r[t]&&(i[t]=i[n]+1,o[t]=n)});var n=0;i.forEach(function(e,t){e>i[n]&&(n=t)});for(var e=[];-1!==n;)e.push(r[n]),n=o[n];return e.reverse()})(e).forEach(function(e){n[e]=!0});for(var o=0;o<i;o+=1){var c=e[o];if(!n[c]){for(var a=0;a<i&&!(n[e[a]]&&e[a]>c);)a+=1;a<o&&(a+=1),n[c]=!0;var f=t[c],u=a<i?t[e[a]]:null;r.push(h({beforeOrderIndex:o,afterOrderIndex:a-1},f,{anchorIndex:u?u.currentIndex:-1,anchor:u?u.currentItem:null})),e.splice(o,1),e.splice(a-1,0,c),o<a&&(o-=1)}}return r}(this.orderPriority.slice(),this.maintained);this.cacheOrdered=e},t.forEachAdded=function(t){this.added.forEach(function(e){return t(e)})},t.forEachAddedRight=function(r){var i=this.added;i.forEach(function(e,t){var n=i[i.length-1-t];r(n)})},t.forEachRemoved=function(t){this.removed.forEach(function(e){return t(e)})},t.forEachChanged=function(t){this.changed.forEach(function(e){return t(e)})},t.forEachOrdered=function(t){this.ordered.forEach(function(e){return t(e)})},t.forEachMaintained=function(t){this.maintained.forEach(function(e){return t(e)})},t.forEachMaintainedRight=function(r){var i=this.maintained;i.forEach(function(e,t){var n=i[i.length-1-t];r(n)})},e}();function r(o,c,e){var t=g?Map:e?m:y,n=e||function(e){return e},a=[],r=[],f=[],u=[],i=o.map(n),h=c.map(n),d=new t,s=new t,v={},l=[],p=0,E=0;return i.forEach(function(e,t){d.set(e,t)}),h.forEach(function(e,t){s.set(e,t)}),i.forEach(function(e,t){var n=s.get(e);void 0===n?(++E,r.push({prevItem:o[t],prevIndex:t})):v[n]=E}),h.forEach(function(e,t){var n=d.get(e),r=c[t];if(void 0===n)a.push({currentItem:r,currentIndex:t}),++p;else{var i=o[n];f.push({prevItem:i,currentItem:r,prevIndex:n,currentIndex:t}),E=v[t]||0,l[n-E]=t-p,n!==t&&u.push({prevItem:i,currentItem:r,prevIndex:n,currentIndex:t})}}),r.reverse(),new I(o,c,a,r,u,f,l)}var e=function(){function e(e,t){void 0===e&&(e=[]),this.findKeyCallback=t,this.list=[].slice.call(e)}return e.prototype.update=function(e){var t=[].slice.call(e),n=r(this.list,t,this.findKeyCallback);return this.list=t,n},e}();return e.diff=r,e});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):((e=e||self).eg=e.eg||{},e.eg.ListDiffer=t())}(this,function(){"use strict";var x=function(){function e(){this.keys=[],this.values=[]}var t=e.prototype;return t.get=function(e){return this.values[this.keys.indexOf(e)]},t.set=function(e,t){var n=this.keys,i=this.values,r=n.indexOf(e),c=-1===r?n.length:r;n[c]=e,i[c]=t},e}(),b=function(){function e(){this.object={}}var t=e.prototype;return t.get=function(e){return this.object[e]},t.set=function(e,t){this.object[e]=t},e}(),m="function"==typeof Map,r=function(){function e(){}var t=e.prototype;return t.connect=function(e,t){this.prev=e,this.next=t,e&&(e.next=this),t&&(t.prev=this)},t.disconnect=function(){var e=this.prev,t=this.next;e&&(e.next=t),t&&(t.prev=e)},t.getIndex=function(){for(var e=this,t=-1;e;)e=e.prev,++t;return t},e}();var O=function(){function e(e,t,n,i,r,c,o,u){this.prevList=e,this.list=t,this.added=n,this.removed=i,this.changed=r,this.maintained=c,this.changedBeforeAdded=o,this.fixed=u}var t=e.prototype;return Object.defineProperty(t,"ordered",{get:function(){return this.cacheOrdered||this.caculateOrdered(),this.cacheOrdered},enumerable:!0,configurable:!0}),Object.defineProperty(t,"pureChanged",{get:function(){return this.cachePureChanged||this.caculateOrdered(),this.cachePureChanged},enumerable:!0,configurable:!0}),t.caculateOrdered=function(){var e=function(e,n){var u=[],s=[];return e.forEach(function(e){var t=e[0],n=e[1],i=new r;u[t]=i,s[n]=i}),u.forEach(function(e,t){e.connect(u[t-1])}),e.filter(function(e,t){return!n[t]}).map(function(e,t){var n=e[0],i=e[1];if(n===i)return[0,0];var r=u[n],c=s[i-1],o=r.getIndex();return r.disconnect(),c?r.connect(c,c.next):r.connect(void 0,u[0]),[o,r.getIndex()]})}(this.changedBeforeAdded,this.fixed),u=this.changed,s=[];this.cacheOrdered=e.filter(function(e,t){var n=e[0],i=e[1],r=u[t],c=r[0],o=r[1];if(n!==i)return s.push([c,o]),!0}),this.cachePureChanged=s},e}();function i(e,t,n){var i=m?Map:n?b:x,r=n||function(e){return e},c=[],o=[],u=[],s=e.map(r),f=t.map(r),a=new i,h=new i,d=[],p=[],v={},l=[],g=0,y=0;return s.forEach(function(e,t){a.set(e,t)}),f.forEach(function(e,t){h.set(e,t)}),s.forEach(function(e,t){var n=h.get(e);void 0===n?(++y,o.push(t)):v[n]=y}),f.forEach(function(e,t){var n=a.get(e);void 0===n?(c.push(t),++g):(u.push([n,t]),y=v[t]||0,d.push([n-y,t-g]),p.push(t===n),n!==t&&l.push([n,t]))}),o.reverse(),new O(e,t,c,o,l,u,d,p)}var e=function(){function e(e,t){void 0===e&&(e=[]),this.findKeyCallback=t,this.list=[].slice.call(e)}return e.prototype.update=function(e){var t=[].slice.call(e),n=i(this.list,t,this.findKeyCallback);return this.list=t,n},e}();return e.diff=i,e});
//# sourceMappingURL=list-differ.min.js.map
{
"name": "@egjs/list-differ",
"version": "1.0.1-beta.0",
"version": "1.0.1",
"description": "A module that checks the diff when values are added, removed, or changed in an array.",

@@ -5,0 +5,0 @@ "main": "./dist/list-differ.js",

@@ -43,4 +43,4 @@

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]);
// 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

@@ -50,3 +50,3 @@ // [1, 2, 3, 4, 5, 6, 7]

// Updated list
// [4, 3, 8, 2, 1, 7]
// [4, 3, 6, 2, 1, 7]
console.log(result.list);

@@ -62,4 +62,7 @@ // Index array of values added to `list`

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]>)
// [[0, 3], [0, 2], [0, 1]]
// [[3, 0], [3, 1], [3, 2]]
console.log(result.ordered);

@@ -72,9 +75,11 @@ // An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved

* **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]>)
||**changed**|
|---:|---|
||**changed**|**pureChanged:**|
|---:|---|---|
||[[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)|
|prevList|![prev_list](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_prev_list.png) |![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_before_prev_list.png)|
|process|<p align="center">-</p>|![](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/changed_list.png)|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_before_list.png)|

@@ -88,3 +93,3 @@ ### What is ordered?

|removed<br/>[5, 4]|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/removed.png)|
|ordered|[[0, 3], [0, 2], [0, 1]]|
|ordered|[[3, 0], [3, 1], [3, 2]]|
||![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_added.gif)|

@@ -104,3 +109,3 @@ |ordered[0]<br/>[3 => 0]| ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_ordered0.png)|

const prevList = [1, 2, 3, 4, 5, 6, 7];
const list = [4, 3, 8, 2, 1, 7];
const list = [4, 3, 6, 2, 1, 7];
// const differ = new ListDiffer(prevList, e => e);

@@ -126,4 +131,4 @@ // const result = differ.update(list);

result.ordered.forEach(([from, to], i) => {
const element = nextList.splice(from, 1)[0];
nextList.splice(to, 0, element);
nextList.splice(from, 1);
nextList.splice(to, 0, list[result.pureChanged[i][1]]);
});

@@ -153,4 +158,4 @@ result.added.forEach(index => {

### removed => maintained => added
||maintained => added|
### removed => maintaind => added
||maintaind => added|
|---|---|

@@ -157,0 +162,0 @@ |prevList|![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/prev_list.png)|

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc