Skip to content

Commit

Permalink
feat: add minifier functionality (#63)
Browse files Browse the repository at this point in the history
Add in the minifier function and general formatJson function to the
services.ts file for JSONFormatter.

Add the minify button and connect it to the function to actually work as
expected.
  • Loading branch information
yodigi7 authored Jan 13, 2025
1 parent 148bc19 commit 803b282
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/containers/JSONFormatter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import SaveIcon from '@mui/icons-material/Save';
import CloseFullscreenIcon from '@mui/icons-material/CloseFullscreen';
import WrapTextIcon from '@mui/icons-material/WrapText';
import { Box, Toolbar } from '@mui/material';
import Button from '@mui/material/Button';
Expand Down Expand Up @@ -48,9 +49,14 @@ const JSONFormatter: React.FC<Props> = ({ inputText, storeInputText }) => {
const [formatted, setFormatted] = React.useState('');

React.useEffect(() => {
setFormatted(services.formatJson(inputText));
setFormatted(services.prettifyJson(inputText));
}, [inputText]);

const handleMinify = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
setFormatted(services.minifyJson(inputText));
};

const handleSaveAs = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
fileService.saveJsonAs(formatted);
Expand Down Expand Up @@ -81,6 +87,15 @@ const JSONFormatter: React.FC<Props> = ({ inputText, storeInputText }) => {

<Toolbar className={classes.toolbar}>
<Box component='div' flexGrow={1}></Box>
<Button
endIcon={<CloseFullscreenIcon></CloseFullscreenIcon>}
variant='contained'
color='primary'
sx={{ mr: 1 }}
onClick={handleMinify}
>
Minify
</Button>
<CopyButton data={formatted} sx={{ mr: 1 }} />
<Button
endIcon={<SaveIcon>Save As…</SaveIcon>}
Expand Down
22 changes: 15 additions & 7 deletions src/containers/JSONFormatter/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
const replacer = (_key: string, value: any) =>
value instanceof Object && !(value instanceof Array)
? Object.keys(value)
.sort()
.reduce((sorted: any, key: string) => {
sorted[key] = value[key];
return sorted;
}, {})
.sort()
.reduce((sorted: any, key: string) => {
sorted[key] = value[key];
return sorted;
}, {})
: value;

export function formatJson(value?: string): string {
export function prettifyJson(value?: string): string {
return formatJson(4, value);
}

export function formatJson(space: number, value?: string): string {
if (!value) {
return '';
}

try {
const obj = JSON.parse(value);
return JSON.stringify(obj, replacer, 4);
return JSON.stringify(obj, replacer, space);
} catch (_e) {
// do nothing user may still be typing...
return value;
}
}

export function minifyJson(value?: string): string {
return formatJson(0, value)
}

0 comments on commit 803b282

Please sign in to comment.