2018-03-26 08:53:46 +00:00
|
|
|
Array.prototype.equals = function( array ) {
|
|
|
|
return this.length == array.length &&
|
|
|
|
this.every( function(item,i) { return item == array[i] } );
|
|
|
|
}
|
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
function slug(str) {
|
|
|
|
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
|
|
|
|
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
|
|
|
|
}).replace(/\s+/g, '');
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
function getRandomString() {
|
|
|
|
return Math.random().toString(36).substr(3);
|
|
|
|
}
|
2018-02-01 11:07:36 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
async function sleep(seconds) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, seconds * 1000);
|
|
|
|
});
|
|
|
|
}
|
2018-02-07 13:23:52 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
function _(text, args) {
|
|
|
|
// should return translated text
|
|
|
|
return stringReplace(text, args);
|
|
|
|
}
|
2018-02-07 13:23:52 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
function stringReplace(str, args) {
|
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
args = [args];
|
|
|
|
}
|
2018-02-07 13:23:52 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
if(str==undefined) return str;
|
2018-02-07 13:23:52 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
let unkeyed_index = 0;
|
|
|
|
return str.replace(/\{(\w*)\}/g, (match, key) => {
|
|
|
|
if (key === '') {
|
|
|
|
key = unkeyed_index;
|
|
|
|
unkeyed_index++
|
|
|
|
}
|
|
|
|
if (key == +key) {
|
|
|
|
return args[key] !== undefined
|
|
|
|
? args[key]
|
|
|
|
: match;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-03-26 12:18:07 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
function getQueryString(params) {
|
|
|
|
if (!params) return '';
|
|
|
|
let parts = [];
|
|
|
|
for (let key in params) {
|
|
|
|
if (key!=null && params[key]!=null) {
|
|
|
|
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
|
2018-03-26 12:56:21 +00:00
|
|
|
}
|
2018-06-24 05:52:23 +00:00
|
|
|
}
|
|
|
|
return parts.join('&');
|
|
|
|
}
|
2018-03-26 12:18:07 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
function asyncHandler(fn) {
|
|
|
|
return (req, res, next) => Promise.resolve(fn(req, res, next))
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
// handle error
|
|
|
|
res.status(err.status_code || 500).send({error: err.message});
|
|
|
|
});
|
|
|
|
}
|
2018-01-25 10:04:48 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
/**
|
|
|
|
* Returns array from 0 to n - 1
|
|
|
|
* @param {Number} n
|
|
|
|
*/
|
|
|
|
function range(n) {
|
|
|
|
return Array.from(Array(4)).map((d, i) => i)
|
|
|
|
}
|
2018-04-19 14:31:17 +00:00
|
|
|
|
2018-06-24 05:52:23 +00:00
|
|
|
function unique(list, key = it => it) {
|
|
|
|
var seen = {};
|
|
|
|
return list.filter(item => {
|
|
|
|
var k = key(item);
|
|
|
|
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
_,
|
|
|
|
slug,
|
|
|
|
getRandomString,
|
|
|
|
sleep,
|
|
|
|
stringReplace,
|
|
|
|
getQueryString,
|
|
|
|
asyncHandler,
|
|
|
|
range,
|
|
|
|
unique,
|
|
|
|
}
|