Projekt

Obecné

Profil

Stáhnout (873 Bajtů) Statistiky
| Větev: | Tag: | Revize:
1
// Business logic for tracking tool
2

    
3
import { CatalogItemDto, PathDto } from '../../swagger/data-contracts'
4

    
5
// For more comprehensive code alias CatalogItemDto[] as path variant
6
export type PathVariant = CatalogItemDto[]
7

    
8
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] =>
9
    sets.reduce<CatalogItemDto[][]>(
10
        (results, ids) =>
11
            results
12
                .map((result) => ids.map((id) => [...result, id]))
13
                .reduce((nested, result) => [...nested, ...result]),
14
        [[]]
15
    )
16

    
17
export const buildPathVariants = (pathDto: PathDto): PathVariant[] => {
18
    if (!pathDto.foundCatalogItems) {
19
        return []
20
    }
21

    
22
    const catalogItems = pathDto.foundCatalogItems
23
    if (catalogItems.length === 1) {
24
        return catalogItems
25
    }
26

    
27
    return cartesianProduct(catalogItems)
28
}
29

    
30
export default buildPathVariants
(6-6/8)