Skip to content

Commit 84606e3

Browse files
authored
Replace white spaces for underscore to generate the state key (#144)
* replace space with _ for state name Signed-off-by: Antonio Mendoza Pérez <antmendoza@gmail.com> * replace space with _ for state name Signed-off-by: Antonio Mendoza Pérez <antmendoza@gmail.com>
1 parent 2bf03c1 commit 84606e3

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

src/lib/diagram/mermaidState.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ export class MermaidState {
4848
transitions.push(...this.dataConditionsTransitions());
4949
transitions.push(...this.eventConditionsTransition());
5050
transitions.push(...this.errorTransitions());
51-
transitions.push(...this.naturalTransition(this.stateName(), this.state.transition));
51+
transitions.push(...this.naturalTransition(this.stateKeyDiagram(this.state.name), this.state.transition));
5252
transitions.push(...this.endTransition());
5353

5454
return transitions.reduce((p, c) => {
5555
return p + '\n' + c;
5656
});
5757
}
5858

59-
private stateName() {
60-
return this.state.name?.replace(' ', '_');
59+
private stateKeyDiagram(name: string | undefined) {
60+
return name?.replace(/ /g, '_');
6161
}
6262

6363
private startTransition() {
6464
const transitions: string[] = [];
6565
if (this.isFirstState) {
66-
const stateName = this.stateName();
66+
const stateName = this.stateKeyDiagram(this.state.name);
6767
transitions.push(this.transitionDescription('[*]', stateName));
6868
}
6969
return transitions;
@@ -74,7 +74,7 @@ export class MermaidState {
7474

7575
const dataBasedSwitchState = this.state as Specification.Databasedswitch;
7676
if (dataBasedSwitchState.dataConditions) {
77-
const stateName = this.stateName();
77+
const stateName = this.state.name;
7878
dataBasedSwitchState.dataConditions.forEach((dataCondition) => {
7979
const transitionDataCondition = dataCondition as Specification.Transitiondatacondition;
8080

@@ -98,7 +98,7 @@ export class MermaidState {
9898

9999
const eventBasedSwitchState = this.state as Specification.Eventbasedswitch;
100100
if (eventBasedSwitchState.eventConditions) {
101-
const stateName = this.stateName();
101+
const stateName = this.state.name;
102102
eventBasedSwitchState.eventConditions.forEach((eventCondition) => {
103103
const transitionEventCondition = eventCondition as Specification.Transitioneventcondition;
104104

@@ -121,7 +121,7 @@ export class MermaidState {
121121
const transitions: string[] = [];
122122

123123
if (state.defaultCondition) {
124-
transitions.push(...this.naturalTransition(this.stateName(), state.defaultCondition.transition, 'default'));
124+
transitions.push(...this.naturalTransition(this.state.name, state.defaultCondition.transition, 'default'));
125125
}
126126
return transitions;
127127
}
@@ -130,7 +130,7 @@ export class MermaidState {
130130
const transitions: string[] = [];
131131

132132
if (this.state.end) {
133-
const stateName = this.stateName();
133+
const stateName = this.state.name;
134134
let transitionLabel = undefined;
135135

136136
if (isObject(this.state.end)) {
@@ -147,20 +147,20 @@ export class MermaidState {
147147
}
148148

149149
private naturalTransition(
150-
start?: string,
151-
end?: string | Specification.Transition,
150+
source?: string,
151+
target?: string | Specification.Transition,
152152
label: string | undefined = undefined
153153
) {
154154
const transitions: string[] = [];
155155

156-
if (end) {
156+
if (target) {
157157
let descTransition = '';
158-
if (isObject(end)) {
159-
descTransition = (end as Specification.Transition).nextState;
160-
} else if (typeof end === 'string') {
161-
descTransition = end;
158+
if (isObject(target)) {
159+
descTransition = (target as Specification.Transition).nextState;
160+
} else if (typeof target === 'string') {
161+
descTransition = target;
162162
}
163-
transitions.push(this.transitionDescription(start, descTransition, label ? label : undefined));
163+
transitions.push(this.transitionDescription(source, descTransition, label ? label : undefined));
164164
}
165165
return transitions;
166166
}
@@ -170,7 +170,7 @@ export class MermaidState {
170170

171171
if (this.state.onErrors) {
172172
this.state.onErrors.forEach((error) => {
173-
transitions.push(...this.naturalTransition(this.stateName(), error.transition, error.errorRef));
173+
transitions.push(...this.naturalTransition(this.stateKeyDiagram(this.state.name), error.transition, error.errorRef));
174174
});
175175
}
176176
return transitions;
@@ -208,7 +208,7 @@ export class MermaidState {
208208

209209
private definitionType() {
210210
const type = this.state.type;
211-
return this.stateDescription(this.stateName(), 'type', type!.charAt(0).toUpperCase() + type!.slice(1) + ' State');
211+
return this.stateDescription(this.stateKeyDiagram(this.state.name), 'type', type!.charAt(0).toUpperCase() + type!.slice(1) + ' State');
212212
}
213213

214214
private parallelStateDetails(): string | undefined {
@@ -217,12 +217,12 @@ export class MermaidState {
217217
const descriptions: string[] = [];
218218

219219
if (parallelState.completionType) {
220-
descriptions.push(this.stateDescription(this.stateName(), 'Completion type', parallelState.completionType));
220+
descriptions.push(this.stateDescription(this.stateKeyDiagram(this.state.name), 'Completion type', parallelState.completionType));
221221
}
222222

223223
if (parallelState.branches) {
224224
descriptions.push(
225-
this.stateDescription(this.stateName(), 'Num. of branches', parallelState.branches?.length + '')
225+
this.stateDescription(this.stateKeyDiagram(this.state.name), 'Num. of branches', parallelState.branches?.length + '')
226226
);
227227
}
228228

@@ -234,11 +234,11 @@ export class MermaidState {
234234
}
235235

236236
private eventBasedSwitchStateDetails() {
237-
return this.stateDescription(this.stateName(), `Condition type`, `event-based`);
237+
return this.stateDescription(this.stateKeyDiagram(this.state.name), `Condition type`, `event-based`);
238238
}
239239

240240
private dataBasedSwitchStateDetails() {
241-
return this.stateDescription(this.stateName(), `Condition type`, `data-based`);
241+
return this.stateDescription(this.stateKeyDiagram(this.state.name), `Condition type`, `data-based`);
242242
}
243243

244244
private operationStateDetails() {
@@ -247,11 +247,11 @@ export class MermaidState {
247247
const descriptions: string[] = [];
248248

249249
if (state.actionMode) {
250-
descriptions.push(this.stateDescription(this.stateName(), 'Action mode', state.actionMode));
250+
descriptions.push(this.stateDescription(this.stateKeyDiagram(this.state.name), 'Action mode', state.actionMode));
251251
}
252252

253253
if (state.actions) {
254-
descriptions.push(this.stateDescription(this.stateName(), 'Num. of actions', state.actions?.length + ''));
254+
descriptions.push(this.stateDescription(this.stateKeyDiagram(this.state.name), 'Num. of actions', state.actions?.length + ''));
255255
}
256256

257257
return descriptions.length > 0
@@ -264,7 +264,7 @@ export class MermaidState {
264264
private sleepStateDetails() {
265265
const state = this.state as Specification.Sleepstate;
266266
if (state.duration) {
267-
return this.stateDescription(this.stateName(), 'Duration', state.duration);
267+
return this.stateDescription(this.stateKeyDiagram(this.state.name), 'Duration', state.duration);
268268
}
269269

270270
return undefined;
@@ -276,11 +276,11 @@ export class MermaidState {
276276
const descriptions: string[] = [];
277277

278278
if (state.inputCollection) {
279-
descriptions.push(this.stateDescription(this.stateName(), 'Input collection', state.inputCollection));
279+
descriptions.push(this.stateDescription(this.stateKeyDiagram(this.state.name), 'Input collection', state.inputCollection));
280280
}
281281

282282
if (state.actions) {
283-
descriptions.push(this.stateDescription(this.stateName(), 'Num. of actions', state.actions?.length + ''));
283+
descriptions.push(this.stateDescription(this.stateKeyDiagram(this.state.name), 'Num. of actions', state.actions?.length + ''));
284284
}
285285

286286
return descriptions.length > 0
@@ -303,11 +303,11 @@ export class MermaidState {
303303
} else if (typeof functionRef === 'string') {
304304
functionRefDescription = functionRef as string;
305305
}
306-
descriptions.push(this.stateDescription(this.stateName(), 'Callback function', functionRefDescription));
306+
descriptions.push(this.stateDescription(this.stateKeyDiagram(this.state.name), 'Callback function', functionRefDescription));
307307
}
308308

309309
if (state.eventRef) {
310-
descriptions.push(this.stateDescription(this.stateName(), 'Callback event', state.eventRef));
310+
descriptions.push(this.stateDescription(this.stateKeyDiagram(this.state.name), 'Callback event', state.eventRef));
311311
}
312312

313313
return descriptions.length > 0
@@ -318,15 +318,15 @@ export class MermaidState {
318318
}
319319

320320
private definitionName() {
321-
return this.stateName() + ' : ' + this.stateName();
321+
return this.stateKeyDiagram(this.state.name) + ' : ' + this.state.name;
322322
}
323323

324324
private transitionDescription(
325-
start: string | undefined,
326-
end: string | undefined,
325+
source: string | undefined,
326+
target: string | undefined,
327327
label: string | undefined = undefined
328328
) {
329-
return start + ' --> ' + end + (label ? ' : ' + label : '');
329+
return this.stateKeyDiagram(source) + ' --> ' + this.stateKeyDiagram(target) + (label ? ' : ' + label : '');
330330
}
331331

332332
private stateDescription(stateName: string | undefined, description: string, value: string) {

tests/lib/diagram/mermaidState.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ CheckCredit --> EvaluateDecision`);
252252

253253
it('should create source code for any state with transition as object', () => {
254254
const states = new Specification.Callbackstate(
255-
JSON.parse(`{
255+
JSON.parse(`{
256256
"name": "CheckCredit",
257257
"type": "callback",
258258
"transition": {"nextState": "EvaluateDecision"}
@@ -263,4 +263,34 @@ CheckCredit --> EvaluateDecision`);
263263
CheckCredit : type = Callback State
264264
CheckCredit --> EvaluateDecision`);
265265
});
266+
267+
268+
it(`should convert white spaces with underscore to create the state key`, () => {
269+
const databasedswitch = new Specification.Databasedswitch(
270+
JSON.parse(`{
271+
"type":"switch",
272+
"name":"Check Application",
273+
"dataConditions": [
274+
{
275+
"condition": "\${ .applicants | .age >= 18 }",
276+
"transition": "Start Application"
277+
},
278+
{
279+
"condition": "\${ .applicants | .age < 18 }",
280+
"end": true
281+
}
282+
],
283+
"defaultCondition": {
284+
"transition": "Start Application"
285+
}
286+
}`)
287+
);
288+
const mermaidState = new MermaidState(databasedswitch);
289+
expect(mermaidState.sourceCode()).toBe(`Check_Application : Check Application
290+
Check_Application : type = Switch State
291+
Check_Application : Condition type = data-based
292+
Check_Application --> Start_Application : \${ .applicants | .age >= 18 }
293+
Check_Application --> [*] : \${ .applicants | .age < 18 }
294+
Check_Application --> Start_Application : default`);
295+
});
266296
});

0 commit comments

Comments
 (0)