mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
feat(new tools): YAML/JSON Flattener/Unflattener
This commit is contained in:
1 parent
1d02e32486
commit
521d0bf638
4 files changed
+142
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import { Braces } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'JSON Flattener/Unflattener',
|
||||
path: '/json-flatten-nestify',
|
||||
description: 'Flatten or nestify/unflatten JSON content (ie, {a:{b:1}} vs {"a.b":1}',
|
||||
keywords: ['json', 'flatten', 'nestify', 'unflatten'],
|
||||
component: () => import('./json-flatten-nestify.vue'),
|
||||
icon: Braces,
|
||||
createdAt: new Date('2025-07-19'),
|
||||
category: 'JSON',
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
<script setup lang="ts">
|
||||
import JSON5 from 'json5';
|
||||
import { nestifyObject } from 'nestify-anything';
|
||||
import { flatten } from 'flatten-anything';
|
||||
import type { UseValidationRule } from '@/composable/validation';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
|
||||
const mode = ref<'flatten' | 'nestify'>('flatten');
|
||||
const defaultValue = '{a: {b: 2}}';
|
||||
function transformer(value: string) {
|
||||
return withDefaultOnError(
|
||||
() => {
|
||||
let o = JSON.parseBigInt(value);
|
||||
if (mode.value === 'flatten') {
|
||||
o = flatten(o);
|
||||
}
|
||||
else {
|
||||
o = nestifyObject(o);
|
||||
}
|
||||
return JSON.stringify(o, null, 2);
|
||||
},
|
||||
'');
|
||||
}
|
||||
|
||||
const rules: UseValidationRule<string>[] = [
|
||||
{
|
||||
validator: (v: string) => v === '' || JSON5.parse(v),
|
||||
message: 'Provided JSON is not valid',
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NSpace justify="center">
|
||||
<n-form-item label="Mode" label-placement="left">
|
||||
<n-radio-group v-model:value="mode">
|
||||
<n-radio value="flatten">
|
||||
Flatten
|
||||
</n-radio>
|
||||
<n-radio value="nestify">
|
||||
Unflatten/Nestify
|
||||
</n-radio>
|
||||
</n-radio-group>
|
||||
</n-form-item>
|
||||
</NSpace>
|
||||
|
||||
<format-transformer
|
||||
input-label="Your JSON content:"
|
||||
:input-default="defaultValue"
|
||||
input-placeholder="Paste your JSON content here"
|
||||
output-label="Transformed JSON:"
|
||||
output-language="json"
|
||||
:input-validation-rules="rules"
|
||||
:transformer="transformer"
|
||||
download-file-name="output.json"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,14 @@
|
||||
import { AlignJustified } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'YAML Flattener/Unflattener',
|
||||
path: '/yaml-flatten-nestify',
|
||||
description: 'Flatten or nestify/unflatten YAML content (ie, a.b: 1 vs a: b: 1)',
|
||||
keywords: ['yaml', 'flatten', 'nestify', 'unflatten'],
|
||||
component: () => import('./yaml-flatten-nestify.vue'),
|
||||
icon: AlignJustified,
|
||||
createdAt: new Date('2025-07-19'),
|
||||
npmPackages: ['yaml'],
|
||||
category: 'YAML',
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
<script setup lang="ts">
|
||||
import YAML from 'yaml';
|
||||
import { nestifyObject } from 'nestify-anything';
|
||||
import { flatten } from 'flatten-anything';
|
||||
import type { UseValidationRule } from '@/composable/validation';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
|
||||
const mode = ref<'flatten' | 'nestify'>('flatten');
|
||||
const defaultValue = `a:
|
||||
b: 5`;
|
||||
function transformer(value: string) {
|
||||
return withDefaultOnError(
|
||||
() => {
|
||||
let o = YAML.parse(value, { intAsBigInt: true });
|
||||
if (mode.value === 'flatten') {
|
||||
o = flatten(o);
|
||||
}
|
||||
else {
|
||||
o = nestifyObject(o);
|
||||
}
|
||||
return YAML.stringify(o);
|
||||
},
|
||||
'');
|
||||
}
|
||||
|
||||
const rules: UseValidationRule<string>[] = [
|
||||
{
|
||||
validator: (v: string) => v === '' || YAML.parse(v),
|
||||
message: 'Provided YAML is not valid',
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NSpace justify="center">
|
||||
<n-form-item label="Mode" label-placement="left">
|
||||
<n-radio-group v-model:value="mode">
|
||||
<n-radio value="flatten">
|
||||
Flatten
|
||||
</n-radio>
|
||||
<n-radio value="nestify">
|
||||
Unflatten/Nestify
|
||||
</n-radio>
|
||||
</n-radio-group>
|
||||
</n-form-item>
|
||||
</NSpace>
|
||||
|
||||
<format-transformer
|
||||
input-label="Your YAML content:"
|
||||
:input-default="defaultValue"
|
||||
input-placeholder="Paste your YAML content here"
|
||||
output-label="Transformed YAML:"
|
||||
output-language="yaml"
|
||||
:input-validation-rules="rules"
|
||||
:transformer="transformer"
|
||||
download-file-name="output.yaml"
|
||||
/>
|
||||
</template>
|
||||
Reference in new issue
Block a user