Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A TypeScript GUID class? [closed]

Does anyone know of a good, solid, implementation of C# like GUID (UUID) in TypeScript?

Could do it myself but figured I'd spare my time if someone else done it before.

like image 571
Gustav Avatar asked Oct 22 '14 06:10

Gustav


People also ask

Does TypeScript have GUID?

Although neither the TypeScript nor JavaScript language have built-in support for generating a UUID or GUID, there are plenty of quality 3rd party, open-source libraries that you can use.

Are UUID and GUID the same?

The GUID designation is an industry standard defined by Microsoft to provide a reference number which is unique in any context. UUID is a term that stands for Universal Unique Identifier. Similarly, GUID stands for Globally Unique Identifier. So basically, two terms for the same thing.

What is GUID in angular?

Unique Identifier generation is a requirement in any programming language. It contains 128 bits in size separated by a hyphen with 5 groups. Angular is an MVC framework based on Typescript. GUID and UUID generate 128 bits of the implementation.


2 Answers

There is an implementation in my TypeScript utilities based on JavaScript GUID generators.

Here is the code:

class Guid {    static newGuid() {      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {        var r = Math.random() * 16 | 0,          v = c == 'x' ? r : (r & 0x3 | 0x8);        return v.toString(16);      });    }  }    // Example of a bunch of GUIDs  for (var i = 0; i < 100; i++) {    var id = Guid.newGuid();    console.log(id);  }

Please note the following:

C# GUIDs are guaranteed to be unique. This solution is very likely to be unique. There is a huge gap between "very likely" and "guaranteed" and you don't want to fall through this gap.

JavaScript-generated GUIDs are great to use as a temporary key that you use while waiting for a server to respond, but I wouldn't necessarily trust them as the primary key in a database. If you are going to rely on a JavaScript-generated GUID, I would be tempted to check a register each time a GUID is created to ensure you haven't got a duplicate (an issue that has come up in the Chrome browser in some cases).

like image 196
Fenton Avatar answered Oct 12 '22 23:10

Fenton


I found this https://typescriptbcl.codeplex.com/SourceControl/latest

here is the Guid version they have in case the link does not work later.

module System {     export class Guid {         constructor (public guid: string) {             this._guid = guid;         }          private _guid: string;          public ToString(): string {             return this.guid;         }          // Static member         static MakeNew(): Guid {             var result: string;             var i: string;             var j: number;              result = "";             for (j = 0; j < 32; j++) {                 if (j == 8 || j == 12 || j == 16 || j == 20)                     result = result + '-';                 i = Math.floor(Math.random() * 16).toString(16).toUpperCase();                 result = result + i;             }             return new Guid(result);         }     } } 
like image 28
dburmeister Avatar answered Oct 12 '22 23:10

dburmeister