Skip to content

Commit

Permalink
fix(module/date): update extractIgnoreTokens func
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodong2008 committed Dec 31, 2023
1 parent 38d9afc commit fce46db
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/date/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@ interface replacement {
}

export function extractIgnoreTokens(formatString: string): [string, string[]] {
let processedString = formatString;
const ignoreTokens: string[] = formatString.match(/<.*?>/g)?.map(match => match.slice(1, -1)) || [];
const ignoreTokens: string[] = [];
let processedString = "";
let tokenBuffer = "";
let depth = 0;

ignoreTokens.forEach((_, index) => {
processedString = processedString.replace(/<.*?>/, `{{*${index}}}`);
})
for (const char of formatString) {
if (char === '<') {
if (++depth === 1) continue;
} else if (char === '>') {
depth--;
if (depth === 0) {
ignoreTokens.push(tokenBuffer);
processedString += `{{*${ignoreTokens.length - 1}}}`;
tokenBuffer = '';
continue;
}
}

if (depth > 0) tokenBuffer += char;
else processedString += char;
}

if (depth > 0) processedString += `<${tokenBuffer}`;

return [processedString, ignoreTokens];
}
Expand Down

0 comments on commit fce46db

Please sign in to comment.