diff options
author | 2023-04-10 17:34:58 +0200 | |
---|---|---|
committer | 2023-04-10 22:38:35 +0200 | |
commit | c0a89131dd919c2bb6562cb0de18b1396e12bf23 (patch) | |
tree | 42cfe5c700ff2cd5190e8585589ea4f8f697bee5 /src/tools/yaml-to-json-converter/yaml-to-json.vue | |
parent | 05f06f6a072e8421c48ebe7c2bafcbbe056163ed (diff) | |
download | it-tools-c0a89131dd919c2bb6562cb0de18b1396e12bf23.tar.gz it-tools-c0a89131dd919c2bb6562cb0de18b1396e12bf23.tar.zst it-tools-c0a89131dd919c2bb6562cb0de18b1396e12bf23.zip |
feat(new-tool): yaml and json converters
Diffstat (limited to 'src/tools/yaml-to-json-converter/yaml-to-json.vue')
-rw-r--r-- | src/tools/yaml-to-json-converter/yaml-to-json.vue | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/tools/yaml-to-json-converter/yaml-to-json.vue b/src/tools/yaml-to-json-converter/yaml-to-json.vue new file mode 100644 index 0000000..c066bdd --- /dev/null +++ b/src/tools/yaml-to-json-converter/yaml-to-json.vue @@ -0,0 +1,32 @@ +<template> + <format-transformer + input-label="Your YAML" + input-placeholder="Paste your yaml here..." + output-label="JSON from your YAML" + output-language="json" + :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 { parse as parseYaml } from 'yaml'; + +const transformer = (value: string) => + withDefaultOnError(() => { + const obj = parseYaml(value); + return obj ? JSON.stringify(obj, null, 3) : ''; + }, ''); + +const rules: UseValidationRule<string>[] = [ + { + validator: (value: string) => isNotThrowing(() => parseYaml(value)), + message: 'Provided YAML is not valid.', + }, +]; +</script> + +<style lang="less" scoped></style> |