'use strict';

const re = /(\r\n)|["\'<>]/g;
const htmlEntity = {
    '&amp;': '\u0026',
    '&quot;': '\u0022',
    '&apos;': '\u0027',
    '&lt;': '\u003c',
    '&gt;': '\u003e'
};

exports.htmlDecode = function(txt) {
    txt = txt + '' || '';
    return txt.replace(/((&(([a-z][a-z0-9]*)|(#[0-9]+)|(#x[0-9a-f]+));)|["'<>&])/gi, function(s) {
        s = s || '';
        const s1 = htmlEntity[s.toLowerCase()];

        if (s1) {
            s = s1;
        }

        return s;
    });
};

exports.htmlEncode = function(str) {
    str = str + '' || '';
    return str.replace(re, function(s) {
        switch (s) {
            case '"':
                return '&quot;';
            case '\'':
                return '&apos;';
            case '<':
                return '&lt;';
            case '>':
                return '&gt;';
            default:
                return s;
        }
    });
};