feat(new tool): JSON string converter

This commit is contained in:
chadmin 2024-10-17 16:40:09 -07:00
parent 0b1b98f93e
commit f536ae79da
5 changed files with 75 additions and 0 deletions

1
components.d.ts vendored
View file

@ -109,6 +109,7 @@ declare module '@vue/runtime-core' {
Ipv6UlaGenerator: typeof import('./src/tools/ipv6-ula-generator/ipv6-ula-generator.vue')['default']
JsonDiff: typeof import('./src/tools/json-diff/json-diff.vue')['default']
JsonMinify: typeof import('./src/tools/json-minify/json-minify.vue')['default']
JsonStringConverter: typeof import('./src/tools/json-string-converter/json-string-converter.vue')['default']
JsonToCsv: typeof import('./src/tools/json-to-csv/json-to-csv.vue')['default']
JsonToToml: typeof import('./src/tools/json-to-toml/json-to-toml.vue')['default']
JsonToXml: typeof import('./src/tools/json-to-xml/json-to-xml.vue')['default']

View file

@ -333,6 +333,10 @@ tools:
title: JSON minify
description: Minify and compress your JSON by removing unnecessary whitespace.
json-string-converter:
title: JSON string converter
description: Convert your plain text or JavaScript objects into JSON string format and vice versa.
ulid-generator:
title: ULID generator
description: Generate random Universally Unique Lexicographically Sortable Identifier (ULID).

View file

@ -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 jsonStringConverter } from './json-string-converter';
import { tool as emailNormalizer } from './email-normalizer';
import { tool as asciiTextDrawer } from './ascii-text-drawer';
@ -106,6 +107,7 @@ export const toolsByCategory: ToolCategory[] = [
textToNatoAlphabet,
textToBinary,
textToUnicode,
jsonStringConverter,
yamlToJson,
yamlToToml,
jsonToYaml,

View file

@ -0,0 +1,12 @@
import { IconBraces } from '@tabler/icons-vue';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Json string converter',
path: '/json-string-converter',
description: '',
keywords: ['json', 'string', 'converter'],
component: () => import('./json-string-converter.vue'),
icon: IconBraces,
createdAt: new Date('2024-10-17'),
});

View file

@ -0,0 +1,56 @@
<script setup lang="ts">
import type { UseValidationRule } from '@/composable/validation';
import { withDefaultOnError } from '@/utils/defaults';
const defaultValue = '{\n\t"hello": [\n\t\t"world"\n\t]\n}';
// Define a reactive variable to track the selected transformation mode
const selectedMode = ref('stringify');
// Create functions for both stringification and parsing
const stringifyTransformer = (value: string) => withDefaultOnError(() => JSON.stringify(value), '');
const parseTransformer = (value: string) => withDefaultOnError(() => JSON.parse(value).toString(), '');
// Dynamically select the transformer based on the selected mode
const transformer = computed(() => {
if (selectedMode.value === 'stringify') {
return stringifyTransformer;
}
else {
return parseTransformer;
}
});
const rules: UseValidationRule<string>[] = [
{
validator: (v: string) => v === '' || (selectedMode.value === 'stringify' ? JSON.stringify(v) : JSON.parse(v)),
message: 'Provided text is not valid. (Make sure your JSON is in double quotes)',
},
];
// Dropdown options
const dropdownOptions = computed(() => [
{ label: 'JSON Stringify', value: 'stringify' },
{ label: 'JSON Parse', value: 'parse' },
]);
</script>
<template>
<c-card>
<c-select
v-model:value="selectedMode"
label="Transformation Mode"
:options="dropdownOptions"
/>
</c-card>
<div />
<format-transformer
input-label="Your text / JSON string"
:input-default="defaultValue"
input-placeholder="Paste your text here..."
output-label="JSON string conversion of your input"
output-language="string"
:input-validation-rules="rules"
:transformer="transformer"
/>
</template>