Guid class

This class represents a globally unique identifier, as described by IETF RFC 4122.

Remarks

The input string is normalized and validated, which provides important guarantees that simplify other code that works with the GUID. This class also provides basic support for generating a pseudo-random GUID; however, be aware that the uniqueness depends on the browser's Math.random() function and may be not be suitable for some applications.

See RFC4122 for more information.

Properties

empty

Returns a new empty Guid instance.

Methods

equals(guid)

Compare this instance to another Guid instance

isValid(guid)

Indicates whether a GUID is valid, i.e. whether it would be successfully parsed by Guid.tryParse(). This function is cheaper than Guid.tryParse() because it does not construct a Guid object.

newGuid(randomNumberGenerator)

Returns a new Guid instance with a pseudo-randomly generated GUID, according to the version 4 UUID algorithm from RFC 4122.

parse(guidString)

Parses the input string to construct a new Guid object. If the string cannot be parsed, then an error is thrown.

toString()

Returns a string representation of the GUID

tryParse(guid)

Attempts to parse the input string to construct a new Guid object. If the string cannot be parsed, then undefined is returned.

Property Details

empty

Returns a new empty Guid instance.

static readonly empty: Guid;

Property Value

Method Details

equals(guid)

Compare this instance to another Guid instance

equals(guid: Guid): boolean;

Parameters

guid
Guid

Returns

boolean

True if this instance and the specified Guid object represent the same value.

isValid(guid)

Indicates whether a GUID is valid, i.e. whether it would be successfully parsed by Guid.tryParse(). This function is cheaper than Guid.tryParse() because it does not construct a Guid object.

static isValid(guid: string | undefined | null): boolean;

Parameters

guid

string | undefined | null

The input string.

Returns

boolean

true, if the Guid is valid.

newGuid(randomNumberGenerator)

Returns a new Guid instance with a pseudo-randomly generated GUID, according to the version 4 UUID algorithm from RFC 4122.

static newGuid(randomNumberGenerator?: IRandomNumberGenerator): Guid;

Parameters

randomNumberGenerator
IRandomNumberGenerator

Returns

A new unique Guid object

parse(guidString)

Parses the input string to construct a new Guid object. If the string cannot be parsed, then an error is thrown.

static parse(guidString: string | undefined | null): Guid;

Parameters

guidString

string | undefined | null

The input string.

Returns

A valid Guid object

Remarks

Example syntaxes accepted by this function:

  • "d5369f3bbd7a412a9c0f7f0650bb5489"

  • "d5369f3b-bd7a-412a-9c0f-7f0650bb5489"

  • "{d5369f3b-bd7a-412a-9c0f-7f0650bb5489}"

  • "/Guid(d5369f3b-bd7a-412a-9c0f-7f0650bb5489)/"

toString()

Returns a string representation of the GUID

toString(): string;

Returns

string

The GUID value in lowercase hexadecimal without braces.

Example: 'd5369f3b-bd7a-412a-9c0f-7f0650bb5489'

tryParse(guid)

Attempts to parse the input string to construct a new Guid object. If the string cannot be parsed, then undefined is returned.

static tryParse(guid: string | undefined | null): Guid | undefined;

Parameters

guid

string | undefined | null

The input string.

Returns

Guid | undefined

The Guid object, or undefined if the string could not be parsed.

Remarks

Example syntaxes accepted by this function:

  • "d5369f3bbd7a412a9c0f7f0650bb5489"

  • "d5369f3b-bd7a-412a-9c0f-7f0650bb5489"

  • "{d5369f3b-bd7a-412a-9c0f-7f0650bb5489}"

  • "/Guid(d5369f3b-bd7a-412a-9c0f-7f0650bb5489)/"