Skip to content

Instantly share code, notes, and snippets.

@LuizMoratelli
Last active October 17, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuizMoratelli/f715ae80c6b56e49803fa4fce43c5729 to your computer and use it in GitHub Desktop.
Save LuizMoratelli/f715ae80c6b56e49803fa4fce43c5729 to your computer and use it in GitHub Desktop.
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