UNPKG

@omnia/fx-models

Version:
52 lines (51 loc) 1.4 kB
"use strict"; //export interface GuidValue { // :string; //} Object.defineProperty(exports, "__esModule", { value: true }); exports.Guid = void 0; /** * Class to create a guid */ class Guid { static newGuid() { 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); })); } static get empty() { return this._empty; } static isValid(guid) { if (guid === undefined || guid === null) { return false; } const validRegex = /^[{]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[}]?$/; return validRegex.test(guid.toString()); } constructor(guid) { this.value = Guid._empty; //We serialize as string value this.toJSON = () => { return this.value.toLowerCase(); }; if (guid) { if (Guid.isValid(guid)) { this.value = guid; } else { throw `The string '${guid}' is not a valid Guid`; } } } toString() { return this.value.toLowerCase(); } valueOf() { return this.value.toLowerCase(); } } exports.Guid = Guid; Guid._empty = "00000000-0000-0000-0000-000000000000";