feat(core/formatters): add rbytes, rbytes10, rbytes2 number modifiers for rounded bytes formatting

This commit is contained in:
Viren070
2025-10-20 16:52:22 +01:00
parent 4fb44be97f
commit edc771c0ec
2 changed files with 43 additions and 25 deletions
+33 -23
View File
@@ -323,10 +323,12 @@ export abstract class BaseFormatter {
)
.join('\n')
.replace(/\{tools.newLine\}/g, '\n');
}
};
}
protected async compileTemplateHelper(str: string): Promise<CompiledParseFunction> {
protected async compileTemplateHelper(
str: string
): Promise<CompiledParseFunction> {
const re = this.regexBuilder.buildRegexExpression();
let matches: RegExpExecArray | null;
@@ -341,7 +343,7 @@ export abstract class BaseFormatter {
);
}
const placeHolder = " ";
const placeHolder = ' ';
// Iterate through all {...} matches
while ((matches = re.exec(str))) {
@@ -435,30 +437,32 @@ export abstract class BaseFormatter {
};
} // end of CHECK TRUE/FALSE logic
str = str.slice(0, index) +placeHolder+ str.slice(re.lastIndex);
re.lastIndex = index+placeHolder.length;
str = str.slice(0, index) + placeHolder + str.slice(re.lastIndex);
re.lastIndex = index + placeHolder.length;
compiledMatchTemplateFns.push({
resultFn: precompiledResolvedVariableFn,
insertIndex: index,
});
} // end of while loop
compiledMatchTemplateFns = compiledMatchTemplateFns.sort((a, b) => (b.insertIndex - a.insertIndex ));
} // end of while loop
compiledMatchTemplateFns = compiledMatchTemplateFns.sort(
(a, b) => b.insertIndex - a.insertIndex
);
return (parseValue: ParseValue) => {
let resultStr = str;
// Sort by startIndex to process in reverse order
for (const { resultFn, insertIndex } of compiledMatchTemplateFns) {
const resolvedResult = resultFn(parseValue);
const resolvedResult = resultFn(parseValue);
const replacement =
resolvedResult.error ?? resolvedResult.result?.toString() ?? '';
resultStr =
resultStr.slice(0, insertIndex) +
replacement +
resultStr.slice(insertIndex+placeHolder.length);
resultStr.slice(insertIndex + placeHolder.length);
}
return resultStr
return resultStr;
};
}
@@ -632,16 +636,19 @@ export abstract class BaseFormatter {
// handle hardcoded modifiers here
switch (true) {
case mod.startsWith('replace(') && mod.endsWith(')'): {
const findStartChar = mod.charAt(8); // either " or '
const findEndChar = mod.charAt(mod.length - 2); // either " or '
const findStartChar = mod.charAt(8); // either " or '
const findEndChar = mod.charAt(mod.length - 2); // either " or '
// Extract the separator from replace(['"]...<matching'">, ['"]...<matching'">)
const content = _mod.substring(9, _mod.length - 2);
// Extract the separator from replace(['"]...<matching'">, ['"]...<matching'">)
const content = _mod.substring(9, _mod.length - 2);
// split on findStartChar<whitespace?>,<whitespace?>findEndChar
const [key, replaceKey, shouldBeUndefined] = content.split(new RegExp(`${findStartChar}\\s*,\\s*${findEndChar}`))
// split on findStartChar<whitespace?>,<whitespace?>findEndChar
const [key, replaceKey, shouldBeUndefined] = content.split(
new RegExp(`${findStartChar}\\s*,\\s*${findEndChar}`)
);
if (!shouldBeUndefined && key && replaceKey) return variable.replaceAll(key, replaceKey);
if (!shouldBeUndefined && key && replaceKey)
return variable.replaceAll(key, replaceKey);
}
}
}
@@ -813,8 +820,11 @@ class ModifierConstants {
octal: (value: number) => value.toString(8),
binary: (value: number) => value.toString(2),
bytes: (value: number) => formatBytes(value, 1000),
rbytes: (value: number) => formatBytes(value, 1000, true),
bytes10: (value: number) => formatBytes(value, 1000),
rbytes10: (value: number) => formatBytes(value, 1000, true),
bytes2: (value: number) => formatBytes(value, 1024),
rbytes2: (value: number) => formatBytes(value, 1024, true),
string: (value: number) => value.toString(),
time: (value: number) => formatDuration(value),
};
@@ -845,11 +855,11 @@ class ModifierConstants {
},
};
static hardcodedModifiersForRegexMatching = {
"replace('.*?'\\s*?,\\s*?'.*?')": null,
"replace(\".*?\"\\s*?,\\s*?'.*?')": null,
"replace('.*?'\\s*?,\\s*?\".*?\")": null,
'replace(".*?"\\s*?,\\s*?\".*?\")': null,
static hardcodedModifiersForRegexMatching = {
"replace('.*?'\\s*?,\\s*?'.*?')": null,
'replace(".*?"\\s*?,\\s*?\'.*?\')': null,
'replace(\'.*?\'\\s*?,\\s*?".*?")': null,
'replace(".*?"\\s*?,\\s*?\".*?\")': null,
"join('.*?')": null,
'join(".*?")': null,
'$.*?': null,
+10 -2
View File
@@ -1,13 +1,21 @@
import { FULL_LANGUAGE_MAPPING } from '../utils/languages.js';
export function formatBytes(bytes: number, k: 1024 | 1000): string {
export function formatBytes(
bytes: number,
k: 1024 | 1000,
round: boolean = false
): string {
if (bytes === 0) return '0 B';
const sizes =
k === 1024
? ['B', 'KiB', 'MiB', 'GiB', 'TiB']
: ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
let value = parseFloat((bytes / Math.pow(k, i)).toFixed(2));
if (round) {
value = Math.round(value);
}
return value + ' ' + sizes[i];
}
export function formatDuration(durationInMs: number): string {