diff --git a/src/pages/tools/list/find-most-popular/service.ts b/src/pages/tools/list/find-most-popular/service.ts index 1f53c0e..5fc9d5e 100644 --- a/src/pages/tools/list/find-most-popular/service.ts +++ b/src/pages/tools/list/find-most-popular/service.ts @@ -1,33 +1,9 @@ +import { itemCounter } from '@utils/string'; + export type SplitOperatorType = 'symbol' | 'regex'; export type DisplayFormat = 'count' | 'percentage' | 'total'; export type SortingMethod = 'count' | 'alphabetic'; -// Function that takes the array as arg and returns a dict of element occurrences and handle the ignoreItemCase -function dictMaker( - array: string[], - ignoreItemCase: boolean -): { [key: string]: number } { - const dict: { [key: string]: number } = {}; - for (const item of array) { - 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; -} - // Function that sorts the dict created with dictMaker based on the chosen sorting method function dictSorter( dict: { [key: string]: number }, @@ -121,7 +97,7 @@ export function TopItemsList( } // Transform the array into dict - const unsortedDict = dictMaker(array, ignoreItemCase); + const unsortedDict = itemCounter(array, ignoreItemCase); // Sort the list if required const sortedDict = dictSorter(unsortedDict, sortingMethod); diff --git a/src/utils/string.ts b/src/utils/string.ts index a0c2894..17088f9 100644 --- a/src/utils/string.ts +++ b/src/utils/string.ts @@ -1,5 +1,24 @@ import { UpdateField } from '@components/options/ToolOptions'; +// Here starting the shared values for string manipulation. + +/** + * This map is used to replace special characters with their visual representations. + * It is useful for displaying strings in a more readable format, especially in tools + **/ + +export const specialCharMap: { [key: string]: string } = { + '': '␀', + ' ': '␣', + '\n': '↲', + '\t': '⇥', + '\r': '␍', + '\f': '␌', + '\v': '␋' +}; + +// Here starting the utility functions for string manipulation. + export function capitalizeFirstLetter(string: string | undefined) { if (!string) return ''; return string.charAt(0).toUpperCase() + string.slice(1); @@ -63,3 +82,26 @@ export function unquoteIfQuoted(value: string, quoteCharacter: string): string { } return value; } + +/** + * Count the occurence of items. + * @param array - array get from user with a custom delimiter. + * @param ignoreItemCase - boolean status to ignore the case i . + * @returns Dict of Items count {[Item]: occcurence}. + */ +export function itemCounter( + array: string[], + ignoreItemCase: boolean +): { [key: string]: number } { + const dict: { [key: string]: number } = {}; + for (const item of array) { + let key = ignoreItemCase ? item.toLowerCase() : item; + + if (key in specialCharMap) { + key = specialCharMap[key]; + } + + dict[key] = (dict[key] || 0) + 1; + } + return dict; +}