node-rigorous
Version:
Rigorous Framework
48 lines (33 loc) • 1.02 kB
JavaScript
const RigorousError = require('../facades/RigorousError');
const errorsMessages = require('../etc/errorsMessages');
function secureInput(unitTextRaw, optional = true) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': ''', // " = ""
"'": ''', // ' = '
};
if (!unitTextRaw) {
if (optional) {
throw new RigorousError(errorsMessages.UndefinedParameterInputError);
} 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;
},
};