-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #107
- Loading branch information
Showing
2 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React, { useEffect } from 'react'; | ||
import { DefaultEditor } from '../DefaultEditor'; | ||
import { faker } from '@faker-js/faker'; | ||
import Grid from '@mui/material/Grid'; | ||
import TextField from '@mui/material/TextField'; | ||
import Typography from '@mui/material/Typography'; | ||
|
||
const ANALYZED_CHARS = | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 :;,.!?*+^${}()|/\\'; | ||
const escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
|
||
export const StringAnalyzer = () => { | ||
const [input, setInput] = React.useState(''); | ||
const [characters, setCharacters] = React.useState(0); | ||
const [lines, setLines] = React.useState(0); | ||
const [words, setWords] = React.useState(0); | ||
const [alphabets, setAlphabets] = React.useState< | ||
{ char: string; count: number }[] | ||
>([]); | ||
|
||
useEffect(() => { | ||
setCharacters(input.length); | ||
setLines(input ? input.split(/\r\n|\r|\n/g).length : 0); | ||
setWords(input ? input.split(/\s+/).length : 0); | ||
const charCounts = []; | ||
let totalCount = 0; | ||
for (const char of ANALYZED_CHARS) { | ||
const count = input.split(new RegExp(escapeRegex(char), 'gi')).length - 1; | ||
totalCount += count; | ||
charCounts.push({ char: char === ' ' ? 'Whitespace' : char, count }); | ||
} | ||
charCounts.push({ char: 'Others', count: input.length - totalCount }); | ||
setAlphabets(charCounts); | ||
}, [input]); | ||
|
||
return ( | ||
<DefaultEditor | ||
input={input} | ||
setInput={setInput} | ||
allowFileUpload | ||
acceptFileTypes=".json,.csv,.txt,.html,.xml,.yaml,.yml,.log,.md,.markdown,.js,.ts,.c,.cpp,.java,.py,.rb,.php,.sh,.bat" | ||
sample={faker.lorem.paragraphs(Math.random() * 10 + 1, '\n')} | ||
rightContent={ | ||
<> | ||
<Grid container> | ||
<Grid item xs={12}> | ||
<Typography variant="h6">Overall stats</Typography> | ||
</Grid> | ||
<Grid item> | ||
<TextField | ||
label="Characters" | ||
value={characters} | ||
variant="standard" | ||
/> | ||
</Grid> | ||
<Grid item> | ||
<TextField label="Lines" value={lines} variant="standard" /> | ||
</Grid> | ||
<Grid item> | ||
<TextField label="Words" value={words} variant="standard" /> | ||
</Grid> | ||
</Grid> | ||
<Grid container style={{ marginTop: '1rem' }}> | ||
<Grid item xs={12}> | ||
<Typography variant="h6">Character stats</Typography> | ||
</Grid> | ||
{alphabets.map(({ char, count }) => ( | ||
<Grid item key={char}> | ||
<TextField | ||
label={char} | ||
value={count} | ||
size="small" | ||
variant="standard" | ||
/> | ||
</Grid> | ||
))} | ||
</Grid> | ||
</> | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
export default StringAnalyzer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters