Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bimedia-fr/bimedia-objectmapper
object mapper for node
https://github.com/bimedia-fr/bimedia-objectmapper
Last synced: 8 days ago
JSON representation
object mapper for node
- Host: GitHub
- URL: https://github.com/bimedia-fr/bimedia-objectmapper
- Owner: bimedia-fr
- License: apache-2.0
- Created: 2014-03-03T11:22:21.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2022-08-25T14:10:56.000Z (over 2 years ago)
- Last Synced: 2024-12-17T08:14:18.655Z (19 days ago)
- Language: JavaScript
- Size: 50.8 KB
- Stars: 0
- Watchers: 11
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
bimedia-objectmapper [![build status](https://secure.travis-ci.org/bimedia-fr/node-object-mapper.png)](http://travis-ci.org/bimedia-fr/node-object-mapper)
==================object mapper for node
Object Mapper Module
-----------------This module allow to map keys and values from an object to an other. Mapping uses a tranformation rules.
There are 3 transformation rules types :
* *identity* : copy the attribute and value to the target object.
* *alias* : rename the attribute in the target object (value is unchanged).
* *complex* : apply tranformation rule provided to change attribute name and value.### Installation
`npm install --save bimedia-objectmapper`
### Usage
#### Simple usecase
```javascript
var ObjectMapper = require('bimedia-objectmapper');
var mapper = objectMapper({'key':'clé'});
var result = mapper({key:'value'});
// {'clé': 'value'}
```#### Streams
```javascript
var objectMapper = require('bimedia-objectmapper'),
fs = require('fs'),
JSONStream = require('JSONStream');
var mapper = objectMapper(rules);
fs.createReadStream('package.json')
.pipe(JSONStream.parse())
.pipe(mapper.stream())
.pipe(process.stdout);
```#### Arrays
```javascript
var objectMapper = require('bimedia-objectmapper');
var mapper = objectMapper({'key':'clé'});
var sources = [{key:'value1'}, {key:'value2'}, {key:'value3'}];
var result = sources.map(mapper);
//[ { 'clé': 'value1' },
// { 'clé': 'value2' },
// { 'clé': 'value3' } ]
```### Mapping rules definitions
#### Identity Rule
No specific rule is needed. Attribute is copied to target object.
#### Simple Rule (alias)
Declare a constant as attribute value. Example :
```javascript
var rules = {
"trxpvptpv": "trxid"
};
```
This rule renames *trxpvptpv* attribute in source object, to *trxid*
in target object.#### Complex Rule
Define an object matching the source attribute.
Example :
```javascript
var rules = {
'state' : {
name : 'currentState',
mapper : function (val) {
if (val == 'Y') {
return 'REFUNDED';
}
return val == 'N' ? 'REFUSED' : 'PENDING';
}
}
};
```
Or directly with a function :
```javascript
var rules = {
'state' : function (k, v) {
var value;
if (val == 'Y') {
value = 'REFUNDED';
}
value = val == 'N' ? 'REFUSED' : 'PENDING';
return {key: 'currentState', value: value};
}
}
};
```
This 2 examples transforms the *state* attribute in source object to an *currentState*
attribute in target object.### Options
Mapping can be customize with the second argmuent of `ObjectMapper` :
For instance to disable identity mapping by default :```js
var mapper = ObjectMapper(rules, {defaults: false});
```Or to specify a default mapping function :
```js
var mapper = ObjectMapper(rules, {defaults: ObjectMapper.keys.camelCase});
```
#### supported options
* `defaults` : (function | boolean) define a default mapping function. By defaut `ObjectMapper.identity`.