A typescript Guid class?

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

Does anyone know of a good, solid, implementation of C# like Guid in typescript?

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

Thanks!

回答1:

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 

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).



回答2:

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 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!