node-rigorous
Version:
Rigorous Framework
48 lines (40 loc) • 1.09 kB
JavaScript
;
var _require = require('../factory/RigorousError/index'),
RigorousError = _require.RigorousError,
errorTypes = _require.errorTypes;
function secureInput(unitTextRaw) {
var optional = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': ''',
// " = ""
"'": ''' // ' = '
};
if (!unitTextRaw) {
if (optional) {
throw new RigorousError('Undefined param input');
} else {
return null;
}
}
return unitTextRaw.replace(/[&<>"']/g, function (m) {
return map[m];
});
}
module.exports = {
escapeHtml: function escapeHtml(unitTextRaw) {
var optional = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var result = null;
if (Array.isArray(unitTextRaw)) {
result = [];
unitTextRaw.forEach(function (input) {
result.push(secureInput(input, optional));
});
} else {
result = secureInput(unitTextRaw, optional);
}
return result;
}
};