Skip to content

Commit 5d5a761

Browse files
committed
fixed logic in setValue for existing null values
1 parent 5f52b94 commit 5d5a761

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
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: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,21 @@ exports.DataTransform = function(data, map){
4747
return;
4848
}
4949

50-
var value = obj || data,
51-
key = key || map.list,
52-
keys = null;
5350
if(key == "") {
5451
return;
5552
}
56-
53+
5754
keys = key.split('.');
58-
let target = null;
55+
var target = obj;
5956
for(var i = 0; i < keys.length; i++ ) {
60-
if(value !== "undefined" && value[keys[i]]) {
61-
target = value;
62-
value = value[keys[i]];
63-
} else{
57+
if(i == keys.length-1){
58+
target[keys[i]] = newValue;
6459
return;
6560
}
61+
if(keys[i] in target)
62+
target = target[keys[i]];
63+
else return;
6664
}
67-
target[keys[i-1]] = newValue;
6865
},
6966

7067
getList: function(){

test/complexTemplate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let map = {
77
id: 'id',
88
sku: 'sku',
99
toReplace: 'sku',
10+
errorReplace: 'notFound',
1011
simpleArray: ['id', 'sku','sku'],
1112
complexArray: [{node: 'id'},{otherNode:'sku'},{toReplace:'sku'}],
1213
subObject: {
@@ -22,6 +23,10 @@ let map = {
2223
run: (val)=>'replacement',
2324
on: 'subObject.subSubObject.node1'
2425
},
26+
{
27+
run: (val)=>'replacement',
28+
on: 'errorReplace'
29+
},
2530
{
2631
run: (val)=>'replacement',
2732
on: 'toReplace'

0 commit comments

Comments
 (0)