Files
yaak/src-web/lib/formatters.ts
2024-02-22 19:51:30 -08:00

25 lines
560 B
TypeScript

import xmlFormat from 'xml-formatter';
const INDENT = ' ';
export function tryFormatJson(text: string, pretty = true): string {
if (text === '') return text;
try {
if (pretty) return JSON.stringify(JSON.parse(text), null, INDENT);
else return JSON.stringify(JSON.parse(text));
} catch (_) {
return text;
}
}
export function tryFormatXml(text: string): string {
if (text === '') return text;
try {
return xmlFormat(text, { throwOnFailure: true, strictMode: false, indentation: INDENT });
} catch (_) {
return text;
}
}