@@ -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
0 commit comments