mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-04 13:29:13 -04:00
feat(i18n): json to toml
This commit is contained in:
parent
7a70dbbe0c
commit
66eceb08ed
4 changed files with 25 additions and 7 deletions
|
@ -6,7 +6,7 @@ type ValidatorReturnType = unknown;
|
||||||
|
|
||||||
export interface UseValidationRule<T> {
|
export interface UseValidationRule<T> {
|
||||||
validator: (value: T) => ValidatorReturnType
|
validator: (value: T) => ValidatorReturnType
|
||||||
message: string
|
message: any
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
|
export function isFalsyOrHasThrown(cb: () => ValidatorReturnType): boolean {
|
||||||
|
@ -56,7 +56,6 @@ export function useValidation<T>({
|
||||||
watch(
|
watch(
|
||||||
[source, ...watchRefs],
|
[source, ...watchRefs],
|
||||||
() => {
|
() => {
|
||||||
state.message = '';
|
|
||||||
state.status = undefined;
|
state.status = undefined;
|
||||||
|
|
||||||
for (const rule of get(rules)) {
|
for (const rule of get(rules)) {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { stringify as stringifyToml } from 'iarna-toml-esm';
|
import { stringify as stringifyToml } from 'iarna-toml-esm';
|
||||||
import JSON5 from 'json5';
|
import JSON5 from 'json5';
|
||||||
import { withDefaultOnError } from '../../utils/defaults';
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import type { UseValidationRule } from '@/composable/validation';
|
import type { UseValidationRule } from '@/composable/validation';
|
||||||
|
|
||||||
|
const { t } = useI18n();
|
||||||
const convertJsonToToml = (value: string) => [stringifyToml(JSON5.parse(value))].flat().join('\n').trim();
|
const convertJsonToToml = (value: string) => [stringifyToml(JSON5.parse(value))].flat().join('\n').trim();
|
||||||
|
|
||||||
const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => convertJsonToToml(value), '');
|
const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => convertJsonToToml(value), '');
|
||||||
|
@ -11,16 +12,16 @@ const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnE
|
||||||
const rules: UseValidationRule<string>[] = [
|
const rules: UseValidationRule<string>[] = [
|
||||||
{
|
{
|
||||||
validator: (v: string) => v === '' || JSON5.parse(v),
|
validator: (v: string) => v === '' || JSON5.parse(v),
|
||||||
message: 'Provided JSON is not valid.',
|
message: computed(() => t('tools.json-to-toml.invalid')),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<format-transformer
|
<format-transformer
|
||||||
input-label="Your JSON"
|
:input-label="t('tools.json-to-toml.your-json')"
|
||||||
input-placeholder="Paste your JSON here..."
|
:input-placeholder="t('tools.json-to-toml.placeholder.your-json')"
|
||||||
output-label="TOML from your JSON"
|
:output-label="t('tools.json-to-toml.toml-from-json')"
|
||||||
output-language="toml"
|
output-language="toml"
|
||||||
:input-validation-rules="rules"
|
:input-validation-rules="rules"
|
||||||
:transformer="transformer"
|
: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