Skip to content

Instantly share code, notes, and snippets.

@vitaliidasaev
Forked from emptyother/Guid.ts
Created July 2, 2020 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitaliidasaev/b002a53978a5513334c6ddecb7111c7e to your computer and use it in GitHub Desktop.
Save vitaliidasaev/b002a53978a5513334c6ddecb7111c7e to your computer and use it in GitHub Desktop.
GUID class for Typescript
class Guid {
public static newGuid(): Guid {
return new Guid('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
const v = (c == 'x') ? r : (r & 0x3 | 0x8);
return v.toString(16);
}));
}
public static get empty(): string {
return '00000000-0000-0000-0000-000000000000';
}
public get empty(): string {
return Guid.empty;
}
public static isValid(str: string): boolean {
const validRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return validRegex.test(str);
}
private value: string = this.empty;
constructor(value?: string) {
if (value) {
if(Guid.isValid(value)) {
this.value = value;
}
}
}
public toString() {
return this.value;
}
public toJSON(): string {
return this.value;
}
}
var e = new Guid();
e; // Guid { value: '00000000-0000-0000-0000-000000000000' }​​​​​
e.toString(); // 00000000-0000-0000-0000-000000000000​​​​​
console.log(e); // ​​​​​Guid { value: '00000000-0000-0000-0000-000000000000' }​​​​​
JSON.stringify(e); // ​​​​​"00000000-0000-0000-0000-000000000000"​​​​​
e = Guid.newGuid(); // ​​​​​Guid { value: 'bb90ef83-1a7e-42b1-90ba-39cdebb6366c' }​​​​​
JSON.stringify(e); // ​​​​​"bb90ef83-1a7e-42b1-90ba-39cdebb6366c"​​​​​
Guid.isValid(e.toString()); // true
Guid.empty; // ​​​​​00000000-0000-0000-0000-000000000000​​​​​
Guid.isValid(Guid.empty); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment