@micheldever/mapper
v1.0.0
Published
Versatile multi-type mapping utility class
Downloads
25
Readme
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');