mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-08 23:25:03 -04:00
refactor(xml-formatter): improved unit tests
This commit is contained in:
parent
18e2010b21
commit
ddd191e35d
3 changed files with 61 additions and 33 deletions
|
@ -1,18 +1,27 @@
|
||||||
import { describe, expect, it } from 'vitest';
|
import { describe, expect, it } from 'vitest';
|
||||||
import { formatXml } from './xml-formatter.service';
|
import { formatXml } from './xml-formatter.service';
|
||||||
|
|
||||||
const options = {
|
describe('xml-formatter service', () => {
|
||||||
indentation: ' ',
|
describe('formatXml', () => {
|
||||||
collapseContent: true,
|
it('converts XML into a human readable format', () => {
|
||||||
lineSeparator: '\n',
|
const initString = '<hello><world>foo</world><world>bar</world></hello>';
|
||||||
};
|
|
||||||
const initString = '<hello><world>foo</world><world>bar</world></hello>';
|
expect(formatXml(initString)).toMatchInlineSnapshot(`
|
||||||
const endString = `<hello>
|
"<hello>
|
||||||
<world>foo</world>
|
<world>
|
||||||
<world>bar</world>
|
foo
|
||||||
</hello>`;
|
</world>
|
||||||
describe('xml-formatter', () => {
|
<world>
|
||||||
it('Should generate same string', () => {
|
bar
|
||||||
expect(formatXml(initString, options)).toEqual(endString);
|
</world>
|
||||||
|
</hello>"
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns an empty string if the input is not valid XML', () => {
|
||||||
|
const initString = 'hello world';
|
||||||
|
|
||||||
|
expect(formatXml(initString)).toEqual('');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,28 @@
|
||||||
import xmlFormat, { type XMLFormatterOptions } from 'xml-formatter';
|
import xmlFormat, { type XMLFormatterOptions } from 'xml-formatter';
|
||||||
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
|
|
||||||
export function formatXml(value: string, options?: XMLFormatterOptions): string {
|
export { formatXml, isValidXML };
|
||||||
return xmlFormat(value, options) || '';
|
|
||||||
|
function cleanRawXml(rawXml: string): string {
|
||||||
|
return rawXml.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatXml(rawXml: string, options?: XMLFormatterOptions): string {
|
||||||
|
return withDefaultOnError(() => xmlFormat(cleanRawXml(rawXml), options) ?? '', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidXML(rawXml: string): boolean {
|
||||||
|
const cleanedRawXml = cleanRawXml(rawXml);
|
||||||
|
|
||||||
|
if (cleanedRawXml === '') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
xmlFormat(cleanedRawXml);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +1,37 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { formatXml } from './xml-formatter.service';
|
import { formatXml, isValidXML } from './xml-formatter.service';
|
||||||
import type { UseValidationRule } from '@/composable/validation';
|
import type { UseValidationRule } from '@/composable/validation';
|
||||||
import { isNotThrowing } from '@/utils/boolean';
|
|
||||||
import { withDefaultOnError } from '@/utils/defaults';
|
|
||||||
|
|
||||||
const defaultValue = '<hello><world>foo</world><world>bar</world></hello>';
|
const defaultValue = '<hello><world>foo</world><world>bar</world></hello>';
|
||||||
const indentSize = useStorage('xml-formatter:indent-size', 2);
|
const indentSize = useStorage('xml-formatter:indent-size', 2);
|
||||||
const collapseContent = useStorage('xml-formatter:collapse-content', true);
|
const collapseContent = useStorage('xml-formatter:collapse-content', true);
|
||||||
|
|
||||||
function transformer(value: string) {
|
function transformer(value: string) {
|
||||||
return withDefaultOnError(() => {
|
return formatXml(value, {
|
||||||
const def = {
|
indentation: ' '.repeat(indentSize.value),
|
||||||
indentation: ' '.repeat(indentSize.value),
|
collapseContent: collapseContent.value,
|
||||||
collapseContent: collapseContent.value,
|
lineSeparator: '\n',
|
||||||
lineSeparator: '\n\r',
|
});
|
||||||
};
|
|
||||||
return formatXml(value, def);
|
|
||||||
}, '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const rules: UseValidationRule<string>[] = [
|
const rules: UseValidationRule<string>[] = [
|
||||||
{
|
{
|
||||||
validator: (value: string) => isNotThrowing(() => formatXml(value)),
|
validator: isValidXML,
|
||||||
message: 'Provided XML is not valid.',
|
message: 'Provided XML is not valid.',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div style="flex: 0 0 100%">
|
<div important:flex-full important:flex-shrink-0 important:flex-grow-0>
|
||||||
<n-space style="margin: 0 auto; max-width: 600px" justify="center">
|
<div flex justify-center>
|
||||||
<n-form-item label="Collapse Content :" label-placement="left" label-width="100">
|
<n-form-item label="Collapse content:" label-placement="left">
|
||||||
<n-switch v-model:value="collapseContent" />
|
<n-switch v-model:value="collapseContent" />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
<n-form-item label="Indent size :" label-placement="left" label-width="100" :show-feedback="false">
|
<n-form-item label="Indent size:" label-placement="left" label-width="100" :show-feedback="false">
|
||||||
<n-input-number v-model:value="indentSize" min="0" max="10" style="width: 100px" />
|
<n-input-number v-model:value="indentSize" min="0" max="10" w-100px />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
</n-space>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<format-transformer
|
<format-transformer
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue