2023-06-18 17:57:18 +02:00
|
|
|
<script setup lang="ts">
|
|
|
|
import JSON5 from 'json5';
|
|
|
|
import { convertArrayToCsv } from './json-to-csv.service';
|
|
|
|
import type { UseValidationRule } from '@/composable/validation';
|
|
|
|
import { withDefaultOnError } from '@/utils/defaults';
|
|
|
|
|
|
|
|
function transformer(value: string) {
|
|
|
|
return withDefaultOnError(() => {
|
|
|
|
if (value === '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return convertArrayToCsv({ array: JSON5.parse(value) });
|
|
|
|
}, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
const rules: UseValidationRule<string>[] = [
|
|
|
|
{
|
|
|
|
validator: (v: string) => v === '' || JSON5.parse(v),
|
|
|
|
message: 'Provided JSON is not valid.',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<format-transformer
|
2023-06-23 15:23:47 +02:00
|
|
|
input-label="Your raw JSON"
|
|
|
|
input-placeholder="Paste your raw JSON here..."
|
2023-06-18 17:57:18 +02:00
|
|
|
output-label="CSV version of your JSON"
|
|
|
|
:input-validation-rules="rules"
|
|
|
|
:transformer="transformer"
|
|
|
|
/>
|
|
|
|
</template>
|