Skip to content

Commit 0c1b984

Browse files
committed
Merge branch 'up/feat/json-string-converter' into chore/all-my-stuffs
2 parents e7b70a5 + f536ae7 commit 0c1b984

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

locales/en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ tools:
349349
title: JSON minify
350350
description: Minify and compress your JSON by removing unnecessary whitespace.
351351

352+
json-string-converter:
353+
title: JSON string converter
354+
description: Convert your plain text or JavaScript objects into JSON string format and vice versa.
355+
352356
ulid-generator:
353357
title: ULID generator
354358
description: Generate random Universally Unique Lexicographically Sortable Identifier (ULID).

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { tool as base64FileConverter } from './base64-file-converter';
22
import { tool as base64StringConverter } from './base64-string-converter';
33
import { tool as basicAuthGenerator } from './basic-auth-generator';
4+
import { tool as jsonStringConverter } from './json-string-converter';
45
import { tool as dnsQueries } from './dns-queries';
56
import { tool as icoConverter } from './ico-converter';
67
import { tool as jsonEditor } from './json-editor';
@@ -289,6 +290,7 @@ export const toolsByCategory: ToolCategory[] = [
289290
textToNatoAlphabet,
290291
textToBinary,
291292
textToUnicode,
293+
jsonStringConverter,
292294
textToUnicodeNames,
293295
yamlToJson,
294296
yamlToToml,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { IconBraces } from '@tabler/icons-vue';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Json string converter',
6+
path: '/json-string-converter',
7+
description: '',
8+
keywords: ['json', 'string', 'converter'],
9+
component: () => import('./json-string-converter.vue'),
10+
icon: IconBraces,
11+
createdAt: new Date('2024-10-17'),
12+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<script setup lang="ts">
2+
import type { UseValidationRule } from '@/composable/validation';
3+
import { withDefaultOnError } from '@/utils/defaults';
4+
5+
const defaultValue = '{\n\t"hello": [\n\t\t"world"\n\t]\n}';
6+
7+
// Define a reactive variable to track the selected transformation mode
8+
const selectedMode = ref('stringify');
9+
10+
// Create functions for both stringification and parsing
11+
const stringifyTransformer = (value: string) => withDefaultOnError(() => JSON.stringify(value), '');
12+
const parseTransformer = (value: string) => withDefaultOnError(() => JSON.parse(value).toString(), '');
13+
14+
// Dynamically select the transformer based on the selected mode
15+
const transformer = computed(() => {
16+
if (selectedMode.value === 'stringify') {
17+
return stringifyTransformer;
18+
}
19+
else {
20+
return parseTransformer;
21+
}
22+
});
23+
24+
const rules: UseValidationRule<string>[] = [
25+
{
26+
validator: (v: string) => v === '' || (selectedMode.value === 'stringify' ? JSON.stringify(v) : JSON.parse(v)),
27+
message: 'Provided text is not valid. (Make sure your JSON is in double quotes)',
28+
},
29+
];
30+
31+
// Dropdown options
32+
const dropdownOptions = computed(() => [
33+
{ label: 'JSON Stringify', value: 'stringify' },
34+
{ label: 'JSON Parse', value: 'parse' },
35+
]);
36+
</script>
37+
38+
<template>
39+
<c-card>
40+
<c-select
41+
v-model:value="selectedMode"
42+
label="Transformation Mode"
43+
:options="dropdownOptions"
44+
/>
45+
</c-card>
46+
<div />
47+
<format-transformer
48+
input-label="Your text / JSON string"
49+
:input-default="defaultValue"
50+
input-placeholder="Paste your text here..."
51+
output-label="JSON string conversion of your input"
52+
output-language="string"
53+
:input-validation-rules="rules"
54+
:transformer="transformer"
55+
/>
56+
</template>

0 commit comments

Comments
 (0)