Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions packages/core/src/formatters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,13 +892,24 @@ export abstract class BaseFormatter {
return variable.replaceAll(key, replaceKey);
}
case mod.startsWith('remove(') && mod.endsWith(')'): {
const findStartChar = mod.charAt(7); // either " or '
const findEndChar = mod.charAt(mod.length - 2); // either " or '

// Extract the content from remove(['"]...['"])
const content = _mod.substring(8, _mod.length - 2);
const content = _mod.substring(7, _mod.length - 1);

// Extract options from remove("...", "...", ...)
const regex = /"([^"]*)"|'([^']*)'/g;
const args: string[] = [];

let match;
while ((match = regex.exec(content)) !== null) {
args.push(match[1] ?? match[2] ?? '');
}

if (content) return variable.replaceAll(content, '');
if (args.length === 0) return undefined;

let result = variable;
for (const arg of args) {
if (arg) result = result.replaceAll(arg, '');
}
return result;
}
case mod.startsWith('truncate(') && mod.endsWith(')'): {
// Extract N from truncate(N)
Expand Down Expand Up @@ -946,13 +957,20 @@ export abstract class BaseFormatter {
return variable.join(separator);
}
case mod.startsWith('remove(') && mod.endsWith(')'): {
const findStartChar = mod.charAt(7); // either " or '
const findEndChar = mod.charAt(mod.length - 2); // either " or '
const content = _mod.substring(7, _mod.length - 1);

// Extract options from remove("...", "...", ...)
const regex = /"([^"]*)"|'([^']*)'/g;
const args: string[] = [];

let match;
while ((match = regex.exec(content)) !== null) {
args.push(match[1] ?? match[2] ?? '');
}

// Extract the content from remove(['"]...['"])
const content = _mod.substring(8, _mod.length - 2);
if (args.length === 0) return undefined;

if (content) return variable.filter((v) => v !== content);
return variable.filter((v) => !args.includes(v));
}
}
}
Expand Down Expand Up @@ -1201,12 +1219,11 @@ class ModifierConstants {
};

static hardcodedModifiersForRegexMatching = {
'remove(.*?)': null,
"replace('.*?'\\s*?,\\s*?'.*?')": null,
'replace(".*?"\\s*?,\\s*?\'.*?\')': null,
'replace(\'.*?\'\\s*?,\\s*?".*?")': null,
'replace(".*?"\\s*?,\\s*?\".*?\")': null,
"remove('.*?')": null,
'remove(".*?")': null,
"join('.*?')": null,
'join(".*?")': null,
'truncate(\\d+)': null,
Expand Down