tjjfvi/subshape

`$record`

harrysolovay opened this issue · 2 comments

Would it be worthwhile to include a $record codec? Something along the lines of...

function $record<K extends keyof any, V>(key: $.Codec<K>, value: $.Codec<V>) {
  return $.transform<[K, V][], Record<K, V>>({
    $base: $.array($.tuple(key, value)),
    encode: Object.entries as (value: Record<K, V>) => [K, V][],
    decode: Object.fromEntries,
  })
}

Records have many pitfalls (for one, key order isn't strictly insertion-based) – $.map is preferable in most cases.

I'm thinking this would be useful mainly for those wanting to utilize the lib's assertion guard/capabilities (in cases where the shape of the data is already decided).