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

19 lines
446 B
TypeScript

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