Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import differenceWith from 'lodash/differenceWith';
2import isEqual from 'lodash/isEqual';
3
4type Changeset<T> = {
5 added: T[];
6 removed: T[];
7};
8
9/**
10 * Calculates the changeset of oldItems and newItems by comparing the values by
11 * value, not by reference
12 */
13export function getChangeset<T>(oldItems: T[], newItems: T[]): Changeset<T> {
14 const added = differenceWith(newItems, oldItems, isEqual);
15 const removed = differenceWith(oldItems, newItems, isEqual);
16 return { added, removed };
17}
18
19export function filterNullOrUndefined<T>(
20 array: readonly (T | null | undefined)[],
21) {
22 return array.filter((it) => it !== null && it !== undefined) as T[];
23}
24
25export function arrayFromArrayOrSingleItem<T>(array: readonly T[] | T): T[] {
26 return Array.isArray(array) ? [...array] : [array];
27}