UNPKG

made-runtime

Version:
66 lines 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Guid = void 0; /** * Defines a globally unique identifier (GUID). */ class Guid { guid; _guid; /** * Initializes a new instance of the Guid class using a Guid value represented by a string. * @param {string} guid - A string that contains a valid GUID. */ constructor(guid) { this.guid = guid; if (!this.isValid(guid)) { throw new Error('The format of the Guid is invalid.'); } this._guid = guid.toLowerCase(); } /** * Returns a value indicating whether this instance and a specified Guid object represent the same value. * @param {Guid} other - A Guid to compare to this instance. * @returns {boolean} True if the Guid is equal to this instance; otherwise, false. */ equals(other) { return this.toString() === other.toString(); } /** * Returns a string representation of the value of this instance of a Guid. * @returns {string} The value of this Guid, formatted as: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. */ toString() { return this._guid; } /** * An instance of the Guid structure whose value is all zeros. * @returns {Guid} A zero GUID. */ static empty() { return new Guid('00000000-0000-0000-0000-000000000000'); } /** * Initializes a new instance of the Guid with a value that is created from a sequence of random bytes. * @returns {Guid} A new GUID. */ static newGuid() { var result; var i; var j; result = ""; for (j = 0; j < 32; j++) { if (j == 8 || j == 12 || j == 16 || j == 20) result = result + '-'; i = Math.floor(Math.random() * 16).toString(16).toLowerCase(); result = result + i; } return new Guid(result); } isValid(guid) { var guidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; return guidRegex.test(guid); } } exports.Guid = Guid; //# sourceMappingURL=Guid.js.map