This commit is contained in:
HalfCoke 2024-07-18 23:26:32 +05:30 committed by GitHub
commit 20129134f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 7 deletions

View file

@ -6,7 +6,7 @@ type ValidatorReturnType = unknown;
export interface UseValidationRule<T> {
validator: (value: T) => ValidatorReturnType
message: string
message: any
}
export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
@ -56,7 +56,6 @@ export function useValidation<T>({
watch(
[source, ...watchRefs],
() => {
state.message = '';
state.status = undefined;
for (const rule of get(rules)) {

View file

@ -1,9 +1,10 @@
<script setup lang="ts">
import { stringify as stringifyToml } from 'iarna-toml-esm';
import JSON5 from 'json5';
import { withDefaultOnError } from '../../utils/defaults';
import { withDefaultOnError } from '@/utils/defaults';
import type { UseValidationRule } from '@/composable/validation';
const { t } = useI18n();
const convertJsonToToml = (value: string) => [stringifyToml(JSON5.parse(value))].flat().join('\n').trim();
const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => convertJsonToToml(value), '');
@ -11,16 +12,16 @@ const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnE
const rules: UseValidationRule<string>[] = [
{
validator: (v: string) => v === '' || JSON5.parse(v),
message: 'Provided JSON is not valid.',
message: computed(() => t('tools.json-to-toml.invalid')),
},
];
</script>
<template>
<format-transformer
input-label="Your JSON"
input-placeholder="Paste your JSON here..."
output-label="TOML from your JSON"
:input-label="t('tools.json-to-toml.your-json')"
:input-placeholder="t('tools.json-to-toml.placeholder.your-json')"
:output-label="t('tools.json-to-toml.toml-from-json')"
output-language="toml"
:input-validation-rules="rules"
:transformer="transformer"

View file

@ -0,0 +1,9 @@
tools:
json-to-toml:
title: 'JSON to TOML'
description: 'Parse and convert JSON to TOML.'
invalid: 'Provided JSON is not valid.'
your-json: 'Your JSON'
toml-from-json: 'TOML from your JSON'
placeholder:
your-json: 'Paste your JSON here...'

View file

@ -0,0 +1,9 @@
tools:
json-to-toml:
title: 'JSON to TOML'
description: '使用这个在线转换工具简单地将JSON转换为TOML。'
invalid: '提供的JSON有问题'
your-json: '原始JSON'
toml-from-json: '转换后的TOML'
placeholder:
your-json: '在这粘贴你的JSON...'