Vertex Viewer SDK
    Preparing search index...

    Function defineMapper

    • Defines a mapper that reads the values from an input and invokes a builder to transform data from one schema to another.

      Type Parameters

      • T
      • V
      • R

      Parameters

      • reader: Func<T, V>

        The mapper that reads values from the input an creates an intermediate format that will be passed to the builder.

      • builder: Func<V, R>

        A mapper that takes the output of reader and constructs the output format.

      Returns Func<T, R>

      import { Mapper as M } from '@vertexvis/utils';

      interface Address {
      address: string;
      city: string;
      state: string;
      zip: string;
      }

      interface Person {
      name: string;
      addresses: Address[];
      }

      type AddressJson = Partial<Address>;
      type PersonJson = {
      name?: string;
      addresses?: AddressJson[];
      }

      const mapAddress: M.Func<AddressJson, Address> = M.defineMapper(
      M.read(
      M.requireProp('address'),
      M.requireProp('city'),
      M.requireProp('state'),
      M.requireProp('zip')
      ),
      ([address, city, state, zip]) => ({
      address, city, state, zip
      })
      );

      const mapPerson: M.Func<PersonJson, Person> = M.defineMapper(
      M.read(
      M.requireProp('name'),
      M.mapProp(
      'addresses',
      M.compose(M.required('addresses'), M.mapArray(mapAddress))
      )
      ),
      ([name, addresses]) => ({ name, addresses })
      )

      const person = mapPerson({
      name: 'John',
      addresses: [{ address: '123', city: 'Ames', state: 'IA', zip: '50010' }]
      })

      read - a helper function to read and validate input values.