Skip to content

Instantly share code, notes, and snippets.

@LuizMoratelli

LuizMoratelli/subset.ts

Last active Oct 17, 2020
Embed
What would you like to do?
export const isSubset = <T extends { id: string }>(array: T[], subset: T[]): boolean => {
const ids: string[] = array.map((item: T) => item.id);
const notInArray = subset.filter((item: T) => !ids.includes(item.id))
return notInArray.length <= 0;
}
// @fakenickles
const isSubsetBy = (source, target, comparator) => target.every(
element => Boolean(source.find(comparator(element)))
);
isSubsetBy(
[{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}],
[{id: 2}, {id: 3}],
(source) => (target) => source.id === target.id
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment