Skip to content

Instantly share code, notes, and snippets.

@mystroken
Last active February 14, 2022 12:29
Show Gist options
  • Save mystroken/4743775b0f7a8f3c4f86402f5340face to your computer and use it in GitHub Desktop.
Save mystroken/4743775b0f7a8f3c4f86402f5340face to your computer and use it in GitHub Desktop.
/**
* Returns the list of distinctive
* elements based on current relations.
* @param items
* @param relations
* @returns string[]
*/
function getDistinctiveItemsFromRelations(
items: Record<string, { type: string }>,
relations: Record<string, string[]>
): string[] {
// We use Set data structure so
// we make sure only unique elements will be added.
const distinctiveItems = new Set<string>()
for (const itemKey in relations) {
if (relations.hasOwnProperty(itemKey) && items.hasOwnProperty(itemKey)) {
distinctiveItems.add(itemKey)
const relatedItemsKeys = relations[itemKey]
if (relatedItemsKeys.isArray()) {
relatedItemsKeys
.filter((relatedItemKey) => items.hasOwnProperty(relatedItemKey))
.forEach((relatedItemKey) => distinctiveItems.add(relatedItemKey))
}
}
}
return [...distinctiveItems]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment