nodejs-rigorous
Version:
Rigorous Framework
50 lines (32 loc) • 981 B
JavaScript
const { RigorousError, errorTypes } = require('../factory/RigorousError/index');
function secureInput(unitTextRaw, optional = true) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': ''', // " = ""
"'": ''', // ' = '
};
if (!unitTextRaw) {
if (optional) {
throw new RigorousError('Undefined param input');
} else {
return null;
}
}
return unitTextRaw.replace(/[&<>"']/g, (m) => { return map[m]; });
}
module.exports = {
escapeHtml: (unitTextRaw, optional = true) => {
let result = null;
if (Array.isArray(unitTextRaw)) {
result = [];
unitTextRaw.forEach((input) => {
result.push(secureInput(input, optional));
});
} else {
result = secureInput(unitTextRaw, optional);
}
return result;
},
};