/
githubmirror
/
preact
Обзор
Документация
Войти
/
githubmirror
/
preact
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
10.29.5
jsx-runtime/src/utils.js
36 строк
815 B
Marvin Hagemeister
code golf
07 ноя 2023, 17:19
07 ноя 2023, 17:19
975779a
Код
Авторство
О чём код?
const ENCODED_ENTITIES = /["&<]/; /** @param {string} str */ export function encodeEntities(str) { // Skip all work for strings with no entities needing encoding: if (str.length === 0 || ENCODED_ENTITIES.test(str) === false) return str; let last = 0, i = 0, out = '', ch = ''; // Seek forward in str until the next entity char: for (; i < str.length; i++) { switch (str.charCodeAt(i)) { case 34: ch = '"'; break; case 38: ch = '&'; break; case 60: ch = '<'; break; default: continue; } // Append skipped/buffered characters and the encoded entity: if (i !== last) out += str.slice(last, i); out += ch; // Start the next seek/buffer after the entity's offset: last = i + 1; } if (i !== last) out += str.slice(last, i); return out; }