Skip to content

Commit

Permalink
Merge f66c934 into 4ff1c38
Browse files Browse the repository at this point in the history
  • Loading branch information
emyann committed Apr 29, 2018
2 parents 4ff1c38 + f66c934 commit 3849c4f
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 91 deletions.
96 changes: 50 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Morphism

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]

[![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/morphism.svg?style=for-the-badge)](https://bundlephobia.com/result?p=morphism)

> Helps you to transform any object structure to another
## Contribution
## Contribution

- Twitter: [@renaudin_yann][twitter-account]
- Pull requests and stars are always welcome 🙏🏽 For bugs and feature requests, [please create an issue](https://github.com/emyann/morphism/issues)
* Twitter: [@renaudin_yann][twitter-account]
* Pull requests and stars are always welcome 🙏🏽 For bugs and feature requests, [please create an issue](https://github.com/emyann/morphism/issues)

## Getting started 🚀
## Getting started 🚀

Install `morphism` using npm.

Expand All @@ -19,7 +22,7 @@ npm install --save morphism
Then require it into any module.

```js
const Morphism = require('morphism');
const Morphism = require('morphism');
```

Or using ES6 import style
Expand All @@ -31,15 +34,16 @@ import Morphism from 'morphism';
If you're using [browserify](http://browserify.org/), the `morphism` npm module
also works from the browser.


## What does it do? 🤔

Morphism uses a semantic configuration to go through the collection of graph objects you have to process. Then it extracts and computes the value from the specified path(s). Finally, it sets this value to the destination property from the schema.

## Usage 🍔

Morphism is curried function that allows a partial application with a semantic configuration. You can use it in many ways:

### Along with an ES6 Class

```js
// Target type you want to have
class User {
Expand Down Expand Up @@ -97,8 +101,8 @@ mapUser(data);
```

### As a Mapper factory
```js

```js
let mapping = { ... }
let collectionOfObjects = [ ... ]
let anotherCollection = [ ... ]
Expand All @@ -110,47 +114,50 @@ myAwesomeMapper(anotherCollection);
```

### As a Static instance

```js
const Morphism = require('morphism');
const Morphism = require('morphism');

let mapping = { ... }
let collectionOfObjects = [ ... ]

// extracts the data straight away
// extracts the data straight away
let results = Morphism(mapping, collectionOfObjects);
```

## Mapping Schema Examples 💡

### Dataset sample

```js
// We'll use this set of data all along the examples
let data = [{
'firstName': 'John',
'lastName': 'Smith',
'address':
{
'city': 'New York',
'country': 'USA'
},
'phoneNumber':
[
{
'type': 'home',
'number': '212 555-1234'
},
{
'type': 'fax',
'number': '646 555-4567'
}
]
}];
let data = [
{
firstName: 'John',
lastName: 'Smith',
address: {
city: 'New York',
country: 'USA'
},
phoneNumber: [
{
type: 'home',
number: '212 555-1234'
},
{
type: 'fax',
number: '646 555-4567'
}
]
}
];
```


### Flattening and Projection

```js
let data = [ ... ];
let mapping = {
let mapping = {
pseudo: 'firstName', // Simple Projection
lastName: 'lastName',
city: 'address.city' // Flatten a value from a deep path
Expand All @@ -165,12 +172,11 @@ let results = Morphism(mapping, data);
// }
```


### Computing over Flattening / Projection

```js
let data = [ ... ];
let mapping = {
let mapping = {
pseudo: 'firstName',
lastName: 'lastName',
city: {
Expand All @@ -192,7 +198,6 @@ let results = mapper(data);
// }
```


### Values Aggregation

```js
Expand Down Expand Up @@ -220,19 +225,20 @@ Morphism provides a powerful local registry where you can store your mappings' c
The transformation sequences are stored as a function in a WeakMap to speed the processing.

**Register a mapping configuration along with a Class**

```js
class User {
constructor(firstName, lastName, phoneNumber){
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
constructor(firstName, lastName, phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
}

let mapUser = Morphism.register(User, schema);

// Map using the registered type and the registry
Morphism.map(User, data)
Morphism.map(User, data);

// Or Map using the mapper reference
mapUser(data);
Expand All @@ -245,31 +251,29 @@ mapUser(data);
Register a mapper for a specific type. The schema is optional.

```js
Morphism.register(type, schema:{});
Morphism.register(type, (schema: {}));
```

#### Map

Map a collection of objects to the specified type

```js
Morphism.map(type, data:[]);
Morphism.map(type, (data: []));
```

#### Get or Set an existing mapper configuration

```js
Morphism.setMapper(type, schema:{});
Morphism.setMapper(type, (schema: {}));
```

#### Delete a registered mapper

```js
Morphism.deleteMapper(type, schema:{});
Morphism.deleteMapper(type, (schema: {}));
```



## License

MIT © [Yann Renaudin][twitter-account]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "morphism",
"version": "0.6.1",
"version": "0.7.0",
"description": "Helps you to transform any object structure to another",
"homepage": "https://emyann.github.io/morphism/",
"scripts": {
Expand Down

0 comments on commit 3849c4f

Please sign in to comment.