/** * @param {string | null} [s] * @returns {s is null | undefined | ""} */ export function nullOrEmpty(s) { return s == null || typeof s !== 'string' || s.length === 0; } /** * @param {string} s * @param {string} key * @param {boolean} [ignoreCase] * @returns {boolean} */ export function contains(s, key, ignoreCase) { if (nullOrEmpty(s) || key == null) { return false; } if (typeof key !== 'string') { key = String(key); } if (ignoreCase) { return s.toLowerCase().includes(key.toLowerCase()); } return s.includes(key); } /** * @param {string} s * @param {string} suffix * @returns {boolean} */ export function endsWith(s, suffix) { if (nullOrEmpty(s) || nullOrEmpty(suffix)) { return false; } return s.endsWith(suffix); // === s.length - suffix.length; } /** * @param {string} s * @param {number} num * @param {string} char * @returns {string} */ export function padStart(s, num, char) { if (nullOrEmpty(s) || isNaN(num) || num <= s.length) { return s; } return (char ?? ' ').repeat(num - s.length); } /** * @param {string} msg * @returns {string} */ export function formatUrl(msg) { //const urlReg = /(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/ig; //const urlArrray = str.match(urlReg); const p = /(http|ftp|https):\/\/.+?(\s|\r\n|\r|\n|\"|\'|\*|$)/g; const r = msg.match(p); msg = escapeHtml(msg); if (r != null && r.length > 0) { const rs = []; for (let t of r) { t = t.replace(/["'\r\n ]/g, ''); if (rs.indexOf(t) < 0) { rs.push(t); } } const win = typeof window !== 'undefined' ? window : self; let path; if (typeof win.consts !== 'undefined') { path = win.consts.path; } else if (typeof win._network !== 'undefined') { path = win._network.root; } else if (typeof win._net !== 'undefined') { path = win._net.root; } for (let r of rs) { msg = msg.replaceAll(r, ``); } } return msg; } /** * @param {string} text * @returns {string} */ export function escapeHtml(text) { if (text == null) { return ''; } return String(text) .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('\r\n', '
') .replaceAll('\n', '
') .replaceAll(' ', ' '); } /** * @param {string} text * @returns {string} */ export function escapeEmoji(text) { if (text == null) { return ''; } if (typeof text !== 'string') { text = String(text); } return text .replace(/(=[A-Fa-f0-9]{2}){4}/g, s => decodeURIComponent(s.replaceAll('=', '%'))) .replace(/&#x([0-9a-fA-F]{2,6});/g, (_, h) => String.fromCodePoint(parseInt(h, 16))); }