/
resultFormatter.ts
143 lines (128 loc) · 4.25 KB
/
resultFormatter.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { InDTMI } from "./internal";
import { logger } from "../utils/logger";
/* eslint-disable no-unused-vars */
enum Expansion {
Nominative = ":n",
Possessive = ":p",
Relative = ":r",
Elective = ":e"
}
/* eslint-enable no-unused-vars */
type ExpansionType = keyof typeof Expansion; // This yields the set of strings that match the enum instances.
export class ResultFormatter {
private _formatString: string;
private _formattedResult: string;
constructor(formatString: string) {
this._formatString = formatString;
this._formattedResult = formatString;
}
public install(key: string, value: string) {
const undecoratedKey = "{" + key + "}";
if (this._formatString.includes(undecoratedKey)) {
this._formattedResult = this._formattedResult.replace(undecoratedKey, value);
return;
}
for (const expansionTypeKey in Expansion) {
// eslint-disable-line guard-for-in
const expansionType = expansionTypeKey as ExpansionType;
const decoratedKey = "{" + key + Expansion[expansionType] + "}";
if (this._formatString.includes(decoratedKey)) {
this._formattedResult = this._formattedResult.replace(
decoratedKey,
this._expandValue(value, expansionType)
);
return;
}
}
}
public toString(): string {
return this._formattedResult;
}
private _expandValue(value: string, expansionType: ExpansionType): string {
if (typeof value !== "string") {
logger.info("VALUE NOT STRING");
}
if (value.startsWith("_:")) {
return this._getBasicExpansion(value, expansionType, "");
}
if (!value.startsWith("dtmi:")) {
return this._getBasicExpansion(value, expansionType, ` '${value}'`);
}
const dtmi = InDTMI.createDtmi(value);
if (dtmi === undefined) {
return this._getBasicExpansion(value, expansionType, ` '${value}'`);
}
if (!dtmi.labels[dtmi.labels.length - 1].startsWith("_")) {
return this._getBasicExpansion(value, expansionType, " " + value);
}
switch (expansionType) {
case "Nominative":
return this._buildNominativePhrase(dtmi, dtmi.labels.length);
case "Possessive":
if (dtmi.labels[dtmi.labels.length - 1].startsWith("__")) {
return (
this._buildNominativePhrase(dtmi, dtmi.labels.length - 2) +
` has '${dtmi.labels[dtmi.labels.length - 2].substr(1)}' value with name '${dtmi.labels[
dtmi.labels.length - 1
].substr(2)}' whose`
);
} else {
return (
this._buildNominativePhrase(dtmi, dtmi.labels.length - 1) +
` has '${dtmi.labels[dtmi.labels.length - 1].substr(1)}' value whose`
);
}
case "Relative":
if (dtmi.labels[dtmi.labels.length - 1].startsWith("__")) {
return ` with name '${dtmi.labels[dtmi.labels.length - 1].substr(2)}'`;
} else {
return "";
}
case "Elective":
return "";
}
}
private _getBasicExpansion(
value: string,
expansionType: ExpansionType,
defaultValue: string
): string {
switch (expansionType) {
case "Nominative":
return value;
case "Possessive":
return value + `'s`;
default:
return defaultValue;
}
}
private _buildNominativePhrase(dtmi: InDTMI, labelCount: number): string {
if (dtmi.labels[labelCount - 1].startsWith("__")) {
return (
this._buildNominativePhrase(dtmi, labelCount - 2) +
` has '${dtmi.labels[labelCount - 2].substr(1)}' value with name '${dtmi.labels[
labelCount - 1
].substr(2)}' which`
);
} else if (dtmi.labels[labelCount - 1].startsWith("_")) {
return (
this._buildNominativePhrase(dtmi, labelCount - 1) +
` has '${dtmi.labels[labelCount - 1].substr(1)}' value which`
);
} else {
let ancestorId = "dtmi";
for (let i = 0; i < labelCount; ++i) {
ancestorId += ":" + dtmi.labels[i];
}
if (dtmi.majorVersion > 0) {
ancestorId += ";" + dtmi.majorVersion;
if (dtmi.minorVersion > 0) {
ancestorId += ";" + dtmi.minorVersion;
}
}
return ancestorId;
}
}
}