node-json-transform
Advanced tools
Comparing version 1.0.14 to 1.0.15
38
index.js
@@ -10,8 +10,3 @@ // DataTransform | ||
defaultOrNull: function(key) { | ||
console.log("Use default!"); | ||
let value = key && map.defaults ? map.defaults[key] : null; | ||
console.log("default", map.defaults); | ||
console.log("key", key); | ||
console.log("value", value); | ||
return value; | ||
return key && map.defaults ? map.defaults[key] : null; | ||
}, | ||
@@ -81,3 +76,3 @@ | ||
transform : function() { | ||
transform : function(context) { | ||
@@ -90,5 +85,5 @@ var value = this.getValue(data, map.list), | ||
var normalized = map.item ? _.map(list, _.bind(this.iterator, this, map.item)) : list; | ||
normalized = _.bind(this.operate, this, normalized)(); | ||
normalized = this.each(normalized); | ||
normalized = _.each(normalized, this.remove); | ||
normalized = _.bind(this.operate, this, normalized)(context); | ||
normalized = this.each(normalized, context); | ||
normalized = this.removeAll(normalized); | ||
} | ||
@@ -100,12 +95,17 @@ | ||
removeAll: function(data){ | ||
if (_.isArray(map.remove)) { | ||
return _.each(data, this.remove) | ||
} | ||
return data; | ||
}, | ||
remove: function(item){ | ||
if( _.isArray(map.remove) ) { | ||
_.each(map.remove, (key)=>{ | ||
delete item[key]; | ||
}); | ||
} | ||
_.each(map.remove, (key)=>{ | ||
delete item[key]; | ||
}); | ||
return item; | ||
}, | ||
operate: function(data) { | ||
operate: function(data, context) { | ||
@@ -121,3 +121,3 @@ if(map.operate) { | ||
} | ||
this.setValue(item,method.on,fn(this.getValue(item,method.on))) | ||
this.setValue(item,method.on,fn(this.getValue(item,method.on), context)) | ||
return item; | ||
@@ -131,5 +131,5 @@ },this)); | ||
each: function(data){ | ||
each: function(data, context){ | ||
if( map.each ) { | ||
_.each(data, map.each); | ||
_.each(data, (value, index, collection) => map.each(value, index, collection, context)); | ||
} | ||
@@ -136,0 +136,0 @@ return data; |
{ | ||
"name": "node-json-transform", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "A node module for transforming and performing operations on JSON.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -278,5 +278,68 @@ # node-data-transform | ||
### Context Example | ||
```javascript | ||
var DataTransform = require("node-json-transform").DataTransform; | ||
``` | ||
First we need some data. | ||
```javascript | ||
var data = { | ||
posts : [ | ||
{ | ||
title : "title1", | ||
description: "description1" | ||
} | ||
] | ||
}; | ||
``` | ||
The map defines how the output will be structured and which operations to run. | ||
```javascript | ||
var map = { | ||
list : 'posts', | ||
item: { | ||
name: "title", | ||
info: "description" | ||
}, | ||
operate: [ | ||
{ | ||
run: function(val, context) { return val + " more info for" + context.type}, | ||
on: "info" | ||
} | ||
], | ||
each: function(item, index, collection, context){ | ||
// make changes | ||
item.type = context.type; | ||
return item; | ||
} | ||
}; | ||
``` | ||
Run it | ||
```javascript | ||
var dataTransform = DataTransform(data, map); | ||
var context = { type: 'my-type' }; | ||
var result = dataTransform.transform(context); | ||
console.log(result); | ||
``` | ||
The expected output. | ||
```javascript | ||
[ | ||
{ | ||
name : "title1", | ||
info: "description1 more info for my-type", | ||
type: 'my-type' | ||
} | ||
] | ||
``` | ||
Enjoy! | ||
## Changelog | ||
1.0.15 Add support for a context object that is passed through to the operate.run and each functions. | ||
1.0.14 Add support for default values via "defaults" definition. Add support for removing attributes via the "remove" definition. | ||
@@ -283,0 +346,0 @@ 1.0.13 Update code examples. |
@@ -64,3 +64,3 @@ var DataTransform = require('../index.js').DataTransform, | ||
text: "This is a blog.", | ||
date: 1383544800000, | ||
date: Date.parse('11/4/2013'), | ||
link: "http://goo.cm", | ||
@@ -85,3 +85,3 @@ info: "mike" | ||
text: "This is a blog.", | ||
date: 1383544800000, | ||
date: Date.parse('11/4/2013'), | ||
link: "http://goo.cm", | ||
@@ -106,3 +106,3 @@ info: "mike", | ||
text: "This is a blog.", | ||
date: 1383544800000, | ||
date: Date.parse('11/4/2013'), | ||
link: "http://goo.cm", | ||
@@ -109,0 +109,0 @@ info: "mike", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24053
9
643
383