node-json-transform
Advanced tools
Comparing version 1.0.13 to 1.0.14
33
index.js
@@ -9,6 +9,15 @@ // DataTransform | ||
getValue : function(obj, key) { | ||
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; | ||
}, | ||
getValue : function(obj, key, newKey) { | ||
if(typeof(obj) == "undefined") { | ||
return ""; | ||
return; | ||
} | ||
@@ -32,3 +41,3 @@ | ||
} else { | ||
return null; | ||
return this.defaultOrNull(newKey); | ||
} | ||
@@ -76,3 +85,4 @@ } | ||
var value = this.getValue(data, map.list), | ||
normalized = {}; | ||
normalized = {}; | ||
if(value) { | ||
@@ -83,7 +93,18 @@ var list = this.getList(); | ||
normalized = this.each(normalized); | ||
normalized = _.each(normalized, this.remove); | ||
} | ||
return normalized; | ||
return normalized; | ||
}, | ||
remove: function(item){ | ||
if( _.isArray(map.remove) ) { | ||
_.each(map.remove, (key)=>{ | ||
delete item[key]; | ||
}); | ||
} | ||
return item; | ||
}, | ||
operate: function(data) { | ||
@@ -126,3 +147,3 @@ | ||
if(typeof(oldkey) == "string" && oldkey.length > 0) { | ||
obj[newkey] = this.getValue(item, oldkey); | ||
obj[newkey] = this.getValue(item, oldkey, newkey); | ||
} else if( _.isArray(oldkey) ) { | ||
@@ -129,0 +150,0 @@ array = _.map(oldkey, _.bind(function(item,map) {return this.iterator(map,item)}, this , item));//need to swap arguments for bind |
{ | ||
"name": "node-json-transform", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"description": "A node module for transforming and performing operations on JSON.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# node-data-transform | ||
##Usage | ||
## Usage | ||
###Basic Example | ||
### Basic Example | ||
@@ -102,3 +102,3 @@ ```javascript | ||
###Advanced Example | ||
### Advanced Example | ||
@@ -123,4 +123,8 @@ ``` | ||
} | ||
} | ||
}, | ||
remove: ['unwanted'] | ||
}, | ||
defaults: { | ||
"missingData": true | ||
}, | ||
operate: [ | ||
@@ -155,3 +159,4 @@ { | ||
zero: 0, | ||
sku:'10234-12312' | ||
sku:'10234-12312', | ||
unwanted: true | ||
} | ||
@@ -196,3 +201,4 @@ ] | ||
} | ||
} | ||
}, | ||
"missingData": true | ||
} | ||
@@ -202,3 +208,3 @@ ] | ||
###Multi-template Example | ||
### Multi-template Example | ||
@@ -280,2 +286,3 @@ ``` | ||
## Changelog | ||
1.0.14 Add support for default values via "defaults" definition. Add support for removing attributes via the "remove" definition. | ||
1.0.13 Update code examples. | ||
@@ -282,0 +289,0 @@ 1.0.12 Fixed readme formatting. |
@@ -258,3 +258,2 @@ var DataTransform = require('../index.js').DataTransform, | ||
it("should be able to combine mapping with each", function(){ | ||
@@ -292,2 +291,62 @@ | ||
it("should delete attributes", function(){ | ||
var data = { | ||
posts: [ | ||
{name: "peter", unwanted: true}, | ||
{name: "paul", unwanted: true}, | ||
{name: "marry", unwanted: true} | ||
] | ||
}; | ||
var map = { | ||
list: 'posts', | ||
remove: ['unwanted'] | ||
}; | ||
var dataTransform = DataTransform(data, map); | ||
var result = dataTransform.transform(); | ||
expect(result).toEqual([ | ||
{name: "peter"}, | ||
{name: "paul"}, | ||
{name: "marry"} | ||
]); | ||
}); | ||
it("should use default attributes for missing data", function(){ | ||
var data = { | ||
posts: [ | ||
{name: "peter", valid: true}, | ||
{name: "paul", valid: true}, | ||
{name: "marry"} | ||
] | ||
}; | ||
var map = { | ||
list: 'posts', | ||
item: { | ||
verified: 'valid', | ||
name: 'name' | ||
}, | ||
defaults: { | ||
verified: false | ||
} | ||
}; | ||
var dataTransform = DataTransform(data, map); | ||
var result = dataTransform.transform(); | ||
expect(result).toEqual([ | ||
{name: "peter", verified:true}, | ||
{name: "paul", verified:true}, | ||
{name: "marry", verified:false} | ||
]); | ||
}); | ||
}); |
21540
595
320