dispatch/client/js/utils/index.js

204 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-01-17 22:33:52 +00:00
import padStart from 'lodash/padStart';
2015-12-28 23:34:32 +00:00
export { findBreakpoints, messageHeight } from './messageHeight';
export { default as linkify } from './linkify';
2015-12-28 23:34:32 +00:00
export function normalizeChannel(channel) {
if (channel.indexOf('#') !== 0) {
return channel;
}
2020-04-30 05:54:30 +00:00
return channel.split('#').join('').toLowerCase();
2015-12-28 23:34:32 +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);
}
return typeof name === 'string' && name[0] === '#';
}
2018-04-25 03:36:27 +00:00
export function stringifyTab(server, name) {
if (typeof server === 'object') {
if (server.name) {
return `${server.server};${server.name}`;
}
return server.server;
}
if (name) {
return `${server};${name}`;
}
return server;
}
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;
}
// 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) {
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;
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;
}
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;
}
for (let i = 0; i < arr.length; i++) {
if (pred(arr[i])) {
2018-04-25 03:36:27 +00:00
return i;
}
}
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;
}
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;
}