New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

morphism

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

morphism

Helps you to transform any object structure to another

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
decreased by-6.69%
Maintainers
1
Weekly downloads
 
Created
Source

Morphism

NPM version Build Status Dependency Status Coverage percentage

Helps you to transform any object structure to another

Communication

Installation

$ npm install --save morphism

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 2 ways:

As a Mapper factory
var Morphism = require('morphism');

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

// produces a reusable mapper from the configuration
let myAwesomeMapper = Morphism(mapping);
myAwesomeMapper(collectionOfObjects);
myAwesomeMapper(anotherCollection);
As a Static instance
var Morphism = require('morphism');

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

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

Dataset sample

// 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'
                        }
                    ]
                }];

Simple mapping

let data = [ ... ];
let mapping = { 
                pseudo: 'firstName',
                lastName: 'lastName',
                city: 'address.city' // get a value from a deep path
               };

let results = Morphism(mapping, data);

console.log(results[0]) ==>  
        {
            pseudo: 'John',
            lastName: 'Smith',
            city: 'New York'
        }

Value transformation

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);

console.log(results[0]) ==>  
        {
            pseudo: 'John',
            lastName: 'Smith',
            city: 'new york',// <== toLowerCase
            nbContacts: 2 // <== computed from the object
        }

Values Aggregation

let data = [ ... ];

let mapping = { 
                user: ['firstName','lastName'] // aggregate the values to an object
                city: 'address.city'
               };

let results = Morphism(mapping, data);

console.log(results[0]) ==>  
        {
            user: {
                'firstName': 'John',
                'lastName': 'Smith'
            },
            city: 'New York'
        }

License

MIT © Yann Renaudin

Keywords

FAQs

Package last updated on 29 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc