improvements to parseGenericTypes

This commit is contained in:
Justin Greywolf
2023-08-25 10:58:20 -07:00
parent 0881619fcb
commit 8b96282c48
2 changed files with 68 additions and 16 deletions

View File

@@ -397,7 +397,7 @@ class C13["With Città foreign language"]
{ {
"annotations": [], "annotations": [],
"cssClasses": [], "cssClasses": [],
"domId": "classId-Student-34", "domId": "classId-Student-134",
"id": "Student", "id": "Student",
"label": "Student", "label": "Student",
"members": [ "members": [

View File

@@ -178,27 +178,79 @@ export const getMin = function (...values: number[]): number {
* @param text - The text to convert * @param text - The text to convert
* @returns The converted string * @returns The converted string
*/ */
export const parseGenericTypes = function (text: string): string { export const parseGenericTypes = function (input: string): string {
if (!text) { const inputSets = input.split(/(,)/);
return ''; const output = [];
let finalResult = '';
let skipNextSet = false;
for (let i = 0; i < inputSets.length; i++) {
const previousIndex = i - 1;
const nextIndex = i + 1;
let thisSet = inputSets[i];
// based on logic below - if we have already combined this set with the previous, we want to skip it
if (skipNextSet) {
continue;
} }
let cleanedText = text; // if the original input included a value such as "~K, V~"", these will be split into
// an array of ["~K",","," V~"].
if (text.split('~').length - 1 >= 2) { // This means that on each call of processSet, there will only be 1 ~ present
let newCleanedText = cleanedText; // To account for this, if we encounter a ",", we are checking the previous and next sets in the array
// to see if they contain matching ~'s
// use a do...while loop instead of replaceAll to detect recursion // in which case we are assuming that they should be rejoined and sent to be processed
// e.g. Array~Array~T~~ // we are also removing
do { if (thisSet === ',' && previousIndex > -1 && nextIndex <= inputSets.length) {
cleanedText = newCleanedText; const previousSet = inputSets[i - 1];
newCleanedText = cleanedText.replace(/~([^\s:;]+)~/, '<$1>'); const nextSet = inputSets[i + 1];
} while (newCleanedText != cleanedText); if (shouldCombineSets(previousSet, nextSet)) {
thisSet = previousSet + ',' + nextSet;
return parseGenericTypes(newCleanedText); skipNextSet = true;
// remove previous set
output.pop();
}
} else { } else {
return cleanedText; skipNextSet = false;
} }
output.push(processSet(thisSet));
}
finalResult = output.join('');
// one last scan to see if any sets were missed
finalResult = processSet(finalResult);
return finalResult;
};
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);
return prevCount === 1 && nextCount === 1;
};
const processSet = (input: string): string => {
const chars = [...input];
const tildeCount = chars.reduce((count, char) => (char === '~' ? count + 1 : count), 0);
// ignoring any
if (tildeCount <= 1) {
return input;
}
let first = chars.indexOf('~');
let last = chars.lastIndexOf('~');
while (first !== -1 && last !== -1 && first !== last) {
chars[first] = '<';
chars[last] = '>';
first = chars.indexOf('~');
last = chars.lastIndexOf('~');
}
return chars.join('');
}; };
export default { export default {