Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/avil13/avil13-mapper
A simple TypeScript object mapper on decorators.
https://github.com/avil13/avil13-mapper
Last synced: 19 days ago
JSON representation
A simple TypeScript object mapper on decorators.
- Host: GitHub
- URL: https://github.com/avil13/avil13-mapper
- Owner: avil13
- License: mit
- Created: 2021-05-23T12:55:06.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-12-06T11:18:18.000Z (almost 3 years ago)
- Last Synced: 2024-03-22T22:43:49.149Z (8 months ago)
- Language: JavaScript
- Size: 129 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @avil13/mapper
A simple TypeScript object mapper on decorators.
Used if you need to convert one object to the appearance of another.
![npm bundle size](https://img.shields.io/bundlephobia/minzip/@avil13/mapper)
![npm version](https://img.shields.io/npm/v/@avil13/mapper)## Installation
```sh
yarn add @avil13/mapperOR
npm i @avil13/mapper
```## Example:
```ts
import { MapperItem } from '@avil13/mapper';/* Arrange */
const handler = (data: PassportData): string => `${data.name} ${data.surname}`;
interface PassportData {
dates: {
issueDate: string;
};
name: string;
surname: string;
}// A class that will map properties
class Passport {
private data: PassportData;constructor(data: PassportData) {
this.data = data;
}@MapperItem('data', 'dates.issueDate')
issueDate!: string; // Create new property@MapperItem('data', null, handler)
fullName!: string; // Create new property
}/* Act */
const passport = new Passport({
dates: {
issueDate: '15.04.1452',
},
name: 'Leo',
surname: 'da Vinci',
});/* Assert */
passport.issueDate === '15.04.1452' // true
passport.fullName === 'Leo da Vinci' // true
```That's it, congratulations, things are a little easier now)))
---
And a little bit about the signature
```ts
MapperItem(
// key in target object, if null take the entire object
dataPath: string | null,
// key in target object, if null take the entire object
dataItemPath: string | null,
// function that can change the data before retrieving it from the object
handler?: (item: any) => any,
)
```