Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wufe/mapper
Object to object mapper in Typescript - Inspired by Automapper
https://github.com/wufe/mapper
mapper object-to-object typescript
Last synced: about 2 months ago
JSON representation
Object to object mapper in Typescript - Inspired by Automapper
- Host: GitHub
- URL: https://github.com/wufe/mapper
- Owner: wufe
- License: mit
- Created: 2018-01-19T22:53:12.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-25T14:37:00.000Z (over 5 years ago)
- Last Synced: 2024-11-11T21:02:01.301Z (about 2 months ago)
- Topics: mapper, object-to-object, typescript
- Language: TypeScript
- Size: 61.5 KB
- Stars: 28
- Watchers: 7
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Mapper - Object to object mapper in Typescript
**Inspired by [Automapper](https://github.com/automapper/automapper)**
**Warning**: This is *NOT* a complete reimplementation of Automapper.
## What is this
This library provides an API to map an object into another object, following previously set rules.
It is designed with **redux**'s reducers in mind, so it is a perfect fit when used as application's state incremental modifier.
It provides **immutability**, nested mapping capabilities and higly customizable configuration interfaces.Supports:
+ Immutability
+ Nested mapping
+ Redudant mapping
+ Override of pre-existing rules to behave differently in runtime (edge cases)
+ Array mapping***
## How to
### Getting started
**Install**
> `yarn add @wufe/mapper`
or
> `npm i --save @wufe/mapper`
***
**Configuration**
Each map ruleset requires a mapping signature, to be able to identify object types at runtime.
**Create a mapper and instantiate your first map ruleset**
Syntax:
`mapper.createMap(signature: MapSignature, destination: { new(): D })````typescript
const mapper = new Mapper();
const sourceDestinationSignature: MapSignature = {
source: Symbol('SourceClass'),
destination: Symbol('DestinationClass')
};
mapper.createMap(sourceDestinationSignature, DestinationClass);
```**To apply the mapping**
Syntax:
`mapper.map(signature: MapSignature, source: S)````typescript
const sourceObject = new SourceClass();
sourceObject.a = 'a';
const destinationObject = mapper.map(sourceDestinationSignature, sourceObject);
// destinationObject.a is now 'a'
```***
### Map creation without signature
It is possible to create a map rule set without a signature, using the `mapTo` decorator.
Syntax:
```typescript
@mapTo(Destination)
class Source {}
```Example:
```typescript
import { Mapper } from '@wufe/mapper';class Destination {
class Destination {
destGreeting: string;
constructor() {
this.destGreeting = 'destination';
}
}@mapTo(Destination)
class Source {
sourceGreeting: string;
constructor() {
this.sourceGreeting = 'source';
}
}const mapper = new Mapper();
mapper.createMap(Source)
.forMember("destGreeting", opt => opt.mapFrom(src => src.sourceGreeting));const sourceEntity = new Source();
const destinationEntity = mapper.map(sourceEntity);
```***
### Examples
More examples are available in the [tests](https://github.com/Wufe/mapper/tree/master/src/tests) folder.
***
## Documentation
Check the documentation in the [wiki](https://github.com/Wufe/mapper/wiki).