ClassScru128Id

Represents a SCRU128 ID and provides converters and comparison operators.

import { Scru128Id } from "scru128";

const x = Scru128Id.fromString("036z968fu2tugy7svkfznewkk");
console.log(String(x));

const y = Scru128Id.fromBigInt(0x017fa1de51a80fd992f9e8cc2d5eb88en);
console.log(y.toBigInt());

Conversion

  • Returns the 128-bit unsigned integer representation.

    Returns bigint

  • Returns the 128-bit unsigned integer representation as a 32-digit hexadecimal string prefixed with "0x".

    Returns string

  • Returns the 25-digit canonical string representation.

    Returns string

  • Creates an object from a 128-bit unsigned integer.

    Parameters

    • value: bigint

    Returns Scru128Id

    RangeError if the argument is out of the range of 128-bit unsigned integer.

  • Creates an object from a byte array representing either a 128-bit unsigned integer or a 25-digit Base36 string.

    This method shallow-copies the content of the argument, so the created object holds another instance of the byte array.

    Parameters

    • value: ArrayLike<number>

      An array of 16 bytes that contains a 128-bit unsigned integer in the big-endian (network) byte order or an array of 25 ASCII code points that reads a 25-digit Base36 string.

    Returns Scru128Id

    SyntaxError if conversion fails.

  • Creates an object from field values.

    Parameters

    • timestamp: number

      A 48-bit timestamp field value.

    • counterHi: number

      A 24-bit counter_hi field value.

    • counterLo: number

      A 24-bit counter_lo field value.

    • entropy: number

      A 32-bit entropy field value.

    Returns Scru128Id

    RangeError if any argument is out of the value range of the field.

  • Creates an object from a 128-bit unsigned integer encoded in a hexadecimal string.

    Parameters

    • value: string

    Returns Scru128Id

    SyntaxError if the argument is not a hexadecimal string encoding a 128-bit unsigned integer.

  • Creates an object from a 25-digit string representation.

    Parameters

    • value: string

    Returns Scru128Id

    SyntaxError if the argument is not a valid string representation.

Other

bytes: Readonly<Uint8Array>

A 16-byte byte array containing the 128-bit unsigned integer representation in the big-endian (network) byte order.

  • get counterHi(): number
  • Returns the 24-bit counter_hi field value.

    Returns number

  • get counterLo(): number
  • Returns the 24-bit counter_lo field value.

    Returns number

  • get entropy(): number
  • Returns the 32-bit entropy field value.

    Returns number

  • get timestamp(): number
  • Returns the 48-bit timestamp field value.

    Returns number

  • Creates an object from this.

    Note that this class is designed to be immutable, and thus clone() is not necessary unless properties marked as private are modified directly.

    Returns Scru128Id

  • Returns a negative integer, zero, or positive integer if this is less than, equal to, or greater than other, respectively.

    Parameters

    Returns number

  • Returns true if this is equivalent to other.

    Parameters

    Returns boolean

  • Represents this in JSON as a 25-digit canonical string.

    Returns string

  • Creates an object from the internal representation, a 16-byte byte array containing the 128-bit unsigned integer representation in the big-endian (network) byte order.

    This method does NOT shallow-copy the argument, and thus the created object holds the reference to the underlying buffer.

    Parameters

    • bytes: Uint8Array

    Returns Scru128Id

    TypeError if the length of the argument is not 16.