-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmoveDuplicatesToComponents.ts
More file actions
86 lines (79 loc) · 2.84 KB
/
moveDuplicatesToComponents.ts
File metadata and controls
86 lines (79 loc) · 2.84 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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:moveDuplicatesToComponents')
/**
*
* @param optimizableComponentGroup all AsyncAPI Specification-valid components that you want to analyze for duplicates.
* @returns A list of optimization report elements.
*/
const buildTarget = (groupType: string, componentName: string): string => {
const componentNameSegment = toLodashPathSegment(componentName)
return `components.${groupType}${componentNameSegment.startsWith('[') ? '' : '.'}${componentNameSegment}`
}
const handleDuplicate = (
resultElements: ReportElement[],
groupType: string,
component: OptimizableComponent,
compareComponent: OptimizableComponent
): void => {
const existingResult = resultElements.find((reportElement) => component.path === reportElement.path)
if (!existingResult) {
const target = buildTarget(groupType, getComponentName(component))
resultElements.push({
path: component.path,
action: Action.Move,
target,
})
resultElements.push({
path: compareComponent.path,
action: Action.Reuse,
target,
})
return
}
resultElements.push({
path: component.path,
action: Action.Reuse,
target: existingResult.target,
})
}
const findDuplicateComponents = (
optimizableComponentGroup: OptimizableComponentGroup
): ReportElement[] => {
const allComponents = optimizableComponentGroup.components
const insideComponentsSection = allComponents.filter(isInComponents)
const outsideComponentsSection = getOutsideComponents(allComponents, insideComponentsSection)
const resultElements: ReportElement[] = []
for (const [index, component] of outsideComponentsSection.entries()) {
for (const compareComponent of outsideComponentsSection.slice(index + 1)) {
if (!isEqual(component.component, compareComponent.component, false)) continue
handleDuplicate(resultElements, optimizableComponentGroup.type, component, compareComponent)
}
}
debug(
'duplicte %s: %O',
optimizableComponentGroup.type,
resultElements.map((element) => element.path)
)
return resultElements
}
export const moveDuplicatesToComponents: Reporter = (optimizableComponentsGroup) => {
return createReport(
findDuplicateComponents,
optimizableComponentsGroup,
'moveDuplicatesToComponents'
)
}
function getOutsideComponents(
allComponents: OptimizableComponent[],
insideComponentsSection: OptimizableComponent[]
) {
return allComponents.filter(
(component) =>
!isInComponents(component) &&
insideComponentsSection.filter((inCSC) => isEqual(component.component, inCSC.component, true))
.length === 0
)
}