From 09058c7c8adc7d127182781ae824866161fff0a5 Mon Sep 17 00:00:00 2001 From: Chesterkxng Date: Tue, 27 May 2025 16:36:00 +0200 Subject: [PATCH] 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. --- .../tools/list/find-most-popular/service.ts | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/pages/tools/list/find-most-popular/service.ts b/src/pages/tools/list/find-most-popular/service.ts index 03bd144..1f53c0e 100644 --- a/src/pages/tools/list/find-most-popular/service.ts +++ b/src/pages/tools/list/find-most-popular/service.ts @@ -9,7 +9,20 @@ function dictMaker( ): { [key: string]: number } { const dict: { [key: string]: number } = {}; 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; } return dict; @@ -74,21 +87,27 @@ export function TopItemsList( sortingMethod: SortingMethod, displayFormat: DisplayFormat, splitSeparator: string, - input: string, + input: string | string[], deleteEmptyItems: boolean, ignoreItemCase: boolean, trimItems: boolean ): string { + if (!input) return ''; + let array: string[]; - switch (splitOperatorType) { - case 'symbol': - array = input.split(splitSeparator); - break; - case 'regex': - array = input - .split(new RegExp(splitSeparator)) - .filter((item) => item !== ''); - break; + if (typeof input === 'string') { + switch (splitOperatorType) { + case 'symbol': + array = input.split(splitSeparator); + break; + case 'regex': + array = input + .split(new RegExp(splitSeparator)) + .filter((item) => item !== ''); + break; + } + } else { + array = input; } // Trim items if required