File tree Expand file tree Collapse file tree 2 files changed +45
-27
lines changed
pages/tools/list/find-most-popular Expand file tree Collapse file tree 2 files changed +45
-27
lines changed Original file line number Diff line number Diff line change 1+ import { itemCounter } from '@utils/string' ;
2+
13export type SplitOperatorType = 'symbol' | 'regex' ;
24export type DisplayFormat = 'count' | 'percentage' | 'total' ;
35export 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
328function 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 ) ;
Original file line number Diff line number Diff line change 11import { 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+
322export 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+ }
You can’t perform that action at this time.
0 commit comments