UNPKG

nodejs-rigorous

Version:
50 lines (32 loc) 981 B
const { RigorousError, errorTypes } = require('../factory/RigorousError/index'); function secureInput(unitTextRaw, optional = true) { const 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, (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; }, };