Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/composable/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -56,7 +56,6 @@ export function useValidation<T>({
watch(
[source, ...watchRefs],
() => {
state.message = '';
state.status = undefined;

for (const rule of get(rules)) {
Expand Down
11 changes: 6 additions & 5 deletions src/tools/json-to-toml/json-to-toml.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
<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), '');

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"
Expand Down
9 changes: 9 additions & 0 deletions src/tools/json-to-toml/locales/en.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions src/tools/json-to-toml/locales/zh.yml
Original file line number Diff line number Diff line change
@@ -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...'