const Util = (function(){
  function r(e, r, n) {
    return r || n ? String.fromCharCode(r || n) : u[e] || e
  }
  function n(e) {
    return p[e]
  }
  var t = /&quot;|&lt;|&gt;|&amp;|&nbsp;|&apos;|&#(\d+);|&#(\d+)/g
    , o = /['<> "&]/g
    , u = {
      "&quot;": '"',
      "&lt;": "<",
      "&gt;": ">",
      "&amp;": "&",
      "&nbsp;": " "
    }
    , c = /\u00a0/g
    , a = /<br\s*\/?>/gi
    , i = /\r?\n/g
    , f = /\s/g
    , p = {}
  for (let s in u) {
    p[u[s]] = s
  }
  u["&apos;"] = "'"
  p["'"] = "&#39;"
  return {
    encode: function(e) {
      return e ? ("" + e).replace(o, n).replace(i, "<br/>").replace(f, "&nbsp;") : ""
    },
    decode: function(e) {
      return e ? ("" + e).replace(a, "\n").replace(t, r).replace(c, " ") : ""
    },
    encodeBase16: function(e) {
      if (!e)
        return e
      e += ""
      for (var r = [], n = 0, t = e.length; t > n; n++)
        r.push(e.charCodeAt(n).toString(16).toUpperCase())
      return r.join("")
    },
    encodeBase16forJSON: function(e) {
      if (!e)
        return e
      e = e.replace(/[\u4E00-\u9FBF]/gi, function(e) {
        return escape(e).replace("%u", "\\u")
      })
      for (var r = [], n = 0, t = e.length; t > n; n++)
        r.push(e.charCodeAt(n).toString(16).toUpperCase())
      return r.join("")
    },
    decodeBase16: function(e) {
      if (!e)
        return e
      e += ""
      for (var r = [], n = 0, t = e.length; t > n; n += 2)
        r.push(String.fromCharCode("0x" + e.slice(n, n + 2)))
      return r.join("")
    },
    encodeObject: function(r) {
      if (r instanceof Array)
        for (var n = 0, t = r.length; t > n; n++)
          r[n] = e.encodeObject(r[n])
      else if ("object" == typeof r)
        for (var o in r)
          r[o] = e.encodeObject(r[o])
      else if ("string" == typeof r)
        return e.encode(r)
      return r
    },
    addLoadEvent: function(func){
      if(typeof func !== 'function') return
      document.addEventListener("DOMContentLoaded", func)
    }
  }
})()

export default Util