xml-utils.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { XMLElement } from '../models/evt-models';
  2. // TODO get rid of any
  3. // tslint:disable-next-line: no-any
  4. declare var window: any;
  5. export function parseXml(xmlStr: string): XMLElement {
  6. if (typeof window.DOMParser !== 'undefined') {
  7. return (new window.DOMParser()).parseFromString(xmlStr, 'text/xml');
  8. }
  9. if (typeof window.ActiveXObject !== 'undefined' &&
  10. new window.ActiveXObject('Microsoft.XMLDOM')) {
  11. const xmlDoc = new window.ActiveXObject('Microsoft.XMLDOM');
  12. xmlDoc.async = 'false';
  13. xmlDoc.loadXML(xmlStr);
  14. return xmlDoc;
  15. }
  16. throw new Error('No XML parser found');
  17. }
  18. export function replaceMultispaces(textContent: string) {
  19. return textContent.replace(/\s{2,}/g, ' ');
  20. }
  21. export function replaceNewLines(textContent: string) {
  22. return replaceMultispaces(textContent.trim().replace(/\n/g, ' '));
  23. }
  24. export function replaceNotWordChar(textContent: string) {
  25. return textContent && textContent.replace(/[\W_]/, ' ') ;
  26. }
  27. export function removeSpaces(textContent: string) {
  28. return textContent.replace(/\s/g, '');
  29. }