Morphism
Helps you to transform any object structure to another
Contribution
Getting started 🚀
Install morphism
using npm.
npm install --save morphism
Then require it into any module.
const Morphism = require('morphism');
Or using ES6 import style
import Morphism from 'morphism';
If you're using browserify, 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
class User {
constructor(firstName, lastName, phoneNumber){
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.city = null;
}
}
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 schema = {
city: 'adress.city',
phoneNumber: (object) => object.phoneNumber.filter(c => c.type === 'home')[0].number;
}
let mapUser = Morphism.register(User, schema);
Morphism.map(User, data)
mapUser(data);
[{
'firstName': 'John',
'lastName': 'Smith',
'phoneNumber': '212 555-1234',
'city': 'New York'
}]
As a Mapper factory
let mapping = { ... }
let collectionOfObjects = [ ... ]
let anotherCollection = [ ... ]
let myAwesomeMapper = Morphism(mapping);
myAwesomeMapper(collectionOfObjects);
myAwesomeMapper(anotherCollection);
As a Static instance
const Morphism = require('morphism');
let mapping = { ... }
let collectionOfObjects = [ ... ]
let results = Morphism(mapping, collectionOfObjects);
Mapping Schema Examples 💡
Dataset sample
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
let data = [ ... ];
let mapping = {
pseudo: 'firstName',
lastName: 'lastName',
city: 'address.city'
};
let results = Morphism(mapping, data);
Computing over Flattening / Projection
let data = [ ... ];
let mapping = {
pseudo: 'firstName',
lastName: 'lastName',
city: {
path: 'address.city',
fn: (city) => city.toLowerCase()
},
nbContacts: (object) => object.phoneNumber.length
};
let mapper = Morphism(mapping);
let results = mapper(data);
Values Aggregation
let data = [ ... ];
let mapping = {
user: ['firstName','lastName']
city: 'address.city'
};
let results = Morphism(mapping, data);
Mappers Registry 📚
Morphism provides a powerful local registry where you can store your mappings' configuration by specifying a Class Type.
The transformation sequences are stored as a function in a WeakMap to speed the processing.
Register a mapping configuration along with a Class
class User {
constructor(firstName, lastName, phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
}
let mapUser = Morphism.register(User, schema);
Morphism.map(User, data);
mapUser(data);
API 📚
Register
Register a mapper for a specific type. The schema is optional.
Morphism.register(type, (schema: {}));
Map
Map a collection of objects to the specified type
Morphism.map(type, (data: []));
Get or Set an existing mapper configuration
Morphism.setMapper(type, (schema: {}));
Delete a registered mapper
Morphism.deleteMapper(type, (schema: {}));
License
MIT © Yann Renaudin