2014-07-19 07:20:16 -05:00
|
|
|
|
var unidecode = require('unidecode'),
|
|
|
|
|
|
|
|
|
|
utils,
|
2014-07-14 07:29:45 -05:00
|
|
|
|
getRandomInt;
|
|
|
|
|
|
2014-07-02 09:22:18 -05:00
|
|
|
|
/**
|
|
|
|
|
* Return a random int, used by `utils.uid()`
|
|
|
|
|
*
|
|
|
|
|
* @param {Number} min
|
|
|
|
|
* @param {Number} max
|
|
|
|
|
* @return {Number}
|
|
|
|
|
* @api private
|
|
|
|
|
*/
|
2014-07-14 07:29:45 -05:00
|
|
|
|
getRandomInt = function (min, max) {
|
2014-07-02 09:22:18 -05:00
|
|
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
2014-07-14 07:29:45 -05:00
|
|
|
|
};
|
2014-07-02 09:22:18 -05:00
|
|
|
|
|
2014-07-14 07:29:45 -05:00
|
|
|
|
utils = {
|
2014-07-28 08:19:49 -05:00
|
|
|
|
/**
|
|
|
|
|
* Timespans in seconds and milliseconds for better readability
|
|
|
|
|
*/
|
2015-05-08 09:54:12 -05:00
|
|
|
|
ONE_HOUR_S: 3600,
|
|
|
|
|
ONE_DAY_S: 86400,
|
|
|
|
|
ONE_YEAR_S: 31536000,
|
|
|
|
|
ONE_HOUR_MS: 3600000,
|
|
|
|
|
ONE_DAY_MS: 86400000,
|
|
|
|
|
ONE_WEEK_MS: 604800000,
|
|
|
|
|
ONE_MONTH_MS: 2628000000,
|
|
|
|
|
ONE_YEAR_MS: 31536000000,
|
2014-07-28 08:19:49 -05:00
|
|
|
|
|
2014-07-02 09:22:18 -05:00
|
|
|
|
/**
|
|
|
|
|
* Return a unique identifier with the given `len`.
|
|
|
|
|
*
|
|
|
|
|
* utils.uid(10);
|
|
|
|
|
* // => "FDaS435D2z"
|
|
|
|
|
*
|
|
|
|
|
* @param {Number} len
|
|
|
|
|
* @return {String}
|
|
|
|
|
* @api private
|
|
|
|
|
*/
|
|
|
|
|
uid: function (len) {
|
|
|
|
|
var buf = [],
|
|
|
|
|
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
|
|
|
|
charlen = chars.length,
|
|
|
|
|
i;
|
|
|
|
|
|
|
|
|
|
for (i = 1; i < len; i = i + 1) {
|
|
|
|
|
buf.push(chars[getRandomInt(0, charlen - 1)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf.join('');
|
2014-07-19 07:20:16 -05:00
|
|
|
|
},
|
|
|
|
|
safeString: function (string) {
|
2015-01-09 22:31:47 -05:00
|
|
|
|
// Handle the £ symbol seperately, since it needs to be removed before
|
|
|
|
|
// the unicode conversion.
|
|
|
|
|
string = string.replace(/£/g, '-');
|
2014-07-19 07:20:16 -05:00
|
|
|
|
|
|
|
|
|
// Remove non ascii characters
|
|
|
|
|
string = unidecode(string);
|
|
|
|
|
|
2015-01-09 22:31:47 -05:00
|
|
|
|
// Replace URL reserved chars: `:/?#[]!$&()*+,;=` as well as `\%<>|^~£"`
|
|
|
|
|
string = string.replace(/(\s|\.|@|:|\/|\?|#|\[|\]|!|\$|&|\(|\)|\*|\+|,|;|=|\\|%|<|>|\||\^|~|"|–|—)/g, '-')
|
|
|
|
|
// Remove apostrophes
|
|
|
|
|
.replace(/'/g, '')
|
2014-07-19 07:20:16 -05:00
|
|
|
|
// Convert 2 or more dashes into a single dash
|
|
|
|
|
.replace(/-+/g, '-')
|
2015-01-09 22:31:47 -05:00
|
|
|
|
// Remove any dashes at the beginning
|
|
|
|
|
.replace(/^-/, '')
|
2014-07-19 07:20:16 -05:00
|
|
|
|
// Make the whole thing lowercase
|
|
|
|
|
.toLowerCase();
|
|
|
|
|
|
2015-01-09 22:31:47 -05:00
|
|
|
|
// Remove trailing dash if needed
|
|
|
|
|
string = string.charAt(string.length - 1) === '-' ? string.substr(0, string.length - 1) : string;
|
|
|
|
|
|
|
|
|
|
// Handle whitespace at the beginning or end.
|
|
|
|
|
string = string.trim();
|
|
|
|
|
|
2014-07-19 07:20:16 -05:00
|
|
|
|
return string;
|
2014-12-01 10:59:49 -05:00
|
|
|
|
},
|
2015-03-14 16:50:00 -05:00
|
|
|
|
// The token is encoded URL safe by replacing '+' with '-', '\' with '_' and removing '='
|
2014-12-01 10:59:49 -05:00
|
|
|
|
// NOTE: the token is not encoded using valid base64 anymore
|
|
|
|
|
encodeBase64URLsafe: function (base64String) {
|
|
|
|
|
return base64String.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
|
|
|
},
|
|
|
|
|
// Decode url safe base64 encoding and add padding ('=')
|
|
|
|
|
decodeBase64URLsafe: function (base64String) {
|
|
|
|
|
base64String = base64String.replace(/-/g, '+').replace(/_/g, '/');
|
|
|
|
|
while (base64String.length % 4) {
|
|
|
|
|
base64String += '=';
|
|
|
|
|
}
|
|
|
|
|
return base64String;
|
2014-07-02 09:22:18 -05:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-09 23:06:24 -05:00
|
|
|
|
module.exports = utils;
|