Helps you to transform any object structure to another
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.
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.
Morphism is curried function that allows a partial application with a semantic configuration. You can use it in many ways:
// Target type you want to have
class User {
constructor(firstName, lastName, phoneNumber){
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.city = null;
}
}
// Data source you want to map
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'
}
]
}];
// Mapping Schema ( see more examples below )
let schema = {
city: 'adress.city',
phoneNumber: (object) => object.phoneNumber.filter(c => c.type === 'home')[0].number;
}
let mapUser = Morphism.register(User, schema);
// Map using the registered type and the registry
Morphism.map(User, data)
// Or Map using the mapper reference
mapUser(data);
/// *** OUTPUT *** ///
[{
'firstName': 'John',
'lastName': 'Smith',
'phoneNumber': '212 555-1234',
'city': 'New York'
}]
let mapping = { ... }
let collectionOfObjects = [ ... ]
let anotherCollection = [ ... ]
// produces a reusable mapper from the configuration
let myAwesomeMapper = Morphism(mapping);
myAwesomeMapper(collectionOfObjects);
myAwesomeMapper(anotherCollection);
const Morphism = require('morphism');
let mapping = { ... }
let collectionOfObjects = [ ... ]
// extracts the data straight away
let results = Morphism(mapping, collectionOfObjects);
// 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 = [ ... ];
let mapping = {
pseudo: 'firstName', // Simple Projection
lastName: 'lastName',
city: 'address.city' // Flatten a value from a deep path
};
let results = Morphism(mapping, data);
// results[0]: {
// pseudo: 'John',
// lastName: 'Smith',
// city: 'New York'
// }
let data = [ ... ];
let mapping = {
pseudo: 'firstName',
lastName: 'lastName',
city: {
path: 'address.city',
fn: (city) => city.toLowerCase() // compute a function on the specified path value
},
nbContacts: (object) => object.phoneNumber.length // compute a function on the iteratee object
};
let mapper = Morphism(mapping);
let results = mapper(data);
// results[0]): {
// pseudo: 'John',
// lastName: 'Smith',
// city: 'new york',// <== toLowerCase
// nbContacts: 2 // <== computed from the object
// }
let data = [ ... ];
let mapping = {
user: ['firstName','lastName'] // aggregate the values to an object
city: 'address.city'
};
let results = Morphism(mapping, data);
// results[0]: {
// user: {
// 'firstName': 'John',
// 'lastName': 'Smith'
// },
// city: 'New York'
// }
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);
// Map using the registered type and the registry
Morphism.map(User, data)
// Or Map using the mapper reference
mapUser(data);
Register a mapper for a specific type. The schema is optional.
Morphism.register(type, schema:{});
Map a collection of objects to the specified type
Morphism.map(type, data:[]);
Morphism.setMapper(type, schema:{});
Morphism.deleteMapper(type, schema:{});
MIT © Yann Renaudin
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。