Projekt

Obecné

Profil

Stáhnout (1.44 KB) Statistiky
| Větev: | Tag: | Revize:
1 812b9f90 Vaclav Honzik
import { CatalogItemDto, PathDto } from '../../../swagger/data-contracts'
2
import generateUuid from '../../../utils/id/uuidGenerator'
3 c0b66eaf Vaclav Honzik
import { MapPoint, MapPointType, PathVariant } from '../trackingToolUtils'
4 91fd3fa6 Vaclav Honzik
5 a7ae217f Vaclav Honzik
6 de12c6be Vaclav Honzik
/**
7
 * Cartesian product of two arrays
8
 * @param sets
9
 * @returns
10
 */
11 91fd3fa6 Vaclav Honzik
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] =>
12
    sets.reduce<CatalogItemDto[][]>(
13
        (results, ids) =>
14
            results
15
                .map((result) => ids.map((id) => [...result, id]))
16
                .reduce((nested, result) => [...nested, ...result]),
17
        [[]]
18
    )
19
20 de12c6be Vaclav Honzik
/**
21
 * Builds a list of all possible path variants from pathDto
22
 * @param pathDto
23
 * @returns
24
 */
25 812b9f90 Vaclav Honzik
export const buildPathVariants = (pathDto: PathDto, mapPointType: MapPointType = MapPointType.LocalCatalog): PathVariant[] => {
26 91fd3fa6 Vaclav Honzik
    if (!pathDto.foundCatalogItems) {
27
        return []
28
    }
29
30 de12c6be Vaclav Honzik
    return (
31
        pathDto.foundCatalogItems.length === 1
32
            ? pathDto.foundCatalogItems
33
            : cartesianProduct(pathDto.foundCatalogItems)
34
    ).map((variant, _) =>
35
        variant.map(
36 a7ae217f Vaclav Honzik
            (catalogItem, idx) => (
37
                {
38 14588cb6 Vaclav Honzik
                    id: '',
39 de12c6be Vaclav Honzik
                    idx,
40 14588cb6 Vaclav Honzik
                    reactId: generateUuid(),
41 f41a4cd3 Vaclav Honzik
                    addToPath: !!catalogItem.latitude && !!catalogItem.longitude,
42 812b9f90 Vaclav Honzik
                    catalogItem,
43
                    type: mapPointType,
44 f41a4cd3 Vaclav Honzik
                } as MapPoint)
45 de12c6be Vaclav Honzik
        )
46
    )
47 91fd3fa6 Vaclav Honzik
}
48
49
export default buildPathVariants