-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
72 lines (65 loc) · 2.2 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Allows this file to be used serverside as well as clientside.
* https://stackoverflow.com/questions/3225251/how-can-i-share-code-between-node-js-and-the-browser
*/
(function(exports) {
// Add string format function if nonexistent
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
// Add trim if nonexistent
if (!('trim' in String.prototype)) {
String.prototype.trim= function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};
}
// Reads a message, and passes the param only to be handled by action
exports.parseCommand = function(command, message) {
var text = message.text;
if (text.toLowerCase().startsWith(command.text.toLowerCase() + " ")) {
var param = text.substring(command.text.length + 1);
command.action(param, message.sender);
return true;
}
return false;
}
exports.getRandomColor = function() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Regex obtains youtube video ID
exports.getVideoID = function(url){
var regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[2].length == 11) {
return match[2];
} else {
return null;
}
}
// Get everyone connected
exports.getListOfSockets = function(io) {
let sockets = [];
try {
let socketObj = io.sockets.sockets;
for (let id of Object.keys(socketObj)) {
sockets.push(io.sockets.connected[id]);
}
} catch(e) {
}
return sockets;
}
// Part of allowing serverside and clientside use
}(typeof exports === 'undefined'? this.util = {} : exports));