@micheldever/mapper

1.0.0 • Public • Published


@micheldever/mapper

Versatile multi-type mapping utility class


git clone https://mtsweb@dev.azure.com/mtsweb/oss/_git/mapper

Installation

This package is distributed via npm. You can install it as a dependency in your project by running:

yarn add @micheldever/mapper

Usage

After defining your mapping profiles and creating a new instance of the Mapper, you can start mapping your data using either the map or mapMany methods:

import { Mapper } from '@micheldever/mapper';

const profiles = [
  {
    egress: 'number',
    ingress: 'string',
    map: (value: string) => Number.parseInt(value),
  },
];

const mapper = new Mapper(profiles);

// Map a single ingress value
mapper.map('string', '1', 'number');

// Map multiple ingress values
mapper.mapMany('string', ['1', '2'], 'number');

TypeScript

Mapper works best with TypeScript. For type safety when using mapping methods, pass the type of your profiles array as a generic argument when creating a new Mapper instance. Ensure the profiles array is a readonly array that conforms to the Profile[] interface.

Please note that the satisfies operator requires Typescript v4.9+.

import { Mapper } from '@micheldever/mapper';
import type { Profile } from '@micheldever/mapper';

const profiles = [
  {
    egress: 'number',
    ingress: 'string',
    map: (value: string) => Number.parseInt(value),
  },
] as const satisfies Profile[];

const mapper = new Mapper<typeof profiles>(profiles);

// Argument of type "string" is not assignable to parameter of type "number"
mapper.map('string', '1', 'string');

Readme

Keywords

none

Package Sidebar

Install

npm i @micheldever/mapper

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

9.12 kB

Total Files

5

Last publish

Collaborators

  • hwll