mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-05 05:14:08 +01:00
fix: Add support for ~test Array~string~
This commit is contained in:
@@ -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', () => {
|
describe('when securityLevel is antiscript, all script must be removed', () => {
|
||||||
/**
|
/**
|
||||||
@@ -59,15 +59,29 @@ describe('Sanitize text', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('generic parser', () => {
|
describe('generic parser', () => {
|
||||||
it('should parse generic types', () => {
|
it.each([
|
||||||
expect(parseGenericTypes('test~T~')).toEqual('test<T>');
|
['test~T~', 'test<T>'],
|
||||||
expect(parseGenericTypes('test~Array~Array~string~~~')).toEqual('test<Array<Array<string>>>');
|
['test~Array~Array~string~~~', 'test<Array<Array<string>>>'],
|
||||||
expect(parseGenericTypes('test~Array~Array~string[]~~~')).toEqual(
|
['test~Array~Array~string[]~~~', 'test<Array<Array<string[]>>>'],
|
||||||
'test<Array<Array<string[]>>>'
|
['test ~Array~Array~string[]~~~', 'test <Array<Array<string[]>>>'],
|
||||||
);
|
['~test', '~test'],
|
||||||
expect(parseGenericTypes('test ~Array~Array~string[]~~~')).toEqual(
|
['~test~T~', '~test<T>'],
|
||||||
'test <Array<Array<string[]>>>'
|
])('should parse generic types: %s to %s', (input: string, expected: string) => {
|
||||||
);
|
expect(parseGenericTypes(input)).toEqual(expected);
|
||||||
expect(parseGenericTypes('~test')).toEqual('~test');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|||||||
@@ -208,21 +208,33 @@ export const parseGenericTypes = function (input: string): string {
|
|||||||
return output.join('');
|
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 shouldCombineSets = (previousSet: string, nextSet: string): boolean => {
|
||||||
const prevCount = [...previousSet].reduce((count, char) => (char === '~' ? count + 1 : count), 0);
|
const prevCount = countOccurrence(previousSet, '~');
|
||||||
const nextCount = [...nextSet].reduce((count, char) => (char === '~' ? count + 1 : count), 0);
|
const nextCount = countOccurrence(nextSet, '~');
|
||||||
|
|
||||||
return prevCount === 1 && nextCount === 1;
|
return prevCount === 1 && nextCount === 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
const processSet = (input: string): string => {
|
const processSet = (input: string): string => {
|
||||||
const chars = [...input];
|
const tildeCount = countOccurrence(input, '~');
|
||||||
const tildeCount = chars.reduce((count, char) => (char === '~' ? count + 1 : count), 0);
|
let hasStartingTilde = false;
|
||||||
|
|
||||||
if (tildeCount <= 1) {
|
if (tildeCount <= 1) {
|
||||||
return input;
|
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 first = chars.indexOf('~');
|
||||||
let last = chars.lastIndexOf('~');
|
let last = chars.lastIndexOf('~');
|
||||||
|
|
||||||
@@ -234,6 +246,11 @@ const processSet = (input: string): string => {
|
|||||||
last = chars.lastIndexOf('~');
|
last = chars.lastIndexOf('~');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the starting tilde back in if we removed it
|
||||||
|
if (hasStartingTilde) {
|
||||||
|
chars.unshift('~');
|
||||||
|
}
|
||||||
|
|
||||||
return chars.join('');
|
return chars.join('');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user