Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carmine DiMascio
committed
Jul 22, 2018
0 parents
commit 044ce21
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# uuid-mongodb | ||
|
||
Generates and parses [BSON UUIDs](https://docs.mongodb.com/manual/reference/method/UUID/) for use with MongoDB. | ||
|
||
Inspired by [@srcagency's](https://github.com/srcagency) [mongo-uuid](https://github.com/srcagency/mongo-uuid) | ||
|
||
## Usage | ||
|
||
``` | ||
# Create a v1 binary UUID | ||
const mUUID1 = MUUID.v1(); | ||
# Create a v4 binary UUID | ||
const mUUID4 = MUUID.v4(); | ||
# Print a string representation of a binary UUID | ||
mUUID1.toString() | ||
# Create a binary UUID from a valid uuid string | ||
const mUUID2 = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459') | ||
``` | ||
|
||
## Notes | ||
|
||
Currently support UUID v1 and v4 | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { Binary } = require('mongodb'); | ||
import * as uuidv1 from 'uuid/v1'; | ||
import * as uuidv4 from 'uuid/v4'; | ||
|
||
export const MUUID = { | ||
v1() { | ||
return generateUUID(null, 1); | ||
}, | ||
|
||
v4() { | ||
return generateUUID(null, 4); | ||
}, | ||
|
||
from(uuid) { | ||
return generateUUID(uuid); | ||
}, | ||
}; | ||
|
||
function generateUUID(uuid = undefined, v = 1) { | ||
if (v <= 0 || v > 4 || v === 2 || v === 3) | ||
throw Error('unsupported uuid version'); | ||
|
||
let uuidv = uuidv1; | ||
if (v === 4) uuidv = uuidv4; | ||
|
||
let muuid; | ||
if (uuid) { | ||
const normalized = normalize(uuid); | ||
if (normalized === false) throw new Error('Invalid hex string'); | ||
muuid = new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID); | ||
} else { | ||
const uuid = uuidv(null, new Buffer(16)); | ||
muuid = Binary(uuid, Binary.SUBTYPE_UUID); | ||
} | ||
|
||
muuid.toString = function() { | ||
const buffer = this.buffer; | ||
|
||
return [ | ||
buffer.toString('hex', 0, 4), | ||
buffer.toString('hex', 4, 6), | ||
buffer.toString('hex', 6, 8), | ||
buffer.toString('hex', 8, 10), | ||
buffer.toString('hex', 10, 16), | ||
].join('-'); | ||
}.bind(muuid); | ||
|
||
return muuid; | ||
} | ||
|
||
function normalize(string) { | ||
if (typeof string !== 'string') return false; | ||
|
||
const stripped = string.replace(/-/g, ''); | ||
|
||
if (stripped.length !== 32 || !stripped.match(/^[a-fA-F0-9]+$/)) return false; | ||
|
||
return stripped; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "uuid-mongodb", | ||
"version": "0.8.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/cdimascio/uuid-mongodb.git" | ||
}, | ||
"keywords": [ | ||
"uuid", | ||
"mongodb" | ||
], | ||
"author": "Carmine DiMascio <cdimascio@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/cdimascio/uuid-mongodb/issues" | ||
}, | ||
"homepage": "https://github.com/cdimascio/uuid-mongodb#readme", | ||
"dependencies": { | ||
"uuid": "^3.3.2" | ||
}, | ||
"peerDependencies": { | ||
"mongodb": "^3.1.1" | ||
} | ||
} |