mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-21 21:44:40 +03:00
feat(yaml-to-json): add nestify options
This commit is contained in:
1 parent
3ffb8052d6
commit
e9dbf1fb0e
1 file changed
+18
-1
@@ -1,12 +1,23 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { parse as parseYaml } from 'yaml';
|
import { parse as parseYaml } from 'yaml';
|
||||||
|
import { nestifyObject } from 'nestify-anything';
|
||||||
import type { UseValidationRule } from '@/composable/validation';
|
import type { UseValidationRule } from '@/composable/validation';
|
||||||
import { isNotThrowing } from '@/utils/boolean';
|
import { isNotThrowing } from '@/utils/boolean';
|
||||||
import { withDefaultOnError } from '@/utils/defaults';
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
|
|
||||||
|
const nestify = ref(false);
|
||||||
|
|
||||||
function transformer(value: string) {
|
function transformer(value: string) {
|
||||||
return withDefaultOnError(() => {
|
return withDefaultOnError(() => {
|
||||||
const obj = parseYaml(value, { merge: true });
|
let obj = parseYaml(value, { merge: true });
|
||||||
|
if (nestify.value) {
|
||||||
|
if (obj && Array.isArray(obj)) {
|
||||||
|
obj = (obj as any[]).map(o => nestifyObject(o));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
obj = nestifyObject(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
return obj ? JSON.stringify(obj, null, 3) : '';
|
return obj ? JSON.stringify(obj, null, 3) : '';
|
||||||
}, '');
|
}, '');
|
||||||
}
|
}
|
||||||
@@ -20,6 +31,12 @@ const rules: UseValidationRule<string>[] = [
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
<n-space justify="center">
|
||||||
|
<n-form-item label="Nestify ('a.b.c' to nested objects)" label-placement="left">
|
||||||
|
<n-checkbox v-model:checked="nestify" />
|
||||||
|
</n-form-item>
|
||||||
|
</n-space>
|
||||||
|
|
||||||
<format-transformer
|
<format-transformer
|
||||||
input-label="Your YAML"
|
input-label="Your YAML"
|
||||||
input-placeholder="Paste your yaml here..."
|
input-placeholder="Paste your yaml here..."
|
||||||
|
|||||||
Reference in new issue
Block a user