Skip to content

Instantly share code, notes, and snippets.

@joaqcid
Last active July 26, 2019 03:17
Show Gist options
  • Save joaqcid/a3a2c01a2ed4a7ad11b6632768fb1233 to your computer and use it in GitHub Desktop.
Save joaqcid/a3a2c01a2ed4a7ad11b6632768fb1233 to your computer and use it in GitHub Desktop.
appendUniq
export declare type Selector<T = any> = (value?: T) => any;
export function appendUniq<T, B>(items: T[], compareBy: Selector<T>) {
return (existing: Readonly<T[]>): T[] => {
// If `items` is `undefined` or `null` or `[]` but `existing` is provided
// just return `existing`
const itemsNotProvidedButExistingIs = (!items || !items.length) && existing;
if (itemsNotProvidedButExistingIs) {
return existing as T[];
}
if (Array.isArray(existing)) {
// extract field to compare by
const existingIds = existing.map(compareBy)
// filter out dupes
const uniq = items.filter(item => !existingIds.includes(compareBy(item)));
// if no uniq items, return existing
if (uniq.length === 0)
return existing as T[];
else
return existing.concat(uniq);
}
// For example if some property is added dynamically
// and didn't exist before thus it's not `ArrayLike`
return items;
};
}
import { appendUniq } from '@shared/modules/ngxs/helpers';
import { patch } from '@ngxs/store/operators';
describe('appendUniq', () => {
describe('when items provided ', () => {
it('returns without dupes', () => {
// Arrange
const original = { items: [{ id: '1' }, { id: '2' }, { id: '3' }] };
// Act
const newValue = patch({
items: appendUniq([{ id: '2' }, { id: '3' }, { id: '4' }], x => x.id)
})(original);
// Assert
expect(newValue.items.length).toEqual(4)
expect(newValue.items).toEqual(jasmine.arrayContaining([{ id: '1' }, { id: '2' }, { id: '3' }, { id: '4' }]))
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment