Merge branch 'up/feat/json-string-converter' into chore/all-my-stuffs

This commit is contained in:
ShareVB committed 2024-11-20 22:41:03 +01:00
commit 0c1b98492c
4 files changed
+74

No files matched your search

+4
View File
@@ -349,6 +349,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).
+2
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 dnsQueries } from './dns-queries';
import { tool as icoConverter } from './ico-converter';
import { tool as jsonEditor } from './json-editor';
@@ -289,6 +290,7 @@ export const toolsByCategory: ToolCategory[] = [
textToNatoAlphabet,
textToBinary,
textToUnicode,
jsonStringConverter,
textToUnicodeNames,
yamlToJson,
yamlToToml,
+12
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'),
});
@@ -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>