feat(new-tool): toml to json

This commit is contained in:
Corentin Thomasset 2023-06-23 21:33:54 +02:00 committed by Corentin THOMASSET
parent 9125dcf9c6
commit c7d4f112c0
8 changed files with 114 additions and 1 deletions

View file

@ -1,6 +1,7 @@
import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as tomlToJson } from './toml-to-json';
import { tool as jsonToCsv } from './json-to-csv';
import { tool as cameraRecorder } from './camera-recorder';
import { tool as listConverter } from './list-converter';
@ -80,6 +81,7 @@ export const toolsByCategory: ToolCategory[] = [
yamlToJson,
jsonToYaml,
listConverter,
tomlToJson,
],
},
{

View file

@ -0,0 +1,13 @@
import { defineTool } from '../tool';
import BracketIcon from '~icons/mdi/code-brackets';
export const tool = defineTool({
name: 'TOML to JSON',
path: '/toml-to-json',
description: 'Parse and convert TOML to JSON.',
keywords: ['toml', 'json', 'convert', 'online', 'transform', 'parser'],
component: () => import('./toml-to-json.vue'),
icon: BracketIcon,
createdAt: new Date('2023-06-23'),
});

View file

@ -0,0 +1,40 @@
import { expect, test } from '@playwright/test';
test.describe('Tool - TOML to JSON', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/toml-to-json');
});
test('Has correct title', async ({ page }) => {
await expect(page).toHaveTitle('TOML to JSON - IT Tools');
});
test('TOML is parsed and outputs clean JSON', async ({ page }) => {
await page.getByTestId('input').fill(`
foo = "bar"
# This is a comment
[list]
name = "item"
[list.another]
key = "value"
`.trim());
const generatedJson = await page.getByTestId('area-content').innerText();
expect(generatedJson.trim()).toEqual(
`
{
"foo": "bar",
"list": {
"name": "item",
"another": {
"key": "value"
}
}
}
`.trim(),
);
});
});

View file

@ -0,0 +1,26 @@
<script setup lang="ts">
import { parse as parseToml } from 'iarna-toml-esm';
import { withDefaultOnError } from '../../utils/defaults';
import { isValidToml } from './toml.services';
import type { UseValidationRule } from '@/composable/validation';
const transformer = (value: string) => value === '' ? '' : withDefaultOnError(() => JSON.stringify(parseToml(value), null, 3), '');
const rules: UseValidationRule<string>[] = [
{
validator: isValidToml,
message: 'Provided TOML is not valid.',
},
];
</script>
<template>
<format-transformer
input-label="Your TOML"
input-placeholder="Paste your TOML here..."
output-label="JSON from your TOML"
output-language="json"
:input-validation-rules="rules"
:transformer="transformer"
/>
</template>

View file

@ -0,0 +1,8 @@
import { parse as parseToml } from 'iarna-toml-esm';
import { isNotThrowing } from '../../utils/boolean';
export { isValidToml };
function isValidToml(toml: string): boolean {
return isNotThrowing(() => parseToml(toml));
}