diff options
author | 2023-02-04 16:56:17 +0800 | |
---|---|---|
committer | 2023-02-04 09:56:17 +0100 | |
commit | f708f5091e2182fc88e7cf3e7d23b3d05edc04da (patch) | |
tree | 4ae4cb3d3fb53cf194cc8125a8926ff6b3e837b1 /src | |
parent | db817a2459e23bd096274a7f91815d613d5f7ff4 (diff) | |
download | it-tools-f708f5091e2182fc88e7cf3e7d23b3d05edc04da.tar.gz it-tools-f708f5091e2182fc88e7cf3e7d23b3d05edc04da.tar.zst it-tools-f708f5091e2182fc88e7cf3e7d23b3d05edc04da.zip |
feat(new-tool): json minify (#265)
Co-authored-by: Corentin THOMASSET <corentin.thomasset74@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/index.ts | 3 | ||||
-rw-r--r-- | src/tools/json-minify/index.ts | 11 | ||||
-rw-r--r-- | src/tools/json-minify/json-minify.vue | 57 |
3 files changed, 70 insertions, 1 deletions
diff --git a/src/tools/index.ts b/src/tools/index.ts index a5130e6..a7333a1 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -1,6 +1,7 @@ import { tool as base64FileConverter } from './base64-file-converter'; import { tool as base64StringConverter } from './base64-string-converter'; import { tool as basicAuthGenerator } from './basic-auth-generator'; +import { tool as jsonMinify } from './json-minify'; import { tool as bcrypt } from './bcrypt'; import { tool as bip39 } from './bip39-generator'; import { tool as caseConverter } from './case-converter'; @@ -74,7 +75,7 @@ export const toolsByCategory: ToolCategory[] = [ }, { name: 'Development', - components: [gitMemo, randomPortGenerator, crontabGenerator, jsonViewer, sqlPrettify, chmodCalculator], + components: [gitMemo, randomPortGenerator, crontabGenerator, jsonViewer, jsonMinify, sqlPrettify, chmodCalculator], }, { name: 'Math', diff --git a/src/tools/json-minify/index.ts b/src/tools/json-minify/index.ts new file mode 100644 index 0000000..e6a02db --- /dev/null +++ b/src/tools/json-minify/index.ts @@ -0,0 +1,11 @@ +import { Braces } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'JSON minify', + path: '/json-minify', + description: 'Minify and compress your JSON by removing unnecessary white spaces.', + keywords: ['json', 'minify', 'format'], + component: () => import('./json-minify.vue'), + icon: Braces, +}); diff --git a/src/tools/json-minify/json-minify.vue b/src/tools/json-minify/json-minify.vue new file mode 100644 index 0000000..92ab7d2 --- /dev/null +++ b/src/tools/json-minify/json-minify.vue @@ -0,0 +1,57 @@ +<template> + <n-form-item + label="Your raw json" + :feedback="rawJsonValidation.message" + :validation-status="rawJsonValidation.status" + > + <n-input + ref="inputElement" + v-model:value="rawJson" + placeholder="Paste your raw json here..." + type="textarea" + rows="20" + autocomplete="off" + autocorrect="off" + autocapitalize="off" + spellcheck="false" + /> + </n-form-item> + <n-form-item label="Minify version of your JSON"> + <textarea-copyable :value="cleanJson" language="json" :follow-height-of="inputElement" /> + </n-form-item> +</template> + +<script setup lang="ts"> +import TextareaCopyable from '@/components/TextareaCopyable.vue'; +import { useValidation } from '@/composable/validation'; +import { withDefaultOnError } from '@/utils/defaults'; +import JSON5 from 'json5'; +import { computed, ref } from 'vue'; + +const inputElement = ref<HTMLElement>(); + +const rawJson = ref('{\n\t"hello": [\n\t\t"world"\n\t]\n}'); +const cleanJson = computed(() => withDefaultOnError(() => JSON.stringify(JSON5.parse(rawJson.value), null, 0), '')); + +const rawJsonValidation = useValidation({ + source: rawJson, + rules: [ + { + validator: (v) => v === '' || JSON5.parse(v), + message: 'Provided JSON is not valid.', + }, + ], +}); +</script> + +<style lang="less" scoped> +.result-card { + position: relative; + + .copy-button { + position: absolute; + top: 10px; + right: 10px; + } +} +</style> |