refactor: review related changes

This commit is contained in:
Chesterkxng
2025-06-05 23:36:52 +02:00
parent 63623a74f3
commit 80df2eb68f
2 changed files with 45 additions and 27 deletions

View File

@@ -1,33 +1,9 @@
import { itemCounter } from '@utils/string';
export type SplitOperatorType = 'symbol' | 'regex'; export type SplitOperatorType = 'symbol' | 'regex';
export type DisplayFormat = 'count' | 'percentage' | 'total'; export type DisplayFormat = 'count' | 'percentage' | 'total';
export type SortingMethod = 'count' | 'alphabetic'; 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 that sorts the dict created with dictMaker based on the chosen sorting method
function dictSorter( function dictSorter(
dict: { [key: string]: number }, dict: { [key: string]: number },
@@ -121,7 +97,7 @@ export function TopItemsList(
} }
// Transform the array into dict // Transform the array into dict
const unsortedDict = dictMaker(array, ignoreItemCase); const unsortedDict = itemCounter(array, ignoreItemCase);
// Sort the list if required // Sort the list if required
const sortedDict = dictSorter(unsortedDict, sortingMethod); const sortedDict = dictSorter(unsortedDict, sortingMethod);

View File

@@ -1,5 +1,24 @@
import { UpdateField } from '@components/options/ToolOptions'; 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) { export function capitalizeFirstLetter(string: string | undefined) {
if (!string) return ''; if (!string) return '';
return string.charAt(0).toUpperCase() + string.slice(1); return string.charAt(0).toUpperCase() + string.slice(1);
@@ -63,3 +82,26 @@ export function unquoteIfQuoted(value: string, quoteCharacter: string): string {
} }
return value; 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;
}