Skip to content

Commit

Permalink
feat: minify json
Browse files Browse the repository at this point in the history
  • Loading branch information
iib0011 committed Mar 8, 2025
1 parent 4a956bf commit 90d3c08
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 103 deletions.
129 changes: 68 additions & 61 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/ToolContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ToolContentPropsBase<T, I> extends ToolComponentProps {

// Tool options
initialValues: T;
getGroups: GetGroupsType<T>;
getGroups: GetGroupsType<T> | null;

// Computation function
compute: (optionsValues: T, input: I) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/components/examples/ExampleCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface ExampleCardProps<T> {
sampleResult: string;
sampleOptions: T;
changeInputResult: (newInput: string, newOptions: T) => void;
getGroups: GetGroupsType<T>;
getGroups: GetGroupsType<T> | null;
}

export default function ExampleCard<T>({
Expand Down
4 changes: 2 additions & 2 deletions src/components/examples/ExampleOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export default function ExampleOptions<T>({
getGroups
}: {
options: T;
getGroups: GetGroupsType<T>;
getGroups: GetGroupsType<T> | null;
}) {
return (
<ToolOptionGroups
// @ts-ignore
groups={getGroups({ values: options })}
groups={getGroups?.({ values: options }) ?? []}
vertical
/>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/examples/ToolExamples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface ExampleProps<T> {
title: string;
subtitle?: string;
exampleCards: CardExampleType<T>[];
getGroups: GetGroupsType<T>;
getGroups: GetGroupsType<T> | null;
formRef: React.RefObject<FormikProps<T>>;
setInput: React.Dispatch<React.SetStateAction<any>>;
}
Expand Down
73 changes: 37 additions & 36 deletions src/components/options/ToolOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,44 +86,45 @@ export default function ToolOptions<T extends FormikValues>({
validationSchema?: any | (() => any);
compute: (optionsValues: T, input: any) => void;
input?: any;
getGroups: GetGroupsType<T>;
getGroups: GetGroupsType<T> | null;
formRef?: RefObject<FormikProps<T>>;
}) {
const theme = useTheme();
return (
<Box
sx={{
mb: 2,
borderRadius: 2,
padding: 2,
backgroundColor: theme.palette.background.default,
boxShadow: '2'
}}
mt={2}
>
<Stack direction={'row'} spacing={1} alignItems={'center'}>
<SettingsIcon />
<Typography fontSize={22}>Tool options</Typography>
</Stack>
<Box mt={2}>
<Formik
innerRef={formRef}
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={() => {}}
>
{(formikProps) => (
<ToolBody
compute={compute}
input={input}
getGroups={getGroups}
formikProps={formikProps}
>
{children}
</ToolBody>
)}
</Formik>
if (getGroups)
return (
<Box
sx={{
mb: 2,
borderRadius: 2,
padding: 2,
backgroundColor: theme.palette.background.default,
boxShadow: '2'
}}
mt={2}
>
<Stack direction={'row'} spacing={1} alignItems={'center'}>
<SettingsIcon />
<Typography fontSize={22}>Tool options</Typography>
</Stack>
<Box mt={2}>
<Formik
innerRef={formRef}
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={() => {}}
>
{(formikProps) => (
<ToolBody
compute={compute}
input={input}
getGroups={getGroups}
formikProps={formikProps}
>
{children}
</ToolBody>
)}
</Formik>
</Box>
</Box>
</Box>
);
);
}
3 changes: 2 additions & 1 deletion src/pages/tools/json/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { tool as jsonPrettify } from './prettify/meta';
import { tool as jsonMinify } from './minify/meta';

export const jsonTools = [jsonPrettify];
export const jsonTools = [jsonPrettify, jsonMinify];
77 changes: 77 additions & 0 deletions src/pages/tools/json/minify/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React, { useState } from 'react';
import ToolContent from '@components/ToolContent';
import ToolTextInput from '@components/input/ToolTextInput';
import ToolTextResult from '@components/result/ToolTextResult';
import { minifyJson } from './service';
import { CardExampleType } from '@components/examples/ToolExamples';
import { ToolComponentProps } from '@tools/defineTool';

type InitialValuesType = Record<string, never>;

const initialValues: InitialValuesType = {};

const exampleCards: CardExampleType<InitialValuesType>[] = [
{
title: 'Minify a Simple JSON Object',
description:
'This example shows how to minify a simple JSON object by removing all unnecessary whitespace.',
sampleText: `{
"name": "John Doe",
"age": 30,
"city": "New York"
}`,
sampleResult: `{"name":"John Doe","age":30,"city":"New York"}`,
sampleOptions: {}
},
{
title: 'Minify a Nested JSON Structure',
description:
'This example demonstrates minification of a complex nested JSON structure with arrays and objects.',
sampleText: `{
"users": [
{
"id": 1,
"name": "Alice",
"hobbies": ["reading", "gaming"]
},
{
"id": 2,
"name": "Bob",
"hobbies": ["swimming", "coding"]
}
]
}`,
sampleResult: `{"users":[{"id":1,"name":"Alice","hobbies":["reading","gaming"]},{"id":2,"name":"Bob","hobbies":["swimming","coding"]}]}`,
sampleOptions: {}
}
];

export default function MinifyJson({ title }: ToolComponentProps) {
const [input, setInput] = useState<string>('');
const [result, setResult] = useState<string>('');

const compute = (_: InitialValuesType, input: string) => {
if (input) setResult(minifyJson(input));
};

return (
<ToolContent
title={title}
inputComponent={
<ToolTextInput title="Input JSON" value={input} onChange={setInput} />
}
resultComponent={<ToolTextResult title="Minified JSON" value={result} />}
initialValues={initialValues}
getGroups={null}
toolInfo={{
title: 'What Is JSON Minification?',
description:
"JSON minification is the process of removing all unnecessary whitespace characters from JSON data while maintaining its validity. This includes removing spaces, newlines, and indentation that aren't required for the JSON to be parsed correctly. Minification reduces the size of JSON data, making it more efficient for storage and transmission while keeping the exact same data structure and values."
}}
exampleCards={exampleCards}
input={input}
setInput={setInput}
compute={compute}
/>
);
}
Loading

0 comments on commit 90d3c08

Please sign in to comment.