-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.js
More file actions
62 lines (52 loc) · 1.99 KB
/
Utils.js
File metadata and controls
62 lines (52 loc) · 1.99 KB
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
var request = require('request');
var utils = {};
utils.doGet = function (hostParam, pathParam, callback) {
request(hostParam + pathParam, function (error, response, body) {
//console.log("Error: " + error + ", response: " + JSON.stringify(response));
if (!error && response.statusCode == 200) {
callback(body);
} else {
console.log('[Utils.js] ERROR or statusCode NOT 200. \n'
+ 'Error: ' + JSON.stringify(error) + ", response: " + JSON.stringify(response));
}
});
}
utils.isMyGoogleFriend = function (myFriendsList, otherGoogleUserId) {
for(var i = 0; i < myFriendsList.length; i++) {
if(myFriendsList[i].id == otherGoogleUserId) {
return true;
} else if(i == myFriendsList.length - 1) {
return false;
}
}
}
// http://stackoverflow.com/questions/10645994/node-js-how-to-format-a-date-string-in-utc
utils.dateFormat = function (date, fstr, utc) {
utc = utc ? 'getUTC' : 'get';
return fstr.replace (/%[YmdHMS]/g, function (m) {
switch (m) {
case '%Y': return date[utc + 'FullYear'] (); // no leading zeros required
case '%m': m = 1 + date[utc + 'Month'] (); break;
case '%d': m = date[utc + 'Date'] (); break;
case '%H': m = date[utc + 'Hours'] (); break;
case '%M': m = date[utc + 'Minutes'] (); break;
case '%S': m = date[utc + 'Seconds'] (); break;
default: return m.slice (1); // unknown code, remove %
}
// add leading zero if required
return ('0' + m).slice (-2);
});
}
// https://gist.github.com/takien/4077195
utils.getIdOfYoutubeVideo = function (videoUrl) {
var id = '';
videoUrl = videoUrl.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
if(videoUrl[2] !== undefined) {
id = videoUrl[2].split(/[^0-9a-z_\-]/i);
id = id[0];
} else {
id = videoUrl;
}
return id
}
module.exports = utils;