-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper.js
32 lines (27 loc) · 823 Bytes
/
helper.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
'use strict';
var Responses = require('./responses/model');
(function(helper){
helper.choose = function(array){
var index = Math.floor((Math.random() * array.length));
return array[index];
};
helper.shorten = function(string){
return string.substring(0, 475);
};
helper.response = function(key, callback) {
Responses.findOne({ key: key }).exec(function(err, result){
callback(err, helper.choose(result.response));
});
};
if(!String.format) {
String.format = function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
})(module.exports);