Skip to content

Commit 80df2eb

Browse files
committed
refactor: review related changes
1 parent 63623a7 commit 80df2eb

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

src/pages/tools/list/find-most-popular/service.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,9 @@
1+
import { itemCounter } from '@utils/string';
2+
13
export type SplitOperatorType = 'symbol' | 'regex';
24
export type DisplayFormat = 'count' | 'percentage' | 'total';
35
export type SortingMethod = 'count' | 'alphabetic';
46

5-
// Function that takes the array as arg and returns a dict of element occurrences and handle the ignoreItemCase
6-
function dictMaker(
7-
array: string[],
8-
ignoreItemCase: boolean
9-
): { [key: string]: number } {
10-
const dict: { [key: string]: number } = {};
11-
for (const item of array) {
12-
let key = ignoreItemCase ? item.toLowerCase() : item;
13-
14-
const specialCharMap: { [key: string]: string } = {
15-
' ': '␣',
16-
'\n': '↲',
17-
'\t': '⇥',
18-
'\r': '␍',
19-
'\f': '␌',
20-
'\v': '␋'
21-
};
22-
if (key in specialCharMap) {
23-
key = specialCharMap[key];
24-
}
25-
26-
dict[key] = (dict[key] || 0) + 1;
27-
}
28-
return dict;
29-
}
30-
317
// Function that sorts the dict created with dictMaker based on the chosen sorting method
328
function dictSorter(
339
dict: { [key: string]: number },
@@ -121,7 +97,7 @@ export function TopItemsList(
12197
}
12298

12399
// Transform the array into dict
124-
const unsortedDict = dictMaker(array, ignoreItemCase);
100+
const unsortedDict = itemCounter(array, ignoreItemCase);
125101

126102
// Sort the list if required
127103
const sortedDict = dictSorter(unsortedDict, sortingMethod);

src/utils/string.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
import { UpdateField } from '@components/options/ToolOptions';
22

3+
// Here starting the shared values for string manipulation.
4+
5+
/**
6+
* This map is used to replace special characters with their visual representations.
7+
* It is useful for displaying strings in a more readable format, especially in tools
8+
**/
9+
10+
export const specialCharMap: { [key: string]: string } = {
11+
'': '␀',
12+
' ': '␣',
13+
'\n': '↲',
14+
'\t': '⇥',
15+
'\r': '␍',
16+
'\f': '␌',
17+
'\v': '␋'
18+
};
19+
20+
// Here starting the utility functions for string manipulation.
21+
322
export function capitalizeFirstLetter(string: string | undefined) {
423
if (!string) return '';
524
return string.charAt(0).toUpperCase() + string.slice(1);
@@ -63,3 +82,26 @@ export function unquoteIfQuoted(value: string, quoteCharacter: string): string {
6382
}
6483
return value;
6584
}
85+
86+
/**
87+
* Count the occurence of items.
88+
* @param array - array get from user with a custom delimiter.
89+
* @param ignoreItemCase - boolean status to ignore the case i .
90+
* @returns Dict of Items count {[Item]: occcurence}.
91+
*/
92+
export function itemCounter(
93+
array: string[],
94+
ignoreItemCase: boolean
95+
): { [key: string]: number } {
96+
const dict: { [key: string]: number } = {};
97+
for (const item of array) {
98+
let key = ignoreItemCase ? item.toLowerCase() : item;
99+
100+
if (key in specialCharMap) {
101+
key = specialCharMap[key];
102+
}
103+
104+
dict[key] = (dict[key] || 0) + 1;
105+
}
106+
return dict;
107+
}

0 commit comments

Comments
 (0)