-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.js
More file actions
87 lines (75 loc) · 1.96 KB
/
tools.js
File metadata and controls
87 lines (75 loc) · 1.96 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var crypto = require('crypto');
/**
* Randomly generates a string of the specified length.
*
* @param {Integer} len How long a string to generate.
* @param {String} [pool='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'] Characters to draw the random string from.
* @returns {String} The uid.
*/
exports.uid = function uid(len, pool) {
var S = pool || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
str = '';
for( var i=0; i<len; i++ ) {
str += S[randInt(0, S.length-1)];
}
return str;
};
exports.randInt = randInt;
function randInt(min, max) {
try {
var int = crypto.randomBytes(4).readUInt32LE(0);
return min + (int % (max-min+1));
}
catch(e) {
return Math.floor(min + (Math.random()*(min-max+1)));
}
}
exports.mapToApp = function mapToApp(routes, app) {
var method, route;
for( method in routes ) {
for( route in routes[method] ) {
app[method](route, routes[method][route]);
}
}
};
exports.mapToObject = function mapToObject(routes, object) {
var method, route;
for( method in routes ) {
for( route in routes[method] ) {
object[method][route] = routes[method][route];
}
}
}
/**
* Parse the given cookie string into an object.
*
* @param {String} str
* @return {Object}
* @api private
*/
exports.parseCookie = function parseCookie(str){
var obj = {}
, pairs = str.split(/[;,] */);
for (var i = 0, len = pairs.length; i < len; ++i) {
var pair = pairs[i]
, eqlIndex = pair.indexOf('=')
, key = pair.substr(0, eqlIndex).trim()
, val = pair.substr(++eqlIndex, pair.length).trim();
// quoted values
if ('"' == val[0]) val = val.slice(1, -1);
// only assign once
if (undefined == obj[key]) {
val = val.replace(/\+/g, ' ');
try {
obj[key] = decodeURIComponent(val);
} catch (err) {
if (err instanceof URIError) {
obj[key] = val;
} else {
throw err;
}
}
}
}
return obj;
};