Skip to content

Commit abcf2a2

Browse files
authored
Merge pull request mermaid-js#4805 from mermaid-js/sidv/FixTilde
fix: Add support for `~test Array~string~` back in Class
2 parents ebaabbf + 4944694 commit abcf2a2

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

packages/mermaid/src/diagrams/common/common.spec.ts

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sanitizeText, removeScript, parseGenericTypes } from './common.js';
1+
import { sanitizeText, removeScript, parseGenericTypes, countOccurrence } from './common.js';
22

33
describe('when securityLevel is antiscript, all script must be removed', () => {
44
/**
@@ -59,15 +59,29 @@ describe('Sanitize text', () => {
5959
});
6060

6161
describe('generic parser', () => {
62-
it('should parse generic types', () => {
63-
expect(parseGenericTypes('test~T~')).toEqual('test<T>');
64-
expect(parseGenericTypes('test~Array~Array~string~~~')).toEqual('test<Array<Array<string>>>');
65-
expect(parseGenericTypes('test~Array~Array~string[]~~~')).toEqual(
66-
'test<Array<Array<string[]>>>'
67-
);
68-
expect(parseGenericTypes('test ~Array~Array~string[]~~~')).toEqual(
69-
'test <Array<Array<string[]>>>'
70-
);
71-
expect(parseGenericTypes('~test')).toEqual('~test');
62+
it.each([
63+
['test~T~', 'test<T>'],
64+
['test~Array~Array~string~~~', 'test<Array<Array<string>>>'],
65+
['test~Array~Array~string[]~~~', 'test<Array<Array<string[]>>>'],
66+
['test ~Array~Array~string[]~~~', 'test <Array<Array<string[]>>>'],
67+
['~test', '~test'],
68+
['~test~T~', '~test<T>'],
69+
])('should parse generic types: %s to %s', (input: string, expected: string) => {
70+
expect(parseGenericTypes(input)).toEqual(expected);
7271
});
7372
});
73+
74+
it.each([
75+
['', '', 0],
76+
['', 'x', 0],
77+
['test', 'x', 0],
78+
['test', 't', 2],
79+
['test', 'te', 1],
80+
['test~T~', '~', 2],
81+
['test~Array~Array~string~~~', '~', 6],
82+
])(
83+
'should count `%s` to contain occurrences of `%s` to be `%i`',
84+
(str: string, substring: string, count: number) => {
85+
expect(countOccurrence(str, substring)).toEqual(count);
86+
}
87+
);

packages/mermaid/src/diagrams/common/common.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,33 @@ export const parseGenericTypes = function (input: string): string {
208208
return output.join('');
209209
};
210210

211+
export const countOccurrence = (string: string, substring: string): number => {
212+
return Math.max(0, string.split(substring).length - 1);
213+
};
214+
211215
const shouldCombineSets = (previousSet: string, nextSet: string): boolean => {
212-
const prevCount = [...previousSet].reduce((count, char) => (char === '~' ? count + 1 : count), 0);
213-
const nextCount = [...nextSet].reduce((count, char) => (char === '~' ? count + 1 : count), 0);
216+
const prevCount = countOccurrence(previousSet, '~');
217+
const nextCount = countOccurrence(nextSet, '~');
214218

215219
return prevCount === 1 && nextCount === 1;
216220
};
217221

218222
const processSet = (input: string): string => {
219-
const chars = [...input];
220-
const tildeCount = chars.reduce((count, char) => (char === '~' ? count + 1 : count), 0);
223+
const tildeCount = countOccurrence(input, '~');
224+
let hasStartingTilde = false;
221225

222226
if (tildeCount <= 1) {
223227
return input;
224228
}
225229

230+
// If there is an odd number of tildes, and the input starts with a tilde, we need to remove it and add it back in later
231+
if (tildeCount % 2 !== 0 && input.startsWith('~')) {
232+
input = input.substring(1);
233+
hasStartingTilde = true;
234+
}
235+
236+
const chars = [...input];
237+
226238
let first = chars.indexOf('~');
227239
let last = chars.lastIndexOf('~');
228240

@@ -234,6 +246,11 @@ const processSet = (input: string): string => {
234246
last = chars.lastIndexOf('~');
235247
}
236248

249+
// Add the starting tilde back in if we removed it
250+
if (hasStartingTilde) {
251+
chars.unshift('~');
252+
}
253+
237254
return chars.join('');
238255
};
239256

0 commit comments

Comments
 (0)