-
Notifications
You must be signed in to change notification settings - Fork 13
/
uuid-base62.test.js
115 lines (92 loc) · 4.01 KB
/
uuid-base62.test.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
var assert = require('assert');
var uuidBase62 = require('../uuid-base62');
describe('uuid-base62', function () {
describe('v4', function () {
it('should generate a unique id without any params', function () {
var res = uuidBase62.v4();
assert(res);
assert.equal(typeof res, 'string');
assert.equal(res.length, 22);
});
it('should convert id back to uuid format', function () {
var uuid = uuidBase62.v4();
assert(uuid);
var res = uuidBase62.decode(uuid);
assert.equal(typeof res, 'string');
assert.equal(res.length, 36);
var parts = res.split('-');
assert.equal(parts.length, 5);
parts.forEach(function (group) {
var num = parseInt(group, 16);
assert(num > 0);
});
});
it('should generate same encoded uuid from different formats', function () {
var uuid = 'de305d54-75b4-431b-adb2-eb6b9e546014';
var uuidB = new Buffer([0xde, 0x30, 0x5d, 0x54, 0x75, 0xb4, 0x43, 0x1b, 0xad, 0xb2, 0xeb, 0x6b, 0x9e, 0x54, 0x60, 0x14]);
var res = uuidBase62.encode(uuid);
var resB = uuidBase62.encode(uuidB);
assert.equal(resB, res);
});
it('decoded UUID should match original UUID', function () {
var uuid = 'de305d54-75b4-431b-adb2-eb6b9e546014';
var uuidB62 = uuidBase62.encode(uuid);
var res = uuidBase62.decode(uuidB62);
assert.equal(res, uuid);
});
});
describe('v1', function () {
it('should generate a unique id without any params', function () {
var res = uuidBase62.v1();
assert(res);
assert.equal(typeof res, 'string');
assert.equal(res.length, 22);
});
});
describe('encode / decode', function () {
var fixtures = {
'0000000000000000000000': '00000000-0000-0000-0000-000000000000',
'0cBaidlJ84Ggc5JA7IYCgv': '06ad547f-fe02-477b-9473-f7977e4d5e17',
'4vqyd6OoARXqj9nRUNhtLQ': '941532a0-6be1-443a-a9d5-d57bdf180a52',
'5FY8KwTsQaUJ2KzHJGetfE': 'ba86b8f0-6fdf-4944-87a0-8a491a19490e',
'7N42dgm5tFLK9N8MT7fHC7': 'ffffffff-ffff-ffff-ffff-ffffffffffff'
};
Object.keys(fixtures).forEach(function(key){
it('should properly encode ' + fixtures[key], function () {
assert.equal(uuidBase62.encode(fixtures[key]), key);
});
it('should properly decode ' + key, function () {
assert.equal(uuidBase62.decode(key), fixtures[key]);
});
});
it('should properly encode/decode with a different encoding', function () {
assert.equal(uuidBase62.encode('06ad547f-fe02-477b-9473-f7977e4d5e17', {encoding: 'ascii'}), 'bqP6JWAS4t1lWczmmHbPIhwMSO27BW1qKdDPx2mTN5l');
assert.equal(uuidBase62.decode('bqP6JWAS4t1lWczmmHbPIhwMSO27BW1qKdDPx2mTN5l', {encoding: 'ascii'}), '06ad547f-fe02-477b-9473-f7977e4d5e17');
});
});
describe('other bases', function () {
it('should accept a custom base as an option', function () {
var customBase = new uuidBase62.baseX("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_");
var uuid = '72be7291-fbf6-400f-87c4-455e23d01cd5';
var uuidB64 = uuidBase62.encode(uuid, {base: customBase});
assert.equal(uuidB64, '1OLDah-_p03Uv4hlUzQ1Pl');
var res = uuidBase62.decode(uuidB64, {base: customBase});
assert.equal(res, uuid);
});
it('should generate a unique id without any params in base64', function () {
uuidBase62.customBase = new uuidBase62.baseX("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_");
var res = uuidBase62.v4();
assert(res);
assert.equal(typeof res, 'string');
assert.equal(res.length, 22);
});
it('should encode and decode a uuid in Base64', function () {
uuidBase62.customBase = new uuidBase62.baseX("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_");
var uuid = '72be7291-fbf6-400f-87c4-455e23d01cd5';
var uuidB64 = uuidBase62.encode(uuid);
assert.equal(uuidB64, '1OLDah-_p03Uv4hlUzQ1Pl');
var res = uuidBase62.decode(uuidB64);
assert.equal(res, uuid);
});
});
});