-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmoveAllToComponents.ts
More file actions
59 lines (55 loc) · 2.24 KB
/
moveAllToComponents.ts
File metadata and controls
59 lines (55 loc) · 2.24 KB
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
import { Action } from '../Optimizer'
import { createReport, isEqual, isInComponents, getComponentName, toLodashPathSegment } from '../Utils'
import { OptimizableComponent, OptimizableComponentGroup, ReportElement, Reporter } from 'types'
import Debug from 'debug'
const debug = Debug('reporter:moveAllToComponents')
/**
*
* @param optimizableComponentGroup all AsyncAPI Specification-valid components.
* @returns A list of optimization report elements.
*/
const findAllComponents = (
optimizableComponentGroup: OptimizableComponentGroup
): ReportElement[] => {
const allComponents = optimizableComponentGroup.components
const insideComponentsSection = allComponents.filter(isInComponents)
const outsideComponentsSection = getOutsideComponents(allComponents, insideComponentsSection)
const resultElements: ReportElement[] = []
for (const component of outsideComponentsSection.values()) {
const existingResult = resultElements.filter(
(reportElement) => component.path === reportElement.path
)[0]
if (!existingResult) {
const componentName = getComponentName(component)
// Use bracket notation if the component name contains a dot to prevent lodash from
// interpreting it as nested properties (e.g., "user.fifo" should not become user: { fifo: ... })
const componentNameSegment = toLodashPathSegment(componentName)
const target = `components.${optimizableComponentGroup.type}${componentNameSegment.startsWith('[') ? '' : '.'}${componentNameSegment}`
resultElements.push({
path: component.path,
action: Action.Move,
target,
})
}
}
debug(
'all %s: %O',
optimizableComponentGroup.type,
resultElements.map((element) => element.path)
)
return resultElements
}
export const moveAllToComponents: Reporter = (optimizableComponentsGroup) => {
return createReport(findAllComponents, optimizableComponentsGroup, 'moveAllToComponents')
}
function getOutsideComponents(
allComponents: OptimizableComponent[],
insideComponentsSection: OptimizableComponent[]
) {
return allComponents.filter(
(component) =>
!isInComponents(component) &&
insideComponentsSection.filter((inCSC) => isEqual(component.component, inCSC.component, true))
.length === 0
)
}