2016-01-17 22:33:52 +00:00
|
|
|
import padStart from 'lodash/padStart';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-04-17 02:11:45 +00:00
|
|
|
export { findBreakpoints, messageHeight } from './messageHeight';
|
2018-04-05 19:13:32 +00:00
|
|
|
export { default as linkify } from './linkify';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
export function isChannel(name) {
|
|
|
|
// TODO: Handle other channel types
|
2018-04-25 03:36:27 +00:00
|
|
|
if (typeof name === 'object') {
|
|
|
|
({ name } = name);
|
|
|
|
}
|
2017-05-28 05:20:43 +00:00
|
|
|
return typeof name === 'string' && name[0] === '#';
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function stringifyTab(network, name) {
|
|
|
|
if (typeof network === 'object') {
|
|
|
|
if (network.name) {
|
|
|
|
return `${network.network};${network.name}`;
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
2020-06-15 08:58:51 +00:00
|
|
|
return network.network;
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
|
|
|
if (name) {
|
2020-06-15 08:58:51 +00:00
|
|
|
return `${network};${name}`;
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
2020-06-15 08:58:51 +00:00
|
|
|
return network;
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 03:02:48 +00:00
|
|
|
function isString(s, maxLength) {
|
|
|
|
if (!s || typeof s !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (maxLength && s.length > maxLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:54:19 +00:00
|
|
|
export function trimPrefixChar(str, char) {
|
|
|
|
if (!isString(str)) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start = 0;
|
|
|
|
while (str[start] === char) {
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start > 0) {
|
|
|
|
return str.slice(start);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-05-16 03:02:48 +00:00
|
|
|
// RFC 2812
|
|
|
|
// nickname = ( letter / special ) *( letter / digit / special / "-" )
|
|
|
|
// letter = A-Z / a-z
|
|
|
|
// digit = 0-9
|
|
|
|
// special = "[", "]", "\", "`", "_", "^", "{", "|", "}"
|
|
|
|
export function isValidNick(nick, maxLength = 30) {
|
|
|
|
if (!isString(nick, maxLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < nick.length; i++) {
|
|
|
|
const char = nick.charCodeAt(i);
|
|
|
|
if (
|
|
|
|
(i > 0 && char < 45) ||
|
|
|
|
(char > 45 && char < 48) ||
|
|
|
|
(char > 57 && char < 65) ||
|
|
|
|
char > 125
|
|
|
|
) {
|
|
|
|
return false;
|
2018-08-12 21:19:17 +00:00
|
|
|
}
|
|
|
|
if ((i === 0 && char < 65) || char > 125) {
|
2018-05-16 03:02:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// chanstring = any octet except NUL, BELL, CR, LF, " ", "," and ":"
|
|
|
|
export function isValidChannel(channel, requirePrefix = true) {
|
|
|
|
if (!isString(channel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requirePrefix && channel[0] !== '#') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < channel.length; i++) {
|
|
|
|
const char = channel.charCodeAt(i);
|
|
|
|
if (
|
|
|
|
char === 0 ||
|
|
|
|
char === 7 ||
|
|
|
|
char === 10 ||
|
|
|
|
char === 13 ||
|
|
|
|
char === 32 ||
|
|
|
|
char === 44 ||
|
|
|
|
char === 58
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// user = any octet except NUL, CR, LF, " " and "@"
|
|
|
|
export function isValidUsername(username) {
|
|
|
|
if (!isString(username)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < username.length; i++) {
|
|
|
|
const char = username.charCodeAt(i);
|
|
|
|
if (
|
|
|
|
char === 0 ||
|
|
|
|
char === 10 ||
|
|
|
|
char === 13 ||
|
|
|
|
char === 32 ||
|
|
|
|
char === 64
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-25 02:12:02 +00:00
|
|
|
export function isInt(i, min, max) {
|
2020-05-10 00:53:39 +00:00
|
|
|
if (typeof i === 'string') {
|
|
|
|
i = parseInt(i, 10);
|
|
|
|
}
|
|
|
|
|
2018-05-25 02:12:02 +00:00
|
|
|
if (i < min || i > max || Math.floor(i) !== i) {
|
2018-05-18 01:39:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
export function timestamp(date = new Date()) {
|
2016-01-17 22:33:52 +00:00
|
|
|
const h = padStart(date.getHours(), 2, '0');
|
|
|
|
const m = padStart(date.getMinutes(), 2, '0');
|
2016-02-04 02:35:50 +00:00
|
|
|
return `${h}:${m}`;
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 13:24:23 +00:00
|
|
|
const dateFmt = new Intl.DateTimeFormat(window.navigator.language);
|
|
|
|
export const formatDate = dateFmt.format;
|
|
|
|
|
2020-06-03 01:04:38 +00:00
|
|
|
export function unix(date) {
|
|
|
|
if (date) {
|
|
|
|
return Math.floor(date.getTime() / 1000);
|
|
|
|
}
|
|
|
|
return Math.floor(Date.now() / 1000);
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
|
|
|
|
export function stringWidth(str, font) {
|
|
|
|
ctx.font = font;
|
|
|
|
return ctx.measureText(str).width;
|
|
|
|
}
|
|
|
|
|
2017-03-30 02:18:25 +00:00
|
|
|
export function measureScrollBarWidth() {
|
2015-12-28 23:34:32 +00:00
|
|
|
const outer = document.createElement('div');
|
|
|
|
outer.style.visibility = 'hidden';
|
|
|
|
outer.style.width = '100px';
|
|
|
|
|
|
|
|
document.body.appendChild(outer);
|
|
|
|
|
|
|
|
const widthNoScroll = outer.offsetWidth;
|
|
|
|
outer.style.overflow = 'scroll';
|
|
|
|
|
|
|
|
const inner = document.createElement('div');
|
|
|
|
inner.style.width = '100%';
|
|
|
|
outer.appendChild(inner);
|
|
|
|
|
|
|
|
const widthWithScroll = inner.offsetWidth;
|
|
|
|
|
|
|
|
outer.parentNode.removeChild(outer);
|
|
|
|
|
|
|
|
return widthNoScroll - widthWithScroll;
|
|
|
|
}
|
2017-05-19 06:36:13 +00:00
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
export function findIndex(arr, pred) {
|
2019-01-30 03:48:37 +00:00
|
|
|
if (!Array.isArray(arr) || typeof pred !== 'function') {
|
2018-04-25 03:36:27 +00:00
|
|
|
return -1;
|
2017-06-29 05:36:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 06:36:13 +00:00
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
if (pred(arr[i])) {
|
2018-04-25 03:36:27 +00:00
|
|
|
return i;
|
2017-05-19 06:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function find(arr, pred) {
|
|
|
|
const i = findIndex(arr, pred);
|
|
|
|
if (i !== -1) {
|
|
|
|
return arr[i];
|
|
|
|
}
|
|
|
|
return null;
|
2017-05-19 06:36:13 +00:00
|
|
|
}
|
2019-01-30 03:48:37 +00:00
|
|
|
|
|
|
|
export function count(arr, pred) {
|
|
|
|
if (!Array.isArray(arr) || typeof pred !== 'function') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let c = 0;
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
if (pred(arr[i])) {
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|