Grounds
    Preparing search index...

    Function Map_

    • Creates a homogeneous map Relish value.

      The name has an underscore suffix to avoid shadowing the global Map object. All keys and values must match their respective types. For string keys, accepts either Record<string, V> or Map for convenience. For non-string keys, requires a native JavaScript Map.

      Type Parameters

      Parameters

      • keyType: K

        TypeCode specifying the type of all keys

      • valueType: V

        TypeCode specifying the type of all values

      • input: MapInput<TypeCodeToJsType<K>, TypeCodeToJsType<V>>

        Either a Record<string, V> (for string keys) or Map<K, V>

      Returns RelishMap<K, V>

      RelishMap value

      Error if any key or value does not match the expected type

      import { Map_, String_, U32, encode } from '@grounds/core';

      // Map with string keys and 32-bit integer values
      const scores = Map_(
      0x0e, // TypeCode.String
      0x04, // TypeCode.U32
      { alice: 100, bob: 95 }
      );
      encode(scores).match(
      (bytes) => console.log('Map encoded:', bytes),
      (error) => console.error(error)
      );

      Named Map_ (with trailing underscore) to avoid shadowing the global Map type. For string keys, pass a plain object for ergonomics. For other key types, use a Map. Type validation is performed at runtime.

      • Array_ for homogeneous collections without keys
      • Struct for fixed fields