Skip to content

Commit b52c450

Browse files
author
Tyler Garlick
committed
Initial drop
1 parent c2ba25a commit b52c450

File tree

8 files changed

+175
-2
lines changed

8 files changed

+175
-2
lines changed

.gitignore

1.96 KB
Binary file not shown.

README.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1-
simple-uuid
1+
Simple UUID
22
===========
33

4-
Simply generates a GUID in both Node.js and the browser
4+
Simply generates a UUID in both Node.js and the browser. This code was taken from Jeff Ward (jcward.com) and http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136. I simply wanted to turn it into a bower package and an npm package for simplicity.
5+
6+
## Browser
7+
8+
**Install via Bower**
9+
10+
```
11+
bower install simple-uuid --save
12+
```
13+
14+
**In your HTML**
15+
16+
```html
17+
<script src="bower_components/simple-uuid/uuid.js"></script>
18+
```
19+
20+
**In your code**
21+
```javascript
22+
var uuid = UUID.generate();
23+
```
24+
25+
## Node.js
26+
27+
**NPM**
28+
29+
```
30+
npm install simple-uuid --save
31+
```
32+
33+
**In your code**
34+
```javascript
35+
var UUID = require('simple-uuid');
36+
var uuid = UUID.generate();
37+
```
38+

bower.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "simple-uuid",
3+
"version": "0.0.1",
4+
"homepage": "https://github.com/TylerGarlick/simple-uuid",
5+
"authors": [
6+
"Tyler Garlick <tjgarlick@gmail.com>"
7+
],
8+
"description": "Simple UUID generation",
9+
"main": "uuid.js",
10+
"moduleType": [
11+
"node"
12+
],
13+
"keywords": [
14+
"uuid",
15+
"guid"
16+
],
17+
"license": "MIT",
18+
"ignore": [
19+
"**/.*",
20+
"node_modules",
21+
"bower_components",
22+
"test",
23+
"tests"
24+
]
25+
}

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/uuid-node');

lib/uuid-node.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
exports = module.exports;
4+
5+
/**
6+
* Generates a UUID
7+
* @returns {string}
8+
*/
9+
exports.generate = function () {
10+
var lut = [];
11+
for(var i = 0; i < 256; i++) {
12+
lut[i] = (i < 16 ? '0' : '') + (i).toString(16);
13+
}
14+
15+
var d0 = Math.random() * 0xffffffff | 0;
16+
var d1 = Math.random() * 0xffffffff | 0;
17+
var d2 = Math.random() * 0xffffffff | 0;
18+
var d3 = Math.random() * 0xffffffff | 0;
19+
20+
return lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + '-' +
21+
lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + '-' + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + '-' +
22+
lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + '-' + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
23+
lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
24+
};

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "simple-uuid",
3+
"version": "1.0.0",
4+
"description": "Simple UUID generation",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha test"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://TylerGarlick@github.com/TylerGarlick/simple-uuid"
12+
},
13+
"keywords": [
14+
"uuid",
15+
"guid"
16+
],
17+
"author": "Tyler Garlick <tjgarlick@gmail.com>",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/TylerGarlick/simple-uuid/issues"
21+
},
22+
"homepage": "https://github.com/TylerGarlick/simple-uuid",
23+
"devDependencies": {
24+
"chai": "^1.10.0",
25+
"lodash": "^2.4.1",
26+
"mocha": "^2.1.0"
27+
}
28+
}

test/uuid.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
var expect = require('chai').expect
4+
, _ = require('lodash');
5+
6+
var UUID = require('../');
7+
8+
describe('UUID', function () {
9+
10+
describe('#generate()', function () {
11+
12+
it('should be able to generate a UUID', function () {
13+
var uuid = UUID.generate();
14+
expect(uuid).to.be.ok();
15+
expect(uuid).to.have.length(36);
16+
});
17+
18+
it('should generate 100 random UUID\'s', function () {
19+
var uuids = [];
20+
for(var i = 0; i < 100; i++) {
21+
var uuid = UUID.generate();
22+
expect(_.contains(uuids, uuid)).to.be.false();
23+
uuids.push(uuid);
24+
}
25+
expect(uuids).to.have.length(100);
26+
});
27+
28+
});
29+
});

uuid.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Fast UUID generator, RFC4122 version 4 compliant.
3+
* @author Jeff Ward (jcward.com).
4+
* @license MIT license
5+
* @link http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
6+
**/
7+
var UUID = (function () {
8+
'use strict';
9+
var self = {};
10+
var lut = [];
11+
12+
for(var i = 0; i < 256; i++) {
13+
lut[i] = (i < 16 ? '0' : '') + (i).toString(16);
14+
}
15+
16+
/**
17+
* Generates a UUID
18+
* @returns {string}
19+
*/
20+
self.generate = function () {
21+
var d0 = Math.random() * 0xffffffff | 0;
22+
var d1 = Math.random() * 0xffffffff | 0;
23+
var d2 = Math.random() * 0xffffffff | 0;
24+
var d3 = Math.random() * 0xffffffff | 0;
25+
return lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + '-' +
26+
lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + '-' + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + '-' +
27+
lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + '-' + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
28+
lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
29+
};
30+
return self;
31+
32+
})();

0 commit comments

Comments
 (0)