node-json-transform
Advanced tools
Comparing version 1.0.10 to 1.0.11
65
index.js
@@ -28,3 +28,3 @@ // DataTransform | ||
if(typeof(value) !== "undefined" && | ||
value[keys[i]]) { | ||
keys[i] in value) { | ||
value = value[keys[i]]; | ||
@@ -41,2 +41,29 @@ } else { | ||
setValue : function(obj, key, newValue) { | ||
if(typeof(obj) == "undefined") { | ||
return; | ||
} | ||
if(key == '' || key == undefined) { | ||
return; | ||
} | ||
if(key == "") { | ||
return; | ||
} | ||
keys = key.split('.'); | ||
var target = obj; | ||
for(var i = 0; i < keys.length; i++ ) { | ||
if(i == keys.length-1){ | ||
target[keys[i]] = newValue; | ||
return; | ||
} | ||
if(keys[i] in target) | ||
target = target[keys[i]]; | ||
else return; | ||
} | ||
}, | ||
getList: function(){ | ||
@@ -52,4 +79,4 @@ return this.getValue(data, map.list); | ||
var list = this.getList(); | ||
var normalized = map.item ? _.map(list, _.bind(this.iterator, this)) : list; | ||
normalized = this.operate(normalized); | ||
var normalized = map.item ? _.map(list, _.bind(this.iterator, this, map.item)) : list; | ||
normalized = _.bind(this.operate, this, normalized)(); | ||
normalized = this.each(normalized); | ||
@@ -64,4 +91,4 @@ } | ||
if(map.operate) { | ||
_.each(map.operate, function(method){ | ||
data = _.map(data, function(item){ | ||
_.each(map.operate, _.bind(function(method){ | ||
data = _.map(data, _.bind(function(item){ | ||
var fn; | ||
@@ -73,6 +100,6 @@ if( 'string' === typeof method.run ) { | ||
} | ||
item[method.on] = fn(item[method.on]); | ||
this.setValue(item,method.on,fn(this.getValue(item,method.on))) | ||
return item; | ||
}); | ||
}); | ||
},this)); | ||
},this)); | ||
} | ||
@@ -90,17 +117,21 @@ return data; | ||
iterator : function(item) { | ||
iterator : function(map, item) { | ||
var obj = {}; | ||
_.each(map.item, _.bind(function(oldkey, newkey) { | ||
//to support simple arrays with recursion | ||
if(typeof(map) == "string") { | ||
return this.getValue(item, map); | ||
} | ||
_.each(map, _.bind(function(oldkey, newkey) { | ||
if(typeof(oldkey) == "string" && oldkey.length > 0) { | ||
obj[newkey] = this.getValue(item, oldkey); | ||
} else if( _.isArray(oldkey) ) { | ||
var array = []; | ||
_.each(oldkey, _.bind(function(key){ | ||
array.push(this.getValue(item, key)); | ||
},this)); | ||
array = _.map(oldkey, _.bind(function(item,map) {return this.iterator(map,item)}, this , item));//need to swap arguments for bind | ||
obj[newkey] = array; | ||
} else { | ||
} else if(typeof oldkey == 'object'){ | ||
var bound = _.bind(this.iterator, this, oldkey,item) | ||
obj[newkey] = bound(); | ||
} | ||
else { | ||
obj[newkey] = ""; | ||
@@ -107,0 +138,0 @@ } |
{ | ||
"name": "node-json-transform", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "A node module for transforming and performing operations on JSON.", | ||
@@ -28,5 +28,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"jasmine-node": "^1.14.5", | ||
"underscore": "^1.8.3" | ||
"deep-freeze": "0.0.1", | ||
"jasmine-node": "^1.14.5" | ||
} | ||
} |
197
README.md
# node-data-transform | ||
Usage | ||
##Usage | ||
###Basic Example | ||
```javascript | ||
@@ -84,18 +86,189 @@ var DataTransform = require("node-json-transform").DataTransform, | ||
```javascript | ||
[{ | ||
name : "title1", | ||
info: "description1", | ||
text: "This is a blog.", | ||
date: 1383544800000, | ||
link: "http://goo.cm", | ||
info: "mike more info", | ||
clearMe: "", | ||
fieldGroup: ['title1', { link : "http://goo.cm" }], | ||
iterated: true | ||
}] | ||
[ | ||
{ | ||
name : "title1", | ||
info: "description1", | ||
text: "This is a blog.", | ||
date: 1383544800000, | ||
link: "http://goo.cm", | ||
info: "mike more info", | ||
clearMe: "", | ||
fieldGroup: ['title1', { link : "http://goo.cm" }], | ||
iterated: true | ||
} | ||
] | ||
``` | ||
###Advanced Example | ||
''' | ||
var map = { | ||
list: 'items', | ||
item: { | ||
id: 'id', | ||
sku: 'sku', | ||
zero: 'zero', | ||
toReplace: 'sku', | ||
errorReplace: 'notFound', | ||
simpleArray: ['id', 'sku','sku'], | ||
complexArray: [ {node: 'id'} , { otherNode:'sku' } , {toReplace:'sku'} ], | ||
subObject: { | ||
node1: 'id', | ||
node2: 'sku', | ||
subSubObject: { | ||
node1: 'id', | ||
node2: 'sku', | ||
} | ||
} | ||
}, | ||
operate: [ | ||
{ | ||
run: (val) => 'replacement', | ||
on: 'subObject.subSubObject.node1' | ||
}, | ||
{ | ||
run: (val) => 'replacement', | ||
on: 'errorReplace' | ||
}, | ||
{ | ||
run: (val) => 'replacement', | ||
on: 'toReplace' | ||
}, | ||
{ | ||
run: (val) => 'replacement', | ||
on: 'simpleArray.2' | ||
}, | ||
{ | ||
run: (val) => 'replacement', | ||
on: 'complexArray.2.toReplace' | ||
} | ||
] | ||
}; | ||
var object = { | ||
items:[ | ||
{ | ||
id: 'books', | ||
zero: 0, | ||
sku:'10234-12312' | ||
} | ||
] | ||
}; | ||
var result = DataTransform(data, map).transform(); | ||
''' | ||
The expected output. | ||
''' | ||
[ | ||
{ | ||
"id": "books", | ||
"sku": "10234-12312", | ||
"zero": 0, | ||
"toReplace": "replacement", | ||
"errorReplace": "replacement", | ||
"simpleArray": [ | ||
"books", | ||
"10234-12312", | ||
"replacement" | ||
], | ||
"complexArray": [ | ||
{ | ||
"node": "books" | ||
}, | ||
{ | ||
"otherNode": "10234-12312" | ||
}, | ||
{ | ||
"toReplace": "replacement" | ||
} | ||
], | ||
"subObject": { | ||
"node1": "books", | ||
"node2": "10234-12312", | ||
"subSubObject": { | ||
"node1": "replacement", | ||
"node2": "10234-12312" | ||
} | ||
} | ||
} | ||
] | ||
''' | ||
###Multi-template Example | ||
''' | ||
products: [{ | ||
id: 'books0', | ||
zero: 0, | ||
sku: '00234-12312', | ||
subitems: [ | ||
{ subid: "0.0", subsku: "subskuvalue0.0" }, | ||
{ subid: "0.1", subsku: "subskuvalue0.1" } | ||
] | ||
}, { | ||
id: 'books1', | ||
zero: 1, | ||
sku: '10234-12312', | ||
subitems: [ | ||
{ subid: "1.0", subsku: "subskuvalue1.0" }, | ||
{ subid: "1.1", subsku: "subskuvalue1.1" } | ||
] | ||
}] | ||
}; | ||
var baseMap = { | ||
'list': 'products', | ||
'item' : { | ||
'myid': 'id', | ||
'mysku': 'sku', | ||
'mysubitems': 'subitems' | ||
}, | ||
operate: [ | ||
{ | ||
'run': function(ary) { | ||
return DataTransform({list:ary}, nestedMap).transform(); | ||
}, | ||
'on': 'mysubitems' | ||
} | ||
] | ||
}; | ||
var nestedMap = { | ||
'list': 'list', | ||
'item' : { | ||
'mysubid': 'subid', | ||
'mysubsku': 'subsku' | ||
} | ||
}; | ||
var result = DataTransform(data, baseMap).transform(); | ||
''' | ||
The expected output. | ||
''' | ||
[ | ||
{ | ||
"myid": "books0", | ||
"mysku": "00234-12312", | ||
"mysubitems": [ | ||
{ "mysubid": "0.0", "mysubsku": "subskuvalue0.0" }, | ||
{ "mysubid": "0.1", "mysubsku": "subskuvalue0.1"} | ||
] | ||
}, | ||
{ | ||
"myid": "books1", | ||
"mysku": "10234-12312", | ||
"mysubitems": [ | ||
{ "mysubid": "1.0", "mysubsku": "subskuvalue1.0" }, | ||
{ "mysubid": "1.1", "mysubsku": "subskuvalue1.1" } | ||
] | ||
} | ||
] | ||
''' | ||
Enjoy! | ||
## Changelog | ||
1.0.11 Adding support for next object and nested array references. | ||
1.0.10 Make each compatible with other options. | ||
@@ -102,0 +275,0 @@ 1.0.9 Updated the changelog. |
var DataTransform = require('../index.js').DataTransform, | ||
_ = require("underscore"); | ||
_ = require("lodash"); | ||
@@ -4,0 +4,0 @@ var data = { |
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
19718
8
532
308