@txmo-dev/midas-mapper
v1.0.3
Published
TypeScript mirror of Midas.Mapper — explicit, type-safe object mapping utilities for the Midas HMS ecosystem.
Maintainers
Readme
@txmo-dev/midas-mapper
TypeScript mirror of Midas.Mapper — explicit, type-safe object mapping utilities for the Midas HMS ecosystem.
No magic, no reflection, no decorators. Mappers are plain functions or classes — fully tree-shakeable and debuggable.
Installation
npm install @txmo-dev/midas-mapperUsage
Functional style (recommended)
import { createMapper, createBidirectionalMapper, mapList } from '@txmo-dev/midas-mapper';
interface Patient { id: string; firstName: string; lastName: string; }
interface PatientDto { id: string; fullName: string; }
const patientMapper = createMapper<Patient, PatientDto>((p) => ({
id: p.id,
fullName: `${p.firstName} ${p.lastName}`,
}));
const dto = patientMapper.map(patient);
const dtos = mapList(patientMapper, patients);Bidirectional mapper
const mapper = createBidirectionalMapper<Patient, PatientDto>(
(p) => ({ id: p.id, fullName: `${p.firstName} ${p.lastName}` }),
(dto) => ({ id: dto.id, firstName: dto.fullName.split(' ')[0], lastName: dto.fullName.split(' ')[1] }),
);
const dto = mapper.map(patient);
const patient = mapper.reverseMap(dto);Registry (class-based, mirrors C# MapperRegistry)
import { MapperRegistry } from '@txmo-dev/midas-mapper';
const registry = new MapperRegistry();
registry.register('Patient', 'PatientDto', patientMapper);
const dto = registry.map<Patient, PatientDto>('Patient', 'PatientDto', patient);
const dtos = registry.mapList<Patient, PatientDto>('Patient', 'PatientDto', patients);API
| Export | Description |
|--------|-------------|
| createMapper<S, D>(fn) | Create a one-way mapper from a function |
| createBidirectionalMapper<S, D>(mapFn, reverseMapFn) | Create a two-way mapper |
| mapList(mapper, sources) | Map an array using a mapper instance |
| MapperRegistry | Central registry for named mappers |
| IMapper<S, D> | Interface for one-way mappers |
| IBidirectionalMapper<S, D> | Interface for two-way mappers |
