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,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;
}