This repository has been archived by the owner on Jun 17, 2020. It is now read-only.
forked from snico-dev/guid-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
90 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"html.format.wrapAttributes": "force-aligned", | ||
"editor.minimap.enabled": false, | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,47 @@ | ||
export class Guid { | ||
|
||
public static validator = new RegExp("^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$", "i"); | ||
|
||
public static EMPTY = "00000000-0000-0000-0000-000000000000"; | ||
|
||
public static isGuid = (guid: any) => guid && (guid instanceof Guid || Guid.validator.test(guid.toString())); | ||
|
||
public static create = (): Guid => new Guid([Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join("-")); | ||
|
||
public static createEmpty = (): Guid => new Guid("emptyguid"); | ||
|
||
public static parse = (guid: string): Guid => new Guid(guid); | ||
export const globals = { | ||
guidDefaultValue: '00000000-0000-0000-0000-000000000000', | ||
validationPattern: '^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$' | ||
} | ||
|
||
public static raw = (): string => [Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join("-"); | ||
export class Guid { | ||
|
||
private static gen(count: number) { | ||
let out: string = ""; | ||
for (let i: number = 0; i < count; i++) { | ||
// tslint:disable-next-line:no-bitwise | ||
out += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); | ||
} | ||
let out: string = ''; | ||
for (let i: number = 0; i < count; i++) out += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); | ||
return out; | ||
} | ||
|
||
private value: string; | ||
|
||
private constructor(guid: string) { | ||
if (!guid) throw new TypeError("Invalid argument; `value` has no value."); | ||
this.value = Guid.EMPTY; | ||
protected constructor(guid: string) { | ||
if (!guid) throw new TypeError('Invalid argument; `value` has no value.'); | ||
this.value = globals.guidDefaultValue; | ||
|
||
if (guid && Guid.isGuid(guid)) this.value = guid; | ||
} | ||
|
||
static validator = new RegExp(globals.validationPattern, 'i'); | ||
|
||
static isGuid = (guid: any) => guid && (guid instanceof Guid || Guid.validator.test(guid.toString())); | ||
|
||
static create = (): Guid => new Guid([Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join('-')); | ||
|
||
static createEmpty = (): Guid => new Guid(globals.guidDefaultValue); | ||
|
||
static parse = (guid: string): Guid => new Guid(guid); | ||
|
||
static raw = (): string => [Guid.gen(2), Guid.gen(1), Guid.gen(1), Guid.gen(1), Guid.gen(3)].join('-'); | ||
|
||
static EMPTY = Guid.parse(globals.guidDefaultValue); | ||
|
||
// Comparing string `value` against provided `guid` will auto-call | ||
// toString on `guid` for comparison | ||
public equals = (other: Guid): boolean => Guid.isGuid(other) && this.value === other.toString(); | ||
equals = (other: Guid): boolean => Guid.isGuid(other) && this.value === other.toString(); | ||
|
||
public isEmpty = (): boolean => this.value === Guid.EMPTY; | ||
isEmpty = (): boolean => this.value === globals.guidDefaultValue; | ||
|
||
public toString = (): string => this.value; | ||
toString = (): string => this.value; | ||
|
||
public toJSON(): any { return { value: this.value, }; } | ||
toJSON(): any { return { value: this.value } } | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters