https://github.com/w3tecch/class-mapper
A easy to use way to map any ugly backend structures into clean TypeScript/ES6 models
https://github.com/w3tecch/class-mapper
annotations clean decorator es6 javascipt library mapping model structure typescript
Last synced: about 1 year ago
JSON representation
A easy to use way to map any ugly backend structures into clean TypeScript/ES6 models
- Host: GitHub
- URL: https://github.com/w3tecch/class-mapper
- Owner: w3tecch
- License: mit
- Created: 2018-05-04T20:49:49.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-31T12:56:33.000Z (over 7 years ago)
- Last Synced: 2025-04-08T11:38:51.567Z (about 1 year ago)
- Topics: annotations, clean, decorator, es6, javascipt, library, mapping, model, structure, typescript
- Language: TypeScript
- Homepage:
- Size: 752 KB
- Stars: 10
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Class Mapper
A easy to use way to map any ugly backend structures into clean TypeScript/ES6 models.
Inspired typestack class-transformer
Made with ❤️ by w3tech

## ❯ Why
Are you tired of ugly and weird backend structures which makes it challenging to work with in your application 🤯? Here comes a solution 🎉! Just use class-mapper to map all weird structures into TypeScript and ES6 models 👉 YOU 🤗 like to work with in your web frontend or Node.js application and not backend guys 🤪.
Try it!! We are happy to hear your feedback or any kind of new features.

## ❯ Table of Contents
- [Installation](#-installation)
- [License](#-license)
- [Methods](#-methods)

## ❯ Installation
### Step 1: Get library via npm or yarn
```shell
npm install class-mapper --save
```
or with yarn
```shell
yarn add class-mapper
```
### Step 2: Add library to your project
#### Browser
```html
```
## ❯ Methods
### mapClasses
This method maps a source class to your target class
```typescript
import {mapClasses, MapFromSource, PropertyType} from 'class-mapper';
/**
* Source classes
*/
abstract class SourcePersonModel {
public name1: string;
public name2: string;
}
class SourceCarModel {
public attribute1: string;
public attribute2: string;
}
class SourceCustomerModel extends SourcePersonModel {
public car1!: SourceCarModel[];
}
const sourceUser: SourceCustomerModel = new SourceCustomerModel();
/**
* Target classes
*/
abstract class TargetPersonModel {
@MapFromSource((sourceUser: SourcePersonModel) => sourceUser.name1)
public firstName!: string;
@MapFromSource((sourceUser: SourcePersonModel) => sourceUser.name2)
public lastName!: string;
}
class TargetCarModel {
@MapFromSource((sourceCar: SourceCarModel) => sourceCar.attribute1)
public manufacturer!: string;
@MapFromSource(sourceCar: SourceCarModel) => sourceCar.attribute2)
public model!: string;
}
class TargetCustomerModel extends TargetPersonModel {
@PropertyType(TargetCarModel)
@MapFromSource((sourceUser: SourcePersonModel) => sourceUser.car1)
public cars!: TargetCarModel[];
}
const targetUser: TargetCustomerModel = mapClasses(TargetCustomerModel, sourceUser);
```
### Using `groups` to exclude properties
With `groups` array, you can exclude properties from mapping. `MapFromSource` decorators with no `groups` option will always be mapped.
```typescript
import {mapClasses, MapFromSource, PropertyType} from 'class-mapper';
/**
* Source class
*/
abstract class SourcePersonModel {
public name1: string;
public name1: string;
}
/**
* Target class
*/
const firstNameOnly = 'first-name-only';
const lastNameOnly = 'last-name-only';
abstract class TargetPersonModel {
@MapFromSource((sourceUser: SourcePersonModel) => sourceUser.name1, { groups: [firstNameOnly] })
public firstName!: string;
@MapFromSource((sourceUser: SourcePersonModel) => sourceUser.name2, { groups: [lastNameOnly] })
public lastName!: string;
}
const targetUser: TargetCustomerModel = mapClasses(TargetCustomerModel, sourceUser, { groups: [lastNameOnly] });
```
### Using `enabled` to exclude properties
With `enabled` you can exclude conditionally properties. `MapFromSource` decorators with no `enabled` option will always be mapped.
```typescript
import {mapClasses, MapFromSource, PropertyType} from 'class-mapper';
/**
* Source class
*/
abstract class SourcePersonModel {
public name1: string;
public name1: string;
}
/**
* Target class
*/
abstract class TargetPersonModel {
@MapFromSource((sourceUser: SourcePersonModel) => sourceUser.name1, { enabled: (sourceUser: SourcePersonModel) => !!sourceUser.name1 })
public firstName!: string;
@MapFromSource(sourceUser => sourceUser.name2, { enabled: (sourceUser: SourcePersonModel) => !!sourceUser.name2 })
public lastName!: string;
}
const targetUser: TargetCustomerModel = mapClasses(TargetCustomerModel, sourceUser);
```
## ❯ License
[MIT](/LICENSE)