From c0a89131dd919c2bb6562cb0de18b1396e12bf23 Mon Sep 17 00:00:00 2001
From: Corentin Thomasset <corentin.thomasset74@gmail.com>
Date: Mon, 10 Apr 2023 17:34:58 +0200
Subject: feat(new-tool): yaml and json converters

---
 src/tools/json-to-yaml-converter/json-to-yaml.vue | 29 +++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 src/tools/json-to-yaml-converter/json-to-yaml.vue

(limited to 'src/tools/json-to-yaml-converter/json-to-yaml.vue')

diff --git a/src/tools/json-to-yaml-converter/json-to-yaml.vue b/src/tools/json-to-yaml-converter/json-to-yaml.vue
new file mode 100644
index 0000000..f25ef37
--- /dev/null
+++ b/src/tools/json-to-yaml-converter/json-to-yaml.vue
@@ -0,0 +1,29 @@
+<template>
+  <format-transformer
+    input-label="Your JSON"
+    input-placeholder="Paste your JSON here..."
+    output-label="YAML from your JSON"
+    output-language="yaml"
+    :input-validation-rules="rules"
+    :transformer="transformer"
+  />
+</template>
+
+<script setup lang="ts">
+import type { UseValidationRule } from '@/composable/validation';
+import { isNotThrowing } from '@/utils/boolean';
+import { withDefaultOnError } from '@/utils/defaults';
+import { stringify } from 'yaml';
+import JSON5 from 'json5';
+
+const transformer = (value: string) => withDefaultOnError(() => stringify(JSON5.parse(value)), '');
+
+const rules: UseValidationRule<string>[] = [
+  {
+    validator: (value: string) => value === '' || isNotThrowing(() => stringify(JSON5.parse(value))),
+    message: 'Provided JSON is not valid.',
+  },
+];
+</script>
+
+<style lang="less" scoped></style>
-- 
cgit v1.2.3

