@haraldki/jsonaccess
    Preparing search index...

    Function jsonAsType

    • Applies the given mappers to data to extract a tuple of parameters for calling the factory. If a mapper returns a JsonAccessError, this function immediately returns it. Example:

      type Item = { name: string, price: number };
      const data = ["socks", 3.14]; // assumed result of jsonParse
      const item: Maybe<Item> = jsonAsType(
      data,
      [jsonStringGetter(0), jsonNumberGetter(1)]),
      (n, p) => ({ name: n, price: p })
      );

      This gets even simpler if Item is a class and we can use jsonAsInstance.

      Type Parameters

      • A extends unknown[]
      • C

      Parameters

      • data: unknown

        to pick values from.

      • mappers: { [I in string | number | symbol]: Extractor<A[I]> }

        is a tupe of functions picking values from data or return a JsonAccessError

      • factory: (...args: A) => C

        must take a parameter list with the parameter types those produces by the mappers.

      Returns Maybe<C>

      either a JsonAccessError or the result of calling the factory