Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 01c11e61e867e7d06c4e31fdce7d873121cc899a @dandean committed Nov 14, 2010
Showing with 163 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +59 −0 README.md
  3. +22 −0 package.json
  4. +63 −0 uuid.js
19 LICENSE
@@ -0,0 +1,19 @@
+
+Copyright 2009, 2010 Dan Dean. All rights reserved.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
59 README.md
@@ -0,0 +1,59 @@
+# Uuid lets you generate and validate Universally Unique Identifiers.
+
+In its simplest form, this lib lets you generate raw UUID formatted strings:
+
+ Uuid.raw();
+ // -> '6fdf6ffced7794fa407ea7b86ed9e59d'
+
+But it has a more robust API which lets you work with UUID's as objects. Let's
+generate a new Uuid instance.
+
+ var uuid = Uuid.create();
+
+We've now got an object which we can work with programmatically. Lets check the
+validity of our uuid using the built-in validator:
+
+ Uuid.isUuid(uuid);
+ // -> true
+
+ Uuid.value;
+ // -> '6fdf6ffced7794fa407ea7b86ed9e59d'
+
+A handy bit of functionality is that its `toString` method returns the string
+value, so you can do handy things like this:
+
+ var itemUrl = "http://whatever.com/items/" + uuid;
+ // -> 'http://whatever.com/items/6fdf6ffced7794fa407ea7b86ed9e59d'
+
+If you need a placehold Uuid, or a value to represent a non-uuid, use the static
+`EMPTY` property:
+
+ Uuid.EMPTY;
+ // -> '00000000000000000000000000000000'
+
+Once you have a `Uuid` object, you can't change its value (thanks ES5!):
+
+ uuid.value = "go suck it, uuid!"
+ uuid.value;
+ // -> '6fdf6ffced7794fa407ea7b86ed9e59d'
+
+To instantiate an Uuid object using an existing Uuid string, use the constructor:
+
+ var uuid = new Uuid('6fdf6ffced7794fa407ea7b86ed9e59d');
+
+You can check the equality of two different Uuid objects using the `equals`
+instance method.
+
+Compare a Uuid object to a uuid string:
+
+ uuid.equals('6fdf6ffced7794fa407ea7b86ed9e59d');
+ // -> true
+
+Compare two Uuid objects:
+
+ uuid.equals(new Uuid('6fdf6ffced7794fa407ea7b86ed9e59d'));
+ // -> true
+
+## Installation
+
+You can use npm to install uuid: `npm install uuid`
22 package.json
@@ -0,0 +1,22 @@
+{ "name" : "uuid"
+, "description" : "A Uuid generator and validator."
+, "version" : "0.0.5"
+, "homepage" : "http://github.com/dandean/uuid"
+, "author" : "Dan Dean <me@dandean.com> (http://dandean.com)"
+, "contributors" : []
+, "repository" :
+ { "type" : "git"
+ , "url" : "http://github.com/dandean/uuid.git"
+ }
+, "bugs" :
+ { "mail" : "me@dandean.com"
+ , "web" : "http://github.com/dandean/uuid/issues"
+ }
+, "main" : "uuid"
+, "engines" : { "node" : ">=0.2.0" }
+, "licenses" :
+ [ { "type" : "MIT"
+ , "url" : "http://github.com/dandean/uuid/raw/master/LICENSE"
+ }
+ ]
+}
63 uuid.js
@@ -0,0 +1,63 @@
+var validator = new RegExp("^[a-z0-9]{32}$", "i");
+
+function gen(count) {
+ var out = "";
+ for (var i=0; i<count; i++) {
+ out += (((1+Math.random())*0x10000)|0).toString(16).substring(1);
+ }
+ return out;
+}
+
+function Uuid(uuid) {
+ if (!uuid) throw new TypeError("Invalid argument; `value` has no value.");
+
+ var value = Uuid.EMPTY;
+
+ if (uuid && uuid instanceof Uuid) {
+ value = Uuid.toString();
+
+ } else if (uuid && Object.prototype.toString.call(uuid) === "[object String]" && Uuid.isUuid(uuid)) {
+ value = uuid;
+ }
+
+ this.equals = function(other) {
+ // Comparing string `value` against provided `uuid` will auto-call
+ // toString on `uuid` for comparison
+ return Uuid.isUuid(other) && value == other;
+ };
+
+ this.isEmpty = function() {
+ return value === Uuid.EMPTY;
+ };
+
+ this.toString = function() {
+ return value;
+ };
+
+ this.toJSON = function() {
+ return value;
+ };
+
+ Object.defineProperty(this, "value", {
+ get: function() { return value; },
+ enumerable: true
+ });
+};
+
+Object.defineProperty(Uuid, "EMPTY", {
+ value: "00000000000000000000000000000000"
+});
+
+Uuid.isUuid = function(value) {
+ return value && (value instanceof Uuid || validator.test(value.toString()));
+};
+
+Uuid.create = function() {
+ return new Uuid(gen(8));
+};
+
+Uuid.raw = function() {
+ return gen(8);
+};
+
+module.exports = Uuid;

0 comments on commit 01c11e6

Please sign in to comment.
Something went wrong with that request. Please try again.