Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SingletonType<E>

A type storing a fixed value. The value takes up no space in the value bytes, only the type bytes. Functions as an EnumType with only one value.

Example:

//Encodes a JSON literal value
let type = new sb.ChoiceType([
  new sb.StructType({
    type: new sb.SingletonType({
      type: new sb.StringType,
      value: 'boolean'
    }),
    value: new sb.BooleanType
  }),
  new sb.StructType({
    type: new sb.SingletonType({
      type: new sb.StringType,
      value: 'number'
    }),
    value: new sb.DoubleType
  }),
  new sb.StructType({
    type: new sb.SingletonType({
      type: new sb.StringType,
      value: 'string'
    }),
    value: new sb.StringType
  })
])

Type parameters

  • E

    The type of the value

Hierarchy

  • AbstractType<E>
    • SingletonType

Index

Constructors

constructor

Properties

Private cachedValueBuffer

cachedValueBuffer: undefined | ArrayBuffer

Readonly type

type: Type<E, E>

The type used to serialize the value

Readonly value

value: E

The value that this type serializes

Accessors

Private singletonValueBuffer

  • get singletonValueBuffer(): ArrayBuffer

Static _value

  • get _value(): number

Methods

addToBuffer

consumeValue

  • consumeValue(): ReadResult<E>

equals

  • equals(otherType: unknown): boolean
  • Parameters

    • otherType: unknown

    Returns boolean

getHash

  • getHash(): string

getSignature

  • getSignature(): string

Private isBuffer

Private isSameType

  • Determines whether the input is a Type with the same class

    Parameters

    • otherType: unknown

      A value, usually a Type instance

    Returns otherType is SingletonType<E>

    whether this and otherType are instances of the same Type class

readValue

  • readValue(valueBuffer: ArrayBuffer | Uint8Array, offset?: number): E
  • Parameters

    • valueBuffer: ArrayBuffer | Uint8Array
    • offset: number = 0

    Returns E

toBuffer

  • toBuffer(): ArrayBuffer

valueBuffer

  • valueBuffer(value: E): ArrayBuffer
  • Parameters

    • value: E

    Returns ArrayBuffer

writeValue

  • Appends value bytes to an AppendableBuffer according to the type

    Example:

    type.writeValue(buffer, {type: 'boolean', value: true})
    type.writeValue(buffer, {type: 'string', value: 'abc'})
    
    throws

    If the value doesn't match the type, e.g. new sb.StringType().writeValue(buffer, 23)

    Parameters

    • buffer: AppendableBuffer

      The buffer to which to append

    • value: E

      The value to write

    Returns void

Generated using TypeDoc