Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@slimpleton/ts-guid) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Typescript Guid
A Typescript safe implementation of a GUID that allows for validation of the format while limiting boilerplate to reference the value itself. Uses types not classes to be more lightweight / reusable
Installation and Usage
Installation
npm i @slimpleton/ts-guid --saveBasic Usage
export class Xyz{
public id: UUID;
constructor(){
this.id = createRandomGuid();
}
//or
constructor(id: `${string}-${string}-${string}-${string}-${string}`){
this.id = parseGuid(id);
}
// or
constructor(id: `${string}-${string}-${string}-${string}-${string}`, version: UUIDVersion){
this.id = parseGuid(id, version);
}
}
Types Included
| Type | Description |
|---|---|
| UUID | Generic type encompassing all versions of Guid<UUIDVersion> available |
| UUIDVersion | Currently supported versions of UUID for parsing by library (4-7) |
Methods Included
| Method | Description |
|---|---|
| createRandomGuid(): UUIDv4 | Creates a random Guid using crypto.randomUUID() and returns it strictly typed |
| parseGuid(val: string, version: UUIDVersion = 4): Guid<UUIDVersion> | Attempts to parse the value with versioned UUID format |
ESLINTER USERS
This comes with an extension to the ruleset that is Highly Recommended to prevent runtime 'as-casting' to Guid 'no-as-uuid':'error'