65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
function nullOrEmpty(s) {
|
|
return s == null || typeof s !== 'string' || s.length === 0;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
function endsWith(s, suffix) {
|
|
if (nullOrEmpty(s) || nullOrEmpty(suffix)) {
|
|
return false;
|
|
}
|
|
return s.indexOf(suffix) === s.length - suffix.length;
|
|
}
|
|
|
|
function padStart(s, num, char) {
|
|
if (nullOrEmpty(s) || isNaN(num) || num <= s.length) {
|
|
return s;
|
|
}
|
|
return (char ?? ' ').repeat(num - s.length);
|
|
}
|
|
|
|
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 = htmlencode(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, '<a target="_blank" href="' + r + '"><svg><use xlink:href="' + ((typeof consts !== 'undefined' && consts.path) ?? '') + 'fonts/fa-regular.svg#link"></use></svg></a>');
|
|
}
|
|
}
|
|
|
|
return msg
|
|
.replaceAll('\r\n', '<br/>')
|
|
.replaceAll('\n', '<br/>')
|
|
.replaceAll(' ', ' ');
|
|
}
|
|
|
|
export {
|
|
nullOrEmpty,
|
|
contains,
|
|
endsWith,
|
|
padStart,
|
|
formatUrl
|
|
} |