Skip to content

Commit

Permalink
Merge pull request #4805 from mermaid-js/sidv/FixTilde
Browse files Browse the repository at this point in the history
fix: Add support for `~test Array~string~` back in Class
  • Loading branch information
knsv authored Sep 6, 2023
2 parents ebaabbf + 4944694 commit abcf2a2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
36 changes: 25 additions & 11 deletions packages/mermaid/src/diagrams/common/common.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sanitizeText, removeScript, parseGenericTypes } from './common.js';
import { sanitizeText, removeScript, parseGenericTypes, countOccurrence } from './common.js';

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

describe('generic parser', () => {
it('should parse generic types', () => {
expect(parseGenericTypes('test~T~')).toEqual('test<T>');
expect(parseGenericTypes('test~Array~Array~string~~~')).toEqual('test<Array<Array<string>>>');
expect(parseGenericTypes('test~Array~Array~string[]~~~')).toEqual(
'test<Array<Array<string[]>>>'
);
expect(parseGenericTypes('test ~Array~Array~string[]~~~')).toEqual(
'test <Array<Array<string[]>>>'
);
expect(parseGenericTypes('~test')).toEqual('~test');
it.each([
['test~T~', 'test<T>'],
['test~Array~Array~string~~~', 'test<Array<Array<string>>>'],
['test~Array~Array~string[]~~~', 'test<Array<Array<string[]>>>'],
['test ~Array~Array~string[]~~~', 'test <Array<Array<string[]>>>'],
['~test', '~test'],
['~test~T~', '~test<T>'],
])('should parse generic types: %s to %s', (input: string, expected: string) => {
expect(parseGenericTypes(input)).toEqual(expected);
});
});

it.each([
['', '', 0],
['', 'x', 0],
['test', 'x', 0],
['test', 't', 2],
['test', 'te', 1],
['test~T~', '~', 2],
['test~Array~Array~string~~~', '~', 6],
])(
'should count `%s` to contain occurrences of `%s` to be `%i`',
(str: string, substring: string, count: number) => {
expect(countOccurrence(str, substring)).toEqual(count);
}
);
25 changes: 21 additions & 4 deletions packages/mermaid/src/diagrams/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,33 @@ export const parseGenericTypes = function (input: string): string {
return output.join('');
};

export const countOccurrence = (string: string, substring: string): number => {
return Math.max(0, string.split(substring).length - 1);
};

const shouldCombineSets = (previousSet: string, nextSet: string): boolean => {
const prevCount = [...previousSet].reduce((count, char) => (char === '~' ? count + 1 : count), 0);
const nextCount = [...nextSet].reduce((count, char) => (char === '~' ? count + 1 : count), 0);
const prevCount = countOccurrence(previousSet, '~');
const nextCount = countOccurrence(nextSet, '~');

return prevCount === 1 && nextCount === 1;
};

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

if (tildeCount <= 1) {
return input;
}

// 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
if (tildeCount % 2 !== 0 && input.startsWith('~')) {
input = input.substring(1);
hasStartingTilde = true;
}

const chars = [...input];

let first = chars.indexOf('~');
let last = chars.lastIndexOf('~');

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

// Add the starting tilde back in if we removed it
if (hasStartingTilde) {
chars.unshift('~');
}

return chars.join('');
};

Expand Down

0 comments on commit abcf2a2

Please sign in to comment.