Git Product home page Git Product logo

guid's Introduction

NOTE: This creates GUID-like strings and not GUIDs. @broofa's node-uuid module is much better than this implementation.

Guid lets you generate and validate unique identifiers.

browser support

In its simplest form, Guid lets you generate raw GUID formatted strings:

Guid.raw();
// -> '6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'

Let's generate a new Guid instance.

var guid = Guid.create();

We've now got an object which we can work with programmatically. Lets check the validity of our Guid using the built-in validator:

Guid.isGuid(guid);
// -> true

Guid.value;
// -> '6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'

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/" + guid;
// -> 'http://whatever.com/items/6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'

If you need a placeholder Guid, or a value to represent a non-GUID, use the static EMPTY property:

Guid.EMPTY;
// -> '00000000-0000-0000-0000-000000000000'

Once you have a Guid object, you can't change its value (thanks ES5!):

guid.value = "go suck it, guid!"
guid.value;
// -> '6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'

To instantiate an Guid object using an existing GUID string, use the constructor:

var guid = new Guid('6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d');

You can check the equality of two different Guid objects using the equals instance method.

Compare a Guid object to a GUID string:

guid.equals('6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d');
// -> true

Compare two Guid objects:

guid.equals(new Guid('6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'));
// -> true

Installation

You can use npm to install guid: npm install guid

But what if I need a UUID, not a GUID?

There's also a Uuid library which has the exact same functionality, but for UUID's: https://github.com/dandean/uuid-lib

guid's People

Contributors

joscha avatar oscargodson avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.