/
slicingProjections.ts
98 lines (89 loc) · 2.67 KB
/
slicingProjections.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Edge } from "../../common/extraction/graph"
import { safeArrayGet } from "../../common/util/arrayUtils"
import { MapFunction, MappedEdge } from "../../common/projection/projectEdges"
export function identity(): MapFunction {
return function id(edge) {
return {
sourceLabel: edge.source,
targetLabel: edge.target
}
}
}
export function sliceByPattern(pattern: string): MapFunction {
const index = pattern.indexOf("(**)")
if (index === -1) {
throw new Error(
"Could not find '(**)' inside slice pattern. It should contain exactly one occurance'(**)'"
)
}
const prefix = escapeForRegexp(pattern.substr(0, index))
const unescapedSuffix = pattern.substr(index + 4, pattern.length)
if (unescapedSuffix.indexOf("(**)") !== -1) {
throw new Error(
"Found too many '(**)' inside slice pattern. It should contain exactly one occurance'(**)'"
)
}
const suffix = escapeForRegexp(unescapedSuffix)
const regexp = `^${prefix}([\\w]+)${suffix}.*$`
return sliceByRegex(new RegExp(regexp))
}
function escapeForRegexp(input: string): string {
return input.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")
}
export function sliceByRegex(regexp: RegExp): MapFunction {
return function slice(edge: Edge): MappedEdge | undefined {
let source = edge.source
let target = edge.target
if (!edge.external) {
const strippedSource = stripSlice(source, regexp)
if (strippedSource === undefined) {
return undefined
}
const strippedTarget = stripSlice(target, regexp)
if (strippedTarget === undefined) {
return undefined
}
if (strippedSource === strippedTarget) {
return undefined
}
return {
sourceLabel: strippedSource,
targetLabel: strippedTarget
}
} else {
return undefined
}
}
}
function stripSlice(relativeFileName: string, stripRegexp: RegExp): string | undefined {
const strippedFileName = stripRegexp.exec(relativeFileName)
if (strippedFileName === null) {
return undefined
}
const match = safeArrayGet(strippedFileName, 1)
if (match === undefined) {
return undefined
}
return strippedFileName[1] ?? undefined
}
export function sliceByFileSuffix(labeling: Map<string, string>): MapFunction {
return function slice(edge: Edge): MappedEdge | undefined {
let sourceMatch: string | undefined = undefined
let targetMatch: string | undefined = undefined
for (const [suffix, label] of labeling.entries()) {
if (edge.source.endsWith(`${suffix}.ts`)) {
sourceMatch = label
}
if (edge.target.endsWith(`${suffix}.ts`)) {
targetMatch = label
}
if (sourceMatch !== undefined && targetMatch !== undefined) {
return {
sourceLabel: sourceMatch,
targetLabel: targetMatch
}
}
}
return undefined
}
}