update top list items functions to (1) replace special characters by symbols when building the dict and (2) take a built array with custom regex as input.

This commit is contained in:
Chesterkxng
2025-05-27 16:36:00 +02:00
parent 962e30d12a
commit 09058c7c8a

View File

@@ -9,7 +9,20 @@ function dictMaker(
): { [key: string]: number } { ): { [key: string]: number } {
const dict: { [key: string]: number } = {}; const dict: { [key: string]: number } = {};
for (const item of array) { for (const item of array) {
const key = ignoreItemCase ? item.toLowerCase() : item; let key = ignoreItemCase ? item.toLowerCase() : item;
const specialCharMap: { [key: string]: string } = {
' ': '␣',
'\n': '↲',
'\t': '⇥',
'\r': '␍',
'\f': '␌',
'\v': '␋'
};
if (key in specialCharMap) {
key = specialCharMap[key];
}
dict[key] = (dict[key] || 0) + 1; dict[key] = (dict[key] || 0) + 1;
} }
return dict; return dict;
@@ -74,12 +87,15 @@ export function TopItemsList(
sortingMethod: SortingMethod, sortingMethod: SortingMethod,
displayFormat: DisplayFormat, displayFormat: DisplayFormat,
splitSeparator: string, splitSeparator: string,
input: string, input: string | string[],
deleteEmptyItems: boolean, deleteEmptyItems: boolean,
ignoreItemCase: boolean, ignoreItemCase: boolean,
trimItems: boolean trimItems: boolean
): string { ): string {
if (!input) return '';
let array: string[]; let array: string[];
if (typeof input === 'string') {
switch (splitOperatorType) { switch (splitOperatorType) {
case 'symbol': case 'symbol':
array = input.split(splitSeparator); array = input.split(splitSeparator);
@@ -90,6 +106,9 @@ export function TopItemsList(
.filter((item) => item !== ''); .filter((item) => item !== '');
break; break;
} }
} else {
array = input;
}
// Trim items if required // Trim items if required
if (trimItems) { if (trimItems) {