Skip to content

Commit e101bc2

Browse files
Carmine DimascioCarmine Dimascio
authored andcommitted
support canonical and relaxed modes
1 parent 241a613 commit e101bc2

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

lib/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* Copyright (c) 2018-2020 Carmine M DiMascio
44
*/
55
const { Binary } = require('mongodb');
6-
const uuidv1 = require('uuid/v1');
7-
const uuidv4 = require('uuid/v4');
6+
const { v1: uuidv1 } = require('uuid');
7+
const { v4: uuidv4 } = require('uuid');
88

99
const Err = {
1010
InvalidUUID: new Error('Invalid UUID.'),
@@ -13,8 +13,15 @@ const Err = {
1313
),
1414
UnsupportedUUIDVersion: new Error('Unsupported uuid version.'),
1515
InvalidHexString: new Error('Invalid hex string.'),
16+
InvalidMode: new Error('Invalid Mode. Exprected \'canonical\' or \'relaxed\'.'),
1617
};
18+
let mode = 'canonical';
1719
module.exports = {
20+
mode(m) {
21+
if (!['canonical', 'relaxed'].includes(m)) throw Err.InvalidMode;
22+
mode = m;
23+
return this;
24+
},
1825
v1() {
1926
return generateUUID(null, 1);
2027
},
@@ -103,5 +110,9 @@ function apply(mu) {
103110

104111
return prefix + stringify(this.buffer, delimiter) + suffix;
105112
}.bind(mu);
113+
114+
if (mode === 'relaxed') {
115+
mu.toJSON = mu.toString;
116+
}
106117
return mu;
107118
}

test/uuid-mongodb_spec.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const assert = require('assert');
2-
const uuidv1 = require('uuid/v1');
3-
const uuidv4 = require('uuid/v4');
2+
const { v1: uuidv1 } = require('uuid');
3+
const { v4: uuidv4 } = require('uuid');
44
const { Binary } = require('mongodb');
55
const MUUID = require('../lib');
66

@@ -110,6 +110,22 @@ describe('MUUID (accept and generate uuids according to spec - (see https://www.
110110
assert.equal(hasHexUpperCase(uuid), false);
111111
});
112112
});
113+
114+
describe('modes', () => {
115+
it('relaxed mode should print friendly uuid in hex', function () {
116+
const mUUID = MUUID.mode('relaxed').v1();
117+
assert.equal(isBase64(JSON.stringify(mUUID)), false);
118+
assert.equal(validate(mUUID.toString()), true);
119+
assert.equal(hasHexUpperCase(mUUID.toString()), false);
120+
});
121+
it('canonical mode should print base64 uuid ', function () {
122+
const mUUID = MUUID.mode('canonical').v1();
123+
const c = JSON.parse(JSON.stringify(mUUID));
124+
assert.equal(isBase64(c), true);
125+
assert.equal(validate(mUUID.toString()), true);
126+
assert.equal(hasHexUpperCase(mUUID.toString()), false);
127+
});
128+
});
113129
});
114130

115131
function validate(uuid, format) {
@@ -131,3 +147,9 @@ function validate(uuid, format) {
131147
);
132148
}
133149
}
150+
151+
function isBase64(s) {
152+
const bc = /[A-Za-z0-9+/=]/.test(s);
153+
const lc = /.*=$/.test(s); // make sure it ends with '='
154+
return bc && lc;
155+
}

0 commit comments

Comments
 (0)