Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Jul 22, 2018
0 parents commit 044ce21
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
.DS_Store
29 changes: 29 additions & 0 deletions README.md
@@ -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
59 changes: 59 additions & 0 deletions lib/index.js
@@ -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;
}
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions package.json
@@ -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"
}
}

0 comments on commit 044ce21

Please sign in to comment.