Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new tool): JSON string converter #1351

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -106,6 +107,7 @@ export const toolsByCategory: ToolCategory[] = [
textToNatoAlphabet,
textToBinary,
textToUnicode,
jsonStringConverter,
yamlToJson,
yamlToToml,
jsonToYaml,
Expand Down
12 changes: 12 additions & 0 deletions src/tools/json-string-converter/index.ts
Original file line number Diff line number Diff line change
@@ -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'),
});
56 changes: 56 additions & 0 deletions src/tools/json-string-converter/json-string-converter.vue
Original file line number Diff line number Diff line change
@@ -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>
Loading