parent
21490b724a
commit
3ed442ec0f
@ -1,10 +1,19 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
function hasDifferentItems(prevItems, currentItems, idProp = 'id') {
|
||||
const diff1 = _.differenceBy(prevItems, currentItems, (item) => item[idProp]);
|
||||
const diff2 = _.differenceBy(currentItems, prevItems, (item) => item[idProp]);
|
||||
if (prevItems === currentItems) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (prevItems.length !== currentItems.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const currentItemIds = new Set();
|
||||
|
||||
currentItems.forEach((currentItem) => {
|
||||
currentItemIds.add(currentItem[idProp]);
|
||||
});
|
||||
|
||||
return diff1.length > 0 || diff2.length > 0;
|
||||
return prevItems.every((prevItem) => currentItemIds.has(prevItem[idProp]));
|
||||
}
|
||||
|
||||
export default hasDifferentItems;
|
||||
|
@ -0,0 +1,21 @@
|
||||
function hasDifferentItemsOrOrder(prevItems, currentItems, idProp = 'id') {
|
||||
if (prevItems === currentItems) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const len = prevItems.length;
|
||||
|
||||
if (len !== currentItems.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (let i = 0; i < len; i++) {
|
||||
if (prevItems[i][idProp] !== currentItems[i][idProp]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export default hasDifferentItemsOrOrder;
|
Loading…
Reference in new issue