Skip to content

Commit 1685b50

Browse files
authored
Merge pull request #16 from fastcodejava/master
Modified jsonapter.js, added params support.
2 parents 2e6953b + 3841b96 commit 1685b50

File tree

6 files changed

+143
-19
lines changed

6 files changed

+143
-19
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The following are the list of all keys that have special meaning in template obj
7575
- [`firstOf`](#firstOf)
7676
- [`assign`](#assign)
7777
- [`ignoreDeep`](#ignoreDeep)
78+
- [`paramKey`](#paramKey)
7879

7980
<a name="dataKey" />
8081
#### `dataKey` rule
@@ -219,6 +220,23 @@ var r2 = j2j.run(template, {
219220
console.log(r2); // null
220221
```
221222

223+
<a name="paramKey" />
224+
#### `paramKey` rule
225+
226+
This rule selects a particular property of params, which can be passed as a optional third parameter to the run function as shown below :
227+
```js
228+
var template = {
229+
paramKey: 'a'
230+
};
231+
232+
var r0 = j2j.run(template, {}, {
233+
a: 1
234+
});
235+
console.log(r0); // 1
236+
237+
```
238+
239+
222240
<a name="value" />
223241
#### `value` rule
224242

@@ -973,7 +991,7 @@ Each engine instance `j2j` contains all the implementation details as functions
973991
- `actionKeys`
974992
- `dataKeyToInput`
975993
- `dataKeyArrayToInput`
976-
- `context
994+
- `context`
977995

978996
`run` is the entry point. `content`, `arrayContent`, `value`, `constant`, `firstOf` and `assign` are called action keys and listed in `actionKeys` array. Only one of `actionKeys` can appear on a template on the same level. None of these keys are designed to be overridden except `context`. However you can add additional functionality by adding new data and action keys.
979997

lib/jsonapter.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,46 @@ var prototype = {
1010
return _.get(input, dataKey, null);
1111
}
1212
},
13-
dataKeyArrayToInput: function (input, dataKeyArray) {
14-
var n = dataKeyArray.length;
13+
paramKeyToInput: function (input, paramKey, params) {
14+
return _.get(params, paramKey, null);
15+
},
16+
genericKeyArrayToInput: function (input, keyArray, fn) {
17+
var n = keyArray.length;
1518
for (var i = 0; i < n; ++i) {
16-
var dataKey = dataKeyArray[i];
17-
var inputCandidate = this.dataKeyToInput(input, dataKey);
19+
var dataKey = keyArray[i];
20+
var inputCandidate = fn(input, dataKey);
1821
if ((inputCandidate !== null) && (inputCandidate !== undefined)) {
1922
return inputCandidate;
2023
}
2124
}
2225
return null;
2326
},
27+
dataKeyArrayToInput: function (input, dataKeyArray) {
28+
return this.genericKeyArrayToInput(input, dataKeyArray, this.dataKeyToInput);
29+
},
30+
paramKeyArrayToInput: function (input, paramKeyArray) {
31+
return this.genericKeyArrayToInput(input, paramKeyArray, this.paramKeyToInput);
32+
},
2433
evaluateDataKey: function (input, dataKey) {
2534
if (Array.isArray(dataKey)) {
2635
return this.dataKeyArrayToInput(input, dataKey);
2736
} else {
2837
return this.dataKeyToInput(input, dataKey);
2938
}
3039
},
31-
evaluateValue: function (value, input) {
40+
evaluateParamKey: function (input, paramKey, params) {
41+
if (Array.isArray(paramKey)) {
42+
return this.paramKeyArrayToInput(input, paramKey);
43+
} else {
44+
return this.paramKeyToInput(input, paramKey, params);
45+
}
46+
},
47+
evaluateValue: function (value, input, params) {
3248
var valueType = (typeof value);
3349
if (valueType === 'function') {
3450
return value(input);
3551
} else if (valueType === 'object') {
36-
return this.run(value, input);
52+
return this.run(value, input, params);
3753
} else {
3854
return value;
3955
}
@@ -52,14 +68,14 @@ var prototype = {
5268
}
5369
return null;
5470
},
55-
content: function (template, input) {
71+
content: function (template, input, params) {
5672
var that = this;
5773
var content = template.content;
5874
var hasValue = false;
5975
var keys = Object.keys(content);
6076
var result = keys.reduce(function (r, key) {
6177
var contentValue = template.content[key];
62-
var value = that.evaluateValue(contentValue, input);
78+
var value = that.evaluateValue(contentValue, input, params);
6379
if (value !== null) {
6480
if (!that.options.pruneValues || !Array.isArray(that.options.pruneValues)) {
6581
if (template.ignoreDeep) {
@@ -84,11 +100,11 @@ var prototype = {
84100
}, {});
85101
return hasValue ? result : null;
86102
},
87-
arrayContent: function (template, input) {
103+
arrayContent: function (template, input, params) {
88104
var that = this;
89105
var content = template.arrayContent;
90106
var result = content.reduce(function (r, e) {
91-
var value = that.evaluateValue(e, input);
107+
var value = that.evaluateValue(e, input, params);
92108
if (value !== null) {
93109
if (Array.isArray(value)) {
94110
Array.prototype.push.apply(r, value);
@@ -100,11 +116,11 @@ var prototype = {
100116
}, []);
101117
return result.length ? result : null;
102118
},
103-
assign: function (template, input) {
119+
assign: function (template, input, params) {
104120
var templateAssign = template.assign;
105121
var that = this;
106122
var assignValues = templateAssign.reduce(function (r, assignValue) {
107-
var v = that.evaluateValue(assignValue, input);
123+
var v = that.evaluateValue(assignValue, input, params);
108124
if ((v !== null) && (typeof v === 'object')) {
109125
r.push(v);
110126
}
@@ -116,11 +132,11 @@ var prototype = {
116132
var v = _.assign.apply(null, assignValues);
117133
return v;
118134
},
119-
firstOf: function (template, input) {
135+
firstOf: function (template, input, params) {
120136
var templateFirstOf = template.firstOf;
121137
for (var i = 0; i < templateFirstOf.length; ++i) {
122138
var t = templateFirstOf[i];
123-
var value = this.evaluateValue(t, input);
139+
var value = this.evaluateValue(t, input, params);
124140
if (value !== null) {
125141
return value;
126142
}
@@ -130,9 +146,9 @@ var prototype = {
130146
constant: function (template, input) {
131147
return template.constant;
132148
},
133-
value: function (template, input) {
149+
value: function (template, input, params) {
134150
var templateValue = template.value;
135-
return this.evaluateValue(templateValue, input);
151+
return this.evaluateValue(templateValue, input, params);
136152
},
137153
runForArray: function (template, input) {
138154
var hasActionKeys = false;
@@ -188,7 +204,7 @@ var prototype = {
188204
}
189205
return false;
190206
},
191-
run: function (template, input) {
207+
run: function (template, input, params) {
192208
if (!this.evaluateExistsWhen(template, input)) {
193209
return null;
194210
}
@@ -198,6 +214,9 @@ var prototype = {
198214
if ((input !== null) && (input !== undefined) && template.dataKey) {
199215
input = this.evaluateDataKey(input, template.dataKey);
200216
}
217+
if ((input !== null) && (input !== undefined) && template.paramKey && params) {
218+
input = this.evaluateParamKey(input, template.paramKey, params);
219+
}
201220
if ((input !== null) && template.dataTransform) {
202221
input = template.dataTransform(input, this.context);
203222
}
@@ -217,7 +236,7 @@ var prototype = {
217236
for (var i = 0; i < this.actionKeys.length; ++i) {
218237
var actionKey = this.actionKeys[i];
219238
if (template.hasOwnProperty(actionKey)) {
220-
result = this[actionKey](template, input);
239+
result = this[actionKey](template, input, params);
221240
break;
222241
}
223242
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"verify": "grunt"
1212
},
1313
"author": "Afsin Ustundag <[email protected]>",
14+
"contributors": [
15+
"Gautam Dev <[email protected]>"
16+
],
1417
"license": "Apache-2.0",
1518
"engines": {
1619
"node": ">= 0.10.0"

test/test-paramKey.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
3+
var chai = require('chai');
4+
5+
var json2json = require('../index');
6+
7+
var case_0 = require('./test_cases/case-paramKey-0');
8+
var case_1 = require('./test_cases/case-paramKey-1');
9+
10+
var params = {timestamp: "2016-08-24T15:38:07-05:00"};
11+
12+
var expect = chai.expect;
13+
14+
describe('paramKey', function () {
15+
var engine = json2json.instance();
16+
17+
it('case-paramKey-0: paramKey ', function () {
18+
var template = case_0.template;
19+
var n = case_0.inputs.length;
20+
for (var i = 0; i < n; ++i) {
21+
var actual = engine.run(template, case_0.inputs[i], params);
22+
expect(actual).to.deep.equal(case_0.expecteds[i]);
23+
}
24+
});
25+
26+
it('case-paramKey-1: paramKey ', function () {
27+
var template = case_1.template;
28+
var n = case_1.inputs.length;
29+
for (var i = 0; i < n; ++i) {
30+
var actual = engine.run(template, case_1.inputs[i], params);
31+
expect(actual).to.deep.equal(case_1.expecteds[i]);
32+
}
33+
});
34+
35+
});

test/test_cases/case-paramKey-0.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
3+
exports.template = {
4+
content: {
5+
created : {
6+
paramKey: 'timestamp'
7+
},
8+
updated : {
9+
paramKey: 'timestamp'
10+
}
11+
}
12+
};
13+
14+
exports.inputs = [];
15+
exports.expecteds = [];
16+
17+
exports.inputs[0] = {
18+
};
19+
20+
exports.expecteds[0] = {
21+
created: '2016-08-24T15:38:07-05:00',
22+
updated: '2016-08-24T15:38:07-05:00'
23+
};
24+

test/test_cases/case-paramKey-1.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
exports.template = {
4+
arrayContent: [{
5+
content : {
6+
created: {paramKey: 'timestamp'}
7+
}
8+
},{
9+
content : {
10+
updated : { paramKey: 'timestamp'}
11+
}}
12+
]
13+
};
14+
15+
exports.inputs = [];
16+
exports.expecteds = [];
17+
18+
exports.inputs[0] = {
19+
};
20+
21+
exports.expecteds[0] = [
22+
{created: '2016-08-24T15:38:07-05:00'},
23+
{updated: '2016-08-24T15:38:07-05:00'}
24+
];
25+

0 commit comments

Comments
 (0)