-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzeplinComponentUtil.ts
executable file
·74 lines (66 loc) · 2.8 KB
/
zeplinComponentUtil.ts
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import ZeplinComponentSection from "../../../common/domain/zeplinComponent/model/ZeplinComponentSection";
import ZeplinComponent from "../../../common/domain/zeplinComponent/model/ZeplinComponent";
import BarrelDetailsResponse from "../../../common/domain/barrel/model/BarrelDetailsResponse";
import BarrelType from "../../../common/domain/barrel/BarrelType";
function getComponentsFromSections(barrel: BarrelDetailsResponse, barrelType: BarrelType): ZeplinComponent[] {
return getComponentsFromSectionsAdditive(
barrel,
barrelType,
barrel.componentSections.map(section => ({
section,
nameBreadcrumbs: [],
idBreadcrumbs: []
} as SectionWithBreadcrumbs)),
true, // Default section is always the first section
[]
);
}
function getComponentsFromSectionsAdditive( // eslint-disable-line max-params
barrel: BarrelDetailsResponse,
barrelType: BarrelType,
sectionWithBreadcrumbsArray: SectionWithBreadcrumbs[],
defaultSection: boolean,
accumulator: ZeplinComponent[]
): ZeplinComponent[] {
if (sectionWithBreadcrumbsArray && sectionWithBreadcrumbsArray.length) {
const { _id: barrelId, name: barrelName } = barrel;
const { section, nameBreadcrumbs, idBreadcrumbs } = sectionWithBreadcrumbsArray.shift()!;
accumulator.push(
...section.components.map(
component => ({
_id: component._id,
description: component.description,
name: component.name,
latestVersion: component.latestVersion,
barrelId,
barrelType,
barrelName,
sectionIds: defaultSection ? idBreadcrumbs : idBreadcrumbs.concat(section._id),
sectionNames: defaultSection ? nameBreadcrumbs : nameBreadcrumbs.concat(section.name)
// Default section components are regarded as sectionless
} as ZeplinComponent)
)
);
if (section.componentSections) {
sectionWithBreadcrumbsArray.unshift(
...section.componentSections.map(
subsection => ({
section: subsection,
idBreadcrumbs: idBreadcrumbs.concat(section._id),
nameBreadcrumbs: nameBreadcrumbs.concat(section.name)
} as SectionWithBreadcrumbs)
)
);
}
return getComponentsFromSectionsAdditive(barrel, barrelType, sectionWithBreadcrumbsArray, false, accumulator);
}
return accumulator;
}
interface SectionWithBreadcrumbs {
section: ZeplinComponentSection;
nameBreadcrumbs: string[];
idBreadcrumbs: string[];
}
export {
getComponentsFromSections
};