mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 13:29:13 -04:00
Merge 66eceb08ed
into 76a19d218d
This commit is contained in:
commit
20129134f6
4 changed files with 25 additions and 7 deletions
|
@ -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)) {
|
||||
|
|
|
@ -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"
|
||||
|
|
9
src/tools/json-to-toml/locales/en.yml
Normal file
9
src/tools/json-to-toml/locales/en.yml
Normal 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...'
|
9
src/tools/json-to-toml/locales/zh.yml
Normal file
9
src/tools/json-to-toml/locales/zh.yml
Normal 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...'
|
Loading…
Add table
Add a link
Reference in a new issue