Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Dec 28, 2010
0 parents commit fba452b
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
= node-uuid

Generate RFC4122(v4) compliant UUID strings.

== Usage

var uuid = require('node-uuid');
var id = uuid(); /// -> '92329D39-6F5C-4520-ABFC-AAB64544E172'

== Other UUID modules

There area couple other node uuid-related modules out there, but the only
one I've found that's of much interest is Nikhil Marathe's `uuidjs` module.
Being built on the native libuuid library, it is both fast and powerful (it
can generate binary UUIDs, something I haven't tackled here.)

So why have node-uuid?

The main reason is that an all-JS implementation provides consistent UUID
generation on both client and server. (node-uuid works in browsers).



12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name" : "node-uuid",
"description" : "Pure JS support for RFC4122(v4) UUIDs (plus a non-RFC utility for generating more compact uids",
"url" : "http://github.com/broofa/node-uuid",
"keywords" : ["uuid", "guid", "rfc4122"],
"author" : "Robert Kieffer <robert@broofa.com>",
"contributors" : [],
"dependencies" : [],
"lib" : ".",
"main" : "lib/uuid.js",
"version" : "0.1.0"
}
15 changes: 15 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<body>Open JavaScript Console to see test results</body>
<script src="../uuid.js"></script>
<script>
function assert(x, msg) {
if (!x) throw Error(msg);
}

var UUID_FORMAT = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/;
for (var i = 0; i < 100; i++) {
id = uuid();
assert(UUID_FORMAT.test(id), 'Proper format');
}
</script>
</html>
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var assert = require('assert').ok;
var uuid = require('../uuid');

var UUID_FORMAT = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/;
for (var i = 0; i < 100; i++) {
id = uuid();
assert(UUID_FORMAT.test(id), 'Proper format');
}
11 changes: 11 additions & 0 deletions test/testVsUuidjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function bench(f, msg, n) {
var start = Date.now();
for (var i = 0; i < n; i++) {
f();
}
var t = Date.now() - start;
console.log(msg + ': ' + (n / (t / 1000) | 0) + ' times per second');
}

bench(require('../uuid'), 'node-uuid', 1e6);
bench(require('uuid'.generate), 'uuid.js', 1e6);
51 changes: 51 additions & 0 deletions uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(function() {
/*
* Generate a RFC4122(v4) UUID
*
* In Node.js:
*
* var uuid = require('node-uuid');
* var id = uuid(); // -> '92329D39-6F5C-4520-ABFC-AAB64544E172'
*
* In a browser:
*
* <script src="uuid.js"></script>
* <script>
* var id = uuid(); // -> '92329D39-6F5C-4520-ABFC-AAB64544E172'
* <script>
*/
var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
function uuid() {
var chars = CHARS, id = new Array(36);
var id = [36], rnd, r, h = 8;
for (var i = 0, j = 0; i < 36; i++) {
if (i == h) {
// Insert hyphens where needed
id[i] = '-';
h = h < 23 ? h+5 : -1;
} else if (i==14) {
// RFC4122 (sec 4.1.3) requires this
id[i] = '4';
} else { // Set random bits
// Minimize calls to [the expensive] Math.random() method
if (j == 0) {
rnd = Math.random()*0x100000000;
}
var r = rnd & 0xf;
rnd = rnd >>> 4;
j = (j + 1) & 0x7;

id[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}

return id.join('');
};

if (typeof(module) != 'undefined') {
module.exports = uuid;
} else {
// In browser? Set as top-level function
this.uuid = uuid;
}
})();

0 comments on commit fba452b

Please sign in to comment.