Huge News!Announcing our $40M Series B led by Abstract Ventures.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 - npm Package Compare versions

Comparing version 0.2.2 to 0.3.0

dist/morphism.js

42

dist/index.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.Morphism = Morphism;
var _lodash = require('lodash');
var _morphism = require('./morphism');
var _ = _interopRequireWildcard(_lodash);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function Morphism(schema, items) {
var _schema = _.clone(schema);
var transformer = function transformer(items) {
return _.chain(items).map(function (obj) {
return _.mapValues(_schema, function (transformation) {
if (!_.isObject(transformation)) {
return _.get(obj, transformation);
} else if (_.isFunction(transformation)) {
var delta = _.get(obj, transformation.path);
return transformation(delta);
} else {
var _delta = _.get(obj, transformation.path);
return transformation.fn(_delta);
}
});
}).value();
};
if (items === undefined) {
return function (items) {
return transformer(items);
};
} else {
return transformer(items);
Object.keys(_morphism).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _morphism[key];
}
}
});
});
{
"name": "morphism",
"version": "0.2.2",
"version": "0.3.0",
"description": "Helps you to transform any object structure to another",

@@ -5,0 +5,0 @@ "homepage": "",

@@ -1,6 +0,11 @@

# 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]
# 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]
> Helps you to transform any object structure to another
## Communication
- Twitter: [@renaudin_yann][twitter-account]
- [GitHub Issues](https://github.com/emyann/morphism/issues)
## Installation
```sh

@@ -10,46 +15,128 @@ $ npm install --save morphism

## Usage
## 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
```js
var morphism = require('morphism');
var Morphism = require('morphism');
let dataToCrunch = [{
'firstName': 'John',
'lastName': 'Smith',
'age': 25,
'address':
{
'city': 'New York',
'state': 'NY',
'postalCode': '10021'
}
}];
let mapping = { ... }
let collectionOfObjects = [ ... ]
let anotherCollection = [ ... ]
let schema = {
// produces a reusable mapper from the configuration
let myAwesomeMapper = Morphism(mapping);
myAwesomeMapper(collectionOfObjects);
myAwesomeMapper(anotherCollection);
```
#### As a Static instance
```js
var Morphism = require('morphism');
let mapping = { ... }
let collectionOfObjects = [ ... ]
// extracts the data straight away
let results = Morphism(mapping, collectionOfObjects);
```
### 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'
}
]
}];
```
### Simple mapping
```js
let data = [ ... ];
let mapping = {
pseudo: 'firstName',
lastName: 'lastName',
city: 'address.city'
state: {
path: 'address.state',
fn: (state) => state.toLowerCase()
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
```js
let data = [ ... ];
let mapping = {
pseudo: 'firstName',
lastName: 'lastName',
city: {
path: 'address.city',
fn: (city) => city.toLowerCase() // compute a function on the specified path value
},
status: () => 'morphed'
};
nbContacts: (object) => object.phoneNumber.length // compute a function on the iteratee object
let results = Morphism(schema, dataToCrunch);
/** results[0]
{
pseudo: 'John',
lastName: 'Smith',
city: 'New York',
state:'ny',
status: 'morphed'
}
*/
};
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
```js
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]()
MIT © [Yann Renaudin][twitter-account]
[twitter-account]: https://twitter.com/renaudin_yann
[npm-image]: https://badge.fury.io/js/morphism.svg

@@ -56,0 +143,0 @@ [npm-url]: https://npmjs.org/package/morphism

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