File

projects/wms-framework/src/lib/baseframework/Guid.ts

Description

Represents a Globally Unique Identifier (GUID).

Implements

Hashable

Index

Properties
  • Private a
  • Private b
  • Private c
  • Private d
  • Static Readonly empty
  • Private Static Readonly guidRegex
Methods

Constructor

Public constructor(value?: string)

Creates an instance of Guid.

Parameters :
Name Type Optional
value string Yes

Properties

Private a
Type : number

Stores the first part of the GUID.

Private b
Type : number

Stores the second part of the GUID.

Private c
Type : number

Stores the third part of the GUID.

Private d
Type : number

Stores the fourth part of the GUID.

Static Readonly empty
Default value : new Guid()

Empty GUID.

Private Static Readonly guidRegex
Default value : /^[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]$/i

Regex used to validate if some string is a valid GUID.

Methods

Public equals
equals(other: any)

Compares this Guid against another object.

Parameters :
Name Type Optional
other any No
Returns : boolean

{boolean} true if both Guid are equivalent, false otherwise.

Public Equals
Equals(obj: any)

Compares this Guid against another object.

Parameters :
Name Type Optional
obj any No
Returns : boolean

{boolean}

Public GetHashCode
GetHashCode()

Gets the hash code for this Guid.

Returns : number

{number}

Static newGuid
newGuid()

Returns a version 4 GUID (random GUID).

Returns : Guid

{Guid}

Static parse
parse(value: string)

Parse an input string into a Guid.

Parameters :
Name Type Optional Description
value string No

The input string.

Returns : Guid

{Guid} The parsed Guid.

Public toString
toString()

Gets the string representation of this Guid.

Returns : string

{string}

Static tryParse
tryParse(value: string, result: out<Guid>)

Attemps to parse a string into a Guid.

Parameters :
Name Type Optional Description
value string No

The input string.

result out<Guid> No

The output Guid.

Returns : boolean

{boolean} true if the parsing is successful, false otherwise.

import { ClassInfo } from '../decorators';
import { out } from '../utils/RefOrOut';
import { Hashable } from './Hashable';

/**
 * Represents a Globally Unique Identifier (GUID).
 *
 * @export
 * @class Guid
 * @wType System.Guid
 * @wNetSupport
 */
// @dynamic
@ClassInfo({ classId: 'System.Guid' })
export class Guid implements Hashable {
  /**
   * Empty GUID.
   *
   * @static
   * @memberof Guid
   * @wProperty Empty
   */
  public static readonly empty = new Guid();

  /**
   * Regex used to validate if some string is a valid GUID.
   *
   * @private
   * @static
   * @memberof Guid
   */
  private static readonly guidRegex =
    /^[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F][0-9|A-F]$/i;

  /**
   * Stores the first part of the GUID.
   *
   * @private
   * @type {number}
   * @memberof Guid
   */
  private a: number;

  /**
   * Stores the second part of the GUID.
   *
   * @private
   * @type {number}
   * @memberof Guid
   */
  private b: number;

  /**
   * Stores the third part of the GUID.
   *
   * @private
   * @type {number}
   * @memberof Guid
   */
  private c: number;

  /**
   * Stores the fourth part of the GUID.
   *
   * @private
   * @type {number}
   * @memberof Guid
   */
  private d: number;

  /**
   * Creates an instance of Guid.
   *
   * @param {string} [value]
   * @memberof Guid
   */
  public constructor(value?: string) {
    if (typeof value === 'undefined') {
      this.a = 0;
      this.b = 0;
      this.c = 0;
      this.d = 0;
      return;
    }

    if (!Guid.guidRegex.test(value)) {
      throw Error('Invalid GUID format');
    }

    const noDash = value.replace(/-/g, '');
    this.a = parseInt(noDash.slice(0, 8), 16);
    this.b = parseInt(noDash.slice(8, 16), 16);
    this.c = parseInt(noDash.slice(16, 24), 16);
    this.d = parseInt(noDash.slice(24, 32), 16);
  }

  /**
   * Returns a version 4 GUID (random GUID).
   *
   * @static
   * @return {*}  {Guid}
   * @memberof Guid
   * @wMethod NewGuid
   */
  public static newGuid(): Guid {
    // see https://stackoverflow.com/questions/105034/how-to-create-guid-uuid
    const guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
      /[xy]/g,
      function (c) {
        var r = (Math.random() * 16) | 0,
          v = c == 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
      }
    );
    return new Guid(guid);
  }

  /**
   * Attemps to parse a string into a `Guid`.
   *
   * @static
   * @param {string} value The input string.
   * @param {out<Guid>} result The output `Guid`.
   * @return {*}  {boolean} `true` if the parsing is successful, `false` otherwise.
   * @memberof Guid
   * @wMethod TryParse
   */
  public static tryParse(value: string, result: out<Guid>): boolean {
    if (this.guidRegex.test(value)) {
      result.value = new Guid(value);
      return true;
    } else {
      result.value = Guid.empty;
      return false;
    }
  }

  /**
   * Parse an input string into a `Guid`.
   *
   * @static
   * @param {string} value The input string.
   * @return {*}  {Guid} The parsed `Guid`.
   * @throws If the input string is not a valid `Guid`.
   * @memberof Guid
   * @wMethod Parse
   */
  public static parse(value: string): Guid {
    return new Guid(value);
  }

  /**
   * Compares this `Guid` against another object.
   *
   * @param {*} other
   * @return {*}  {boolean} `true` if both `Guid` are equivalent, `false` otherwise.
   * @memberof Guid
   */
  public equals(other: any): boolean {
    return (
      other instanceof Guid &&
      this.a === other.a &&
      this.b === other.b &&
      this.c === other.c &&
      this.d === other.d
    );
  }

  /**
   * Gets the string representation of this `Guid`.
   *
   * @return {*}  {string}
   * @memberof Guid
   * @wMethod ToString
   */
  public toString(): string {
    const strA = this.a.toString(16).padStart(8, '0');
    const strB = this.b.toString(16).padStart(8, '0');
    const strC = this.c.toString(16).padStart(8, '0');
    const strD = this.d.toString(16).padStart(8, '0');
    const strB1 = strB.slice(0, 4);
    const strB2 = strB.slice(4);
    const strC1 = strC.slice(0, 4);
    const strC2 = strC.slice(4);
    return `${strA}-${strB1}-${strB2}-${strC1}-${strC2}${strD}`;
  }

  /**
   * Gets the hash code for this `Guid`.
   *
   * @return {*}  {number}
   * @memberof Guid
   */
  // eslint-disable-next-line @typescript-eslint/naming-convention
  public GetHashCode(): number {
    // Similar implementation to the one used in .Net Core:
    //   https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Guid.cs#L797
    // XOR all bits in the GUID.
    // eslint-disable-next-line no-bitwise
    return this.a ^ this.b ^ this.c ^ this.d;
  }

  /**
   * Compares this `Guid` against another object.
   *
   * @param {*} obj
   * @return {*}  {boolean}
   * @memberof Guid
   */
  // eslint-disable-next-line @typescript-eslint/naming-convention
  public Equals(obj: any): boolean {
    return this.equals(obj);
  }
}

result-matching ""

    No results matching ""