Guid0: A Javascript GUID Generator

Guid0.js

Guid0 is a GUID library for Javascript. Okay, it doesn’t yet do official, bona fide, 128-bit, GUIDs yet, mainly for API design reasons. But this is a library you might find useful if you want to generate a unique ID in your Ajax app.

Usage:

javascript
< view plain text >
  1. guid = new Guid();
  2. guid.generate(); // Returns a unique ID, e.g. "dkvagrkx1rt"

With options:

javascript
< view plain text >
  1. guid = new Guid(
  2.   {
  3.     chars: Guid.constants.base85,  // or you could say "abc" if you only wanted those chars to appear
  4.     epoch: "June 1, 2003",
  5.     counterSequenceLength: 2, // a counter field appended to the end
  6.     randomSequenceLength: 2 // a random field appended to the end
  7.   }
  8. )

The demo:

The demo generates a bunch of GUIDs. They look almost the same because most of the GUID is just a time representation in Base N (where N is the number of characters in the GUID’s configured charset).

I made Guid0 in conjunction with TiddlyWiki work. Within a store, tiddlers are keyed on their title. In the typical case, the user has entered a title to describe the content. But when we get into application territory – using TiddlyWiki as an application framework – there are reasons why we want the more familiar database approach of automagically generating the primary key. These are several reasons for this:

  • Tiddlers with no meaningful title. Sometimes, tiddlers are just containers for text someone has entered. A perfect example is the Comments Plugin I’ve been working on (separate blog post pending). A comment is a tiddler. When you have a blog or forum or whatever with comments, you usually don’t invite users title their comment…it’s not very enticing. So in this case, the comment is the tiddler text and the title must be auto-generated so we can put it in the store.
  • Renaming. As with the usual motivation for auto-generated IDs over meaningful IDs, renaming support is sometimes required. In one case, I have a user submitting a first cut of something, and an admin coming along later and cleansing it. We can’t expect the user to get the name right the first time, and there are several tiddlers referencing the initial tiddler (via custom fields) by the time it might be renamed.
  • Can’t ensure uniqueness. With multiple users, you don’t always want to force them into choosing different titles for each tiddler. If the comments plugin did include titles for instance, you wouldn’t want a uniqueness constraint. Using GUIDs, you could support multiple titles with the same display name.

The library is generic – not TiddlyWiki-specific – but I’ve wrapped it into a small TiddlyWiki plugin. In the future, we may expand the plugin. For example, each time a new tiddler is created, the plugin may intercept the call and give the tiddler a GUID title, and stick the title in a “name” field.

I’ll update this post when proper 128-bit GUID is supported.

7 thoughts on Guid0: A Javascript GUID Generator

  1. Pingback: Ajaxian » Guid0: JavaScript GUIDs

  2. Pingback: Guid0: JavaScript GUIDs | Eroarea 403

  3. I’m looking over this code again. The Number.toString method takes an optional radix up to 36, which obviates the need for some of the alphabet constants. For example, 35..toString(36) == ‘z’. (The double dot is only necessary for invoking members of number literals since the first dot is interpreted as a decimal point.)

  4. Pingback: TiddlyCMS Plugins from TiddlyGuv

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>