export function nullOrEmpty(s) {
return s == null || typeof s !== 'string' || s.length === 0;
}
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().indexOf(key.toLowerCase()) >= 0;
}
return s.indexOf(key) >= 0;
}
export function endsWith(s, suffix) {
if (nullOrEmpty(s) || nullOrEmpty(suffix)) {
return false;
}
return s.indexOf(suffix) === s.length - suffix.length;
}
export function padStart(s, num, char) {
if (nullOrEmpty(s) || isNaN(num) || num <= s.length) {
return s;
}
return (char ?? ' ').repeat(num - s.length);
}
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?.length > 0) {
const rs = [];
for (let t of r) {
t = t.replace(/["'\r\n ]/g, '');
if (rs.indexOf(t) < 0) {
rs.push(t);
}
}
for (let r of rs) {
msg = msg.replaceAll(r, '');
}
}
return msg;
}
export function escapeHtml(text) {
if (text == null) {
return '';
}
return String(text)
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('\r\n', '
')
.replaceAll('\n', '
')
.replaceAll(' ', ' ');
}
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(/([0-9a-fA-F]{2,6});/g, (_, h) => String.fromCodePoint(parseInt(h, 16)));
}