mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-24 00:19:34 +02:00
feat: minify json
This commit is contained in:
@@ -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
src/pages/tools/json/minify/index.tsx
Normal file
77
src/pages/tools/json/minify/index.tsx
Normal file
@@ -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}
|
||||
/>
|
||||
);
|
||||
}
|
12
src/pages/tools/json/minify/meta.ts
Normal file
12
src/pages/tools/json/minify/meta.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { defineTool } from '@tools/defineTool';
|
||||
import { lazy } from 'react';
|
||||
|
||||
export const tool = defineTool('json', {
|
||||
name: 'Minify JSON',
|
||||
path: 'minify',
|
||||
icon: 'lets-icons:json-light',
|
||||
description: 'Minify your JSON by removing all unnecessary whitespace and formatting. This tool compresses JSON data to its smallest possible size while maintaining valid JSON structure.',
|
||||
shortDescription: 'Remove all unnecessary whitespace from JSON data.',
|
||||
keywords: ['minify', 'compress', 'minimize', 'json', 'compact'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
10
src/pages/tools/json/minify/service.ts
Normal file
10
src/pages/tools/json/minify/service.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export const minifyJson = (text: string) => {
|
||||
let parsedJson;
|
||||
try {
|
||||
parsedJson = JSON.parse(text);
|
||||
} catch (e) {
|
||||
throw new Error('Invalid JSON string');
|
||||
}
|
||||
|
||||
return JSON.stringify(parsedJson);
|
||||
};
|
Reference in New Issue
Block a user