UNPKG

@onesy/utils

Version:
29 lines (19 loc) 893 B
import he from 'he'; import Try from './try'; const textToInnerHTML = value => { var _Try; return (_Try = Try(() => { var _Try3; if (!value) return ''; // Step 1: Decode URI components repeatedly (e.g., "%20" -> " ") let decoded = value; let previous; do { var _Try2; previous = decoded; decoded = (_Try2 = Try(() => decodeURIComponent(decoded))) !== null && _Try2 !== void 0 ? _Try2 : decoded; // Stop when no more %-encoding exists } while (decoded !== previous); // Step 2: Decode HTML entities (e.g., "&" -> "&") const decodedHTMLString = he.decode(decoded); // Step 3: Escape JSON-breaking characters return (_Try3 = Try(() => JSON.parse("\"".concat(decodedHTMLString, "\"")))) !== null && _Try3 !== void 0 ? _Try3 : decodedHTMLString; })) !== null && _Try !== void 0 ? _Try : value; }; export default textToInnerHTML;