Morphit
Simple utility to morph an object to another one.
const morphit = ;
Basic Usage
Basic morphit function transforms an object to another object, mapping values with dot notation.
const morphit = ; // object to morphconst obj = id: 1 first_name: 'morph' last_name: 'it' street: 'Champs Élysées' post_code: '75000' country: 'FR' id_offer: 2 orders: id: 1 id_products: 1 id_invoice: 1 id_payment: 1 date: created: '2017-01-01' updated: '2017-01-02' ; // morphingconst morph = ; console;/*{ user: { id: 1, firstName: 'morph', lastName: 'it', idOffer: 2, creationDate: '2017-01-01', updateDate: '2017-01-02', }, address: { street: 'Champs Élysées', postCode: '75000', country: 'FR' }, orders: [ { id: 1, id_products: 1, id_invoice: 1, id_payment: 1 } ], _morphed: true}*/
Array
You can morph arrays of objects
const morphit = ; const arr = first_name: 'John' last_name: 'Doe' ; const morph = ; console;/*[ { firstName: 'John', lastName: 'Doe' } ]*/
Nested array morphing
You can morph an array nested in an morphing using morphit.each
.
const morphit = ; const obj = users_info: first_name: 'John' last_name: 'Doe' first_name: 'Foo' last_name: 'Bar' ; const morph = ; console;/*{ users: [ { firstName: 'John', lastName: 'Doe' }, { firstName: 'Foo', lastName: 'Bar' } ] }*/
Morph transform data
Morphit provides a way to transform the value before mapping to the morphed object;
You can run async as long as you return a Promise
.
When adding transformations, morphit will return a Promise
, you must use .then
.
If you add a _concurrency
to the morph object, promises from transformations will be run in a pool of size 4, if no concurrency is provided, all transformations will be run synchronously.
const obj = orders: id: 1 id_product: 1 id_invoice: 1 id_payment: 1 id_user: 1; const getSomeProduct = { return Promise;}; const getSomeInvoice = { return Promise;}; const getSomePayment = { return Promise;}; const getSomeUser = { return Promise;}; const morph = ;