forked from spring-io/antora-ui-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
related_projects.js
28 lines (24 loc) · 945 Bytes
/
related_projects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'use strict'
module.exports = (categories, projectIds, projects) => relatedProjects(categories, projectIds, projects)
function relatedProjects (categories, projectIds, projects) {
categories = categories || []
projectIds = projectIds || []
if (categories.length === 0 && projectIds.length === 0) {
return projects
}
const result = projects.filter(
(project) => filterProjectByCategories(project, categories) || filterProjectByProjectIds(project, projectIds)
)
for (const project of result) {
if (project.children) {
project.children = relatedProjects(categories, projectIds, project.children)
}
}
return result
}
function filterProjectByCategories (project, categories) {
return project.categories && categories.some((category) => project.categories.includes(category))
}
function filterProjectByProjectIds (project, projectIds) {
return projectIds.some((projectId) => project.id === projectId)
}