-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new tool): JSON string converter
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Braces } from '@vicons/tabler'; | ||
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: Braces, | ||
createdAt: new Date('2024-10-17'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |