UNPKG

node-rigorous

Version:
48 lines (40 loc) 1.09 kB
"use strict"; 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 = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&#039;', // " = &quot" "'": '&#039;' // ' = &#039 }; 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; } };