Projekt

Obecné

Profil

Stáhnout (1.51 KB) 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 = MapPoint[]
7

    
8
export interface MapPoint {
9
    idx: number,
10
    active: boolean,
11
    catalogItem: CatalogItemDto,
12
}
13

    
14
export const isMapPointDisplayable = (mapPoint: MapPoint): boolean =>
15
    !!mapPoint.catalogItem.latitude && !!mapPoint.catalogItem.longitude
16

    
17
/**
18
 * Cartesian product of two arrays
19
 * @param sets
20
 * @returns
21
 */
22
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] =>
23
    sets.reduce<CatalogItemDto[][]>(
24
        (results, ids) =>
25
            results
26
                .map((result) => ids.map((id) => [...result, id]))
27
                .reduce((nested, result) => [...nested, ...result]),
28
        [[]]
29
    )
30

    
31
/**
32
 * Builds a list of all possible path variants from pathDto
33
 * @param pathDto
34
 * @returns
35
 */
36
export const buildPathVariants = (pathDto: PathDto): PathVariant[] => {
37
    if (!pathDto.foundCatalogItems) {
38
        return []
39
    }
40

    
41
    return (
42
        pathDto.foundCatalogItems.length === 1
43
            ? pathDto.foundCatalogItems
44
            : cartesianProduct(pathDto.foundCatalogItems)
45
    ).map((variant, _) =>
46
        variant.map(
47
            (catalogItem, idx) => (
48
                {
49
                    idx,
50
                    active: !!catalogItem.latitude && !!catalogItem.longitude,
51
                    catalogItem
52
                })
53
        )
54
    )
55
}
56

    
57
export default buildPathVariants
(6-6/8)