Last active
February 14, 2022 12:29
-
-
Save mystroken/4743775b0f7a8f3c4f86402f5340face to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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