This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
forked from uuidjs/uuid
-
Notifications
You must be signed in to change notification settings - Fork 34
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
0 parents
commit fba452b
Showing
6 changed files
with
120 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,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). | ||
|
||
|
||
|
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,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" | ||
} |
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,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> |
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,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'); | ||
} |
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,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); |
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,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; | ||
} | ||
})(); |