multiformats / multiformats/js-multiformats

How to access `.or` prop of decoders

Open
#141 7 comments 0 reactions 1 assignee View on GitHub

@Gozala is already working on this.

Since Dec 14, 2021.

Dominant language
TypeScript
Stars
268
Forks
57
PR merge metrics
No merged PRs in 30d

Description

I'd like to compose a decoder, something like:

import * as b32 from 'multiformats/bases/base32'
import * as b36 from 'multiformats/bases/base36'
import * as b58 from 'multiformats/bases/base58'
import * as b64 from 'multiformats/bases/base64'
import { base32 } from 'multiformats/bases/base32'

const bases:Record<string, MultibaseCodec<any>> = {
  ...b32,
  ...b36,
  ...b58,
  ...b64
}

const baseDecoder = Object
  .keys(bases)
  .map(key => bases[key].decoder)
  .reduce(
    (acc, curr) => acc.or(curr),  // <--- fails because `.or` is not in the types though it is in the `Decoder` class
    base32.decoder
  )

It's not clear to me how I'm supposed to do this and not upset the type checker, any pointers?

I tried adding .or to the MultibaseDecoder type def but it explodes because elsewhere you need to know if you're being passed a UnibaseDecoder or a CombobaseDecoder to create a ComposedDecoder - I started trying to fix it but it got a little out of hand so I thought I'd ask instead.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.