-
Notifications
You must be signed in to change notification settings - Fork 13
/
uuid-base62.js
139 lines (115 loc) · 2.84 KB
/
uuid-base62.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
// dependencies
var uuid = require('node-uuid');
var baseX = require('base-x');
var base62 = baseX('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
// expose node-uuid and baseX for convenience
module.exports.uuid = uuid;
module.exports.baseX = baseX;
module.exports.customBase = base62;
module.exports.length = 22;
module.exports.uuidLength = 32;
/**
* v4
*/
module.exports.v4 = function v4() {
var self = this;
var args = Array.prototype.slice.call(arguments);
if (!args[1]) {
// make sure we use a buffer to avoid getting an uuid with dashes
args[1] = new Buffer(16);
}
var id = uuid.v4.apply(self, args);
return self.encode(id);
};
/**
* v1
*/
module.exports.v1 = function v1() {
var self = this;
var args = Array.prototype.slice.call(arguments);
if (!args[1]) {
// make sure we use a buffer to avoid getting an uuid with dashes
args[1] = new Buffer(16);
}
var id = uuid.v1.apply(self, args);
return self.encode(id);
};
/**
* encode
*/
module.exports.encode = function encode(input, options) {
options = options || {};
// Make compatible with previous API.
options.encoding = typeof options === 'string' ?
options
: options.encoding || 'hex';
options.base = options.base || this.customBase;
options.length = options.length || module.exports.length;
if (typeof input === 'string') {
// remove the dashes to save some space
input = new Buffer(input.replace(/-/g, ''), options.encoding);
}
return ensureLength(options.base.encode(input), options.length);
};
/**
* decode
*/
module.exports.decode = function decode(b62Str, options) {
options = options || {};
// Make compatible with previous API.
options.encoding = typeof options === 'string' ?
options
: options.encoding || 'hex';
options.base = options.base || this.customBase;
options.length = options.length || module.exports.uuidLength;
var res = ensureLength(new Buffer(options.base.decode(b62Str)).toString(options.encoding), options.length);
// re-add the dashes so the result looks like an uuid
var resArray = res.split('');
[8, 13, 18, 23].forEach(function(idx){
resArray.splice(idx, 0, '-');
});
res = resArray.join('');
return res;
};
/**
* ensureLength
*
* @api private
*/
function ensureLength(str, maxLen){
str = str + "";
if(str.length < maxLen){
return padLeft(str, maxLen);
}
else if (str.length > maxLen){
return trimLeft(str, maxLen);
}
return str;
};
/**
* padLeft
*
* @api private
*/
function padLeft(str, padding){
str = str + "";
var pad = "";
for(var i=str.length; i<padding; i++){
pad += '0';
}
return pad + str;
};
/**
* trimLeft
*
* @api private
*/
function trimLeft(str, maxLen){
str = str + "";
var trim = 0;
while(str[trim] === '0' && (str.length - trim) > maxLen){
trim++;
}
return str.slice(trim);
};