aboutsummaryrefslogtreecommitdiff
path: root/src/tools/json-to-toml/json-to-toml.vue
diff options
context:
space:
mode:
authorGravatar Corentin Thomasset <corentin.thomasset74@gmail.com> 2023-06-23 21:40:30 +0200
committerGravatar Corentin THOMASSET <corentin.thomasset74@gmail.com> 2023-06-23 21:47:23 +0200
commitea50a3fc655f329d20895fee40042ab4aa8d2be6 (patch)
tree44170fdf28e560c50c2c869424e235b3e6aefa48 /src/tools/json-to-toml/json-to-toml.vue
parent746e5bdcccb37a39d1b46fc83740e881e49aa9b1 (diff)
downloadit-tools-ea50a3fc655f329d20895fee40042ab4aa8d2be6.tar.gz
it-tools-ea50a3fc655f329d20895fee40042ab4aa8d2be6.tar.zst
it-tools-ea50a3fc655f329d20895fee40042ab4aa8d2be6.zip
feat(new-tool): json to toml
Diffstat (limited to 'src/tools/json-to-toml/json-to-toml.vue')
-rw-r--r--src/tools/json-to-toml/json-to-toml.vue28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/tools/json-to-toml/json-to-toml.vue b/src/tools/json-to-toml/json-to-toml.vue
new file mode 100644
index 0000000..b1d37a3
--- /dev/null
+++ b/src/tools/json-to-toml/json-to-toml.vue
@@ -0,0 +1,28 @@
+<script setup lang="ts">
+import { stringify as stringifyToml } from 'iarna-toml-esm';
+import JSON5 from 'json5';
+import { withDefaultOnError } from '../../utils/defaults';
+import type { UseValidationRule } from '@/composable/validation';
+
+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.',
+ },
+];
+</script>
+
+<template>
+ <format-transformer
+ input-label="Your JSON"
+ input-placeholder="Paste your JSON here..."
+ output-label="TOML from your JSON"
+ output-language="toml"
+ :input-validation-rules="rules"
+ :transformer="transformer"
+ />
+</template>