aboutsummaryrefslogtreecommitdiff
path: root/src/tools/xml-formatter/xml-formatter.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/xml-formatter/xml-formatter.service.ts')
-rw-r--r--src/tools/xml-formatter/xml-formatter.service.ts28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/tools/xml-formatter/xml-formatter.service.ts b/src/tools/xml-formatter/xml-formatter.service.ts
new file mode 100644
index 0000000..3441f0b
--- /dev/null
+++ b/src/tools/xml-formatter/xml-formatter.service.ts
@@ -0,0 +1,28 @@
+import xmlFormat, { type XMLFormatterOptions } from 'xml-formatter';
+import { withDefaultOnError } from '@/utils/defaults';
+
+export { formatXml, isValidXML };
+
+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;
+ }
+}