class StringUtil {
  replaceXML(str) {
    if (str != null) {
      while (str.indexOf('"') != -1) {
        str = str.replace(/\"/g, '@quot;');
      }
      return str;
    }
    return '';
  }
}

StringUtil.encodeHTML = (t) => {
  let text = t;
  if (text != null) {
    text = text.replace(/#/g, '%23');
    text = text.replace(/&/g, '&amp;');
    text = text.replace(/"/g, '@quot;');
    text = text.replace(/</g, '@lt;');
    text = text.replace(/>/g, '@gt;');
    text = text.replace(/'/g, '@#146;');
    text = text.replace(/ /g, '@nbsp;');
    text = text.replace(/\r/g, '&#10;');
    text = text.replace(/\n/g, '&#13;');
  }
  return text;
};

StringUtil.dencodeHTML = (t) => {
  let text = t;
  if (text != null) {
    text = text.replace(/&amp;/g, '&');
    text = text.replace(/@amp;/g, '&');
    text = text.replace(/&quot;/g, '"');
    text = text.replace(/@quot;/g, '"');
    text = text.replace(/&lt;/g, '<');
    text = text.replace(/@lt;/g, '<');
    text = text.replace(/&gt;/g, '>');
    text = text.replace(/@gt;/g, '>');
    text = text.replace(/&#146;/g, "'");
    text = text.replace(/@#146;/g, "'");
    text = text.replace(/&nbsp;/g, ' ');
    text = text.replace(/@nbsp;/g, ' ');
    text = text.replace(/&#10;/g, '\r');
    text = text.replace(/&#13;/g, '\n');
    text = text.replace(/&#9;/g, ' ');
    text = text.replace(/%23/g, '#');

    text = text.replace('\n\r', '\n');
    text = text.replace('\r\n', '\n');
  }
  return text;
};

export default StringUtil;
