Skip to content

Commit 01eb204

Browse files
authored
Merge pull request #11 from lingwooc/complexTemplate
add support for complex templates (sub objects, arrays of object and operations for them)
2 parents 7b62a80 + a14ef62 commit 01eb204

File tree

3 files changed

+125
-17
lines changed

3 files changed

+125
-17
lines changed

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible Node.js debug attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceRoot}\\test\\complexTemplate.js",
12+
"cwd": "${workspaceRoot}"
13+
},
14+
{
15+
"type": "node",
16+
"request": "attach",
17+
"name": "Attach to Process",
18+
"port": 5858
19+
}
20+
]
21+
}

index.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ exports.DataTransform = function(data, map){
2525
keys = key.split('.');
2626
for(var i = 0; i < keys.length; i++ ) {
2727
if(typeof(value) !== "undefined" &&
28-
value[keys[i]]) {
28+
keys[i] in value) {
2929
value = value[keys[i]];
3030
} else {
3131
return null;
@@ -37,6 +37,33 @@ exports.DataTransform = function(data, map){
3737

3838
},
3939

40+
setValue : function(obj, key, newValue) {
41+
42+
if(typeof(obj) == "undefined") {
43+
return;
44+
}
45+
46+
if(key == '' || key == undefined) {
47+
return;
48+
}
49+
50+
if(key == "") {
51+
return;
52+
}
53+
54+
keys = key.split('.');
55+
var target = obj;
56+
for(var i = 0; i < keys.length; i++ ) {
57+
if(i == keys.length-1){
58+
target[keys[i]] = newValue;
59+
return;
60+
}
61+
if(keys[i] in target)
62+
target = target[keys[i]];
63+
else return;
64+
}
65+
},
66+
4067
getList: function(){
4168
return this.getValue(data, map.list);
4269
},
@@ -47,8 +74,8 @@ exports.DataTransform = function(data, map){
4774
normalized = {};
4875
if(value) {
4976
var list = this.getList();
50-
var normalized = map.item ? _.map(list, _.bind(this.iterator, this)) : list;
51-
normalized = this.operate(normalized);
77+
var normalized = map.item ? _.map(list, _.bind(this.iterator, this, map.item)) : list;
78+
normalized = _.bind(this.operate, this, normalized)();
5279
normalized = this.each(normalized);
5380
}
5481
return normalized;
@@ -58,18 +85,18 @@ exports.DataTransform = function(data, map){
5885
operate: function(data) {
5986

6087
if(map.operate) {
61-
_.each(map.operate, function(method){
62-
data = _.map(data, function(item){
88+
_.each(map.operate, _.bind(function(method){
89+
data = _.map(data, _.bind(function(item){
6390
var fn;
6491
if( 'string' === typeof method.run ) {
6592
fn = eval( method.run );
6693
} else {
6794
fn = method.run;
6895
}
69-
item[method.on] = fn(item[method.on]);
96+
this.setValue(item,method.on,fn(this.getValue(item,method.on)))
7097
return item;
71-
});
72-
});
98+
},this));
99+
},this));
73100
}
74101
return data;
75102

@@ -82,21 +109,25 @@ exports.DataTransform = function(data, map){
82109
return data;
83110
},
84111

85-
iterator : function(item) {
112+
iterator : function(map, item) {
86113

87114
var obj = {};
88-
_.each(map.item, _.bind(function(oldkey, newkey) {
115+
116+
//to support simple arrays with recursion
117+
if(typeof(map) == "string") {
118+
return this.getValue(item, map);
119+
}
120+
_.each(map, _.bind(function(oldkey, newkey) {
89121
if(typeof(oldkey) == "string" && oldkey.length > 0) {
90122
obj[newkey] = this.getValue(item, oldkey);
91123
} else if( _.isArray(oldkey) ) {
92-
93-
var array = [];
94-
_.each(oldkey, _.bind(function(key){
95-
array.push(this.getValue(item, key));
96-
},this));
124+
array = _.map(oldkey, _.bind(function(item,map) {return this.iterator(map,item)}, this , item));//need to swap arguments for bind
97125
obj[newkey] = array;
98-
99-
} else {
126+
} else if(typeof oldkey == 'object'){
127+
let bound = _.bind(this.iterator, this, oldkey,item)
128+
obj[newkey] = bound();
129+
}
130+
else {
100131
obj[newkey] = "";
101132
}
102133

test/complexTemplate.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var DataTransform = require('../index.js').DataTransform,
2+
_ = require("underscore");
3+
4+
let map = {
5+
list: 'items',
6+
item: {
7+
id: 'id',
8+
sku: 'sku',
9+
zero: 'zero',
10+
toReplace: 'sku',
11+
errorReplace: 'notFound',
12+
simpleArray: ['id', 'sku','sku'],
13+
complexArray: [{node: 'id'},{otherNode:'sku'},{toReplace:'sku'}],
14+
subObject: {
15+
node1: 'id',
16+
node2: 'sku',
17+
subSubObject: {
18+
node1: 'id',
19+
node2: 'sku',
20+
}
21+
}
22+
},
23+
operate: [{
24+
run: (val)=>'replacement',
25+
on: 'subObject.subSubObject.node1'
26+
},
27+
{
28+
run: (val)=>'replacement',
29+
on: 'errorReplace'
30+
},
31+
{
32+
run: (val)=>'replacement',
33+
on: 'toReplace'
34+
},
35+
{
36+
run: (val)=>'replacement',
37+
on: 'simpleArray.2'
38+
},
39+
{
40+
run: (val)=>'replacement',
41+
on: 'complexArray.2.toReplace'
42+
},]
43+
};
44+
45+
let object ={
46+
items:[{
47+
id: 'books',
48+
zero: 0,
49+
sku:'10234-12312'
50+
}]
51+
}
52+
53+
let dataTransform = new DataTransform(object, map);
54+
let result = dataTransform.transform();
55+
56+
console.log(JSON.stringify(result, null, 4));

0 commit comments

Comments
 (0)