Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "0.10"
- "4.0"

before_script:
- npm install -g grunt-cli@0.1.x
Expand Down
24 changes: 12 additions & 12 deletions lib/jsonapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
var _ = require('lodash');
var util = require('./util');

var outFns = {
string: util.convertToString,
boolean: util.convertToBoolean,
number: util.convertToNumber,
object: util.convertToObject
var outFns = {
string: util.convertToString,
boolean: util.convertToBoolean,
number: util.convertToNumber,
object: util.convertToObject
};


function templateHasKey(template) {
return function (key) {
return template.hasOwnProperty(key);
Expand All @@ -29,7 +28,7 @@ var prototype = {
if ((Array.isArray(paramKey) || _.isPlainObject(paramKey)) && this.options.mode === 'strict') {
throw new Error("paramKey can only be a string " + paramKey);
}
if ((paramKey === '' || paramKey === null) && this.options.mode === 'strict') {
if ((paramKey === '' || paramKey === null) && this.options.mode === 'strict') {
throw new Error("paramKey can not be an empty string or null " + paramKey);
}
return _.get(params, paramKey, null);
Expand All @@ -42,7 +41,7 @@ var prototype = {
var n = keyArray.length;
for (var i = 0; i < n; ++i) {
var dataKey = keyArray[i];
var inputCandidate = fn(input, dataKey);
var inputCandidate = fn.call(this, input, dataKey);
if ((inputCandidate !== null) && (inputCandidate !== undefined)) {
return inputCandidate;
}
Expand All @@ -60,7 +59,7 @@ var prototype = {
if (_.isPlainObject(dataKey) && this.options.mode === 'strict') {
throw new Error("dataKey can be a string, array or function " + dataKey);
}
if ((dataKey === '' || dataKey === null) && this.options.mode === 'strict') {
if ((dataKey === '' || dataKey === null) && this.options.mode === 'strict') {
throw new Error("dataKey can not be an empty string or null " + dataKey);
}
if (Array.isArray(dataKey)) {
Expand Down Expand Up @@ -243,7 +242,7 @@ var prototype = {

return this.evaluateValue(templateValue, input, parent, params, arrayIndex, arraySize, options);
},
arrayIndex : function (template, input, parent, params, arrayIndex, arraySize) {
arrayIndex: function (template, input, parent, params, arrayIndex, arraySize) {
return this.evaluateArrayIndex(arrayIndex, template.arrayIndex);
},
runForArray: function (template, input, parent, params, options) {
Expand Down Expand Up @@ -384,7 +383,8 @@ var prototype = {
return template.output(resultForArray, input, parent, params);
} else if (output && outFns[output] && !util.isNullOrUndefined(resultForArray)) {
return outFns[output](resultForArray, template.output.type ? template.output : {});
} if (Array.isArray(resultForArray) && _.intersection(_.keys(template), this.arrayOptions).length > 0) {
}
if (Array.isArray(resultForArray) && _.intersection(_.keys(template), this.arrayOptions).length > 0) {
return util.convertArray(resultForArray, template);
} else {
return resultForArray;
Expand Down Expand Up @@ -439,6 +439,6 @@ exports.instance = function (overrides, addlActionKeys, options) {
if (addlActionKeys) {
result.actionKeys = result.actionKeys.concat(addlActionKeys);
}
result.options = options || {mode: 'strict'};
result.options = options || { mode: 'strict' };
return result;
};
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ module.exports = {
convertToNumber: convertToNumber,
convertToObject: convertToObject,
isNullOrUndefined: isNullOrUndefined
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapter",
"version": "2.0.3",
"version": "2.0.4",
"description": "Template based JSON to JSON transformer",
"main": "./index.js",
"directories": {
Expand Down
10 changes: 10 additions & 0 deletions test/test-dataKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var case_0 = require('./test_cases/case-dataKey-0');
var case_1 = require('./test_cases/case-dataKey-1');
var case_2 = require('./test_cases/case-dataKey-2');
var case_3 = require('./test_cases/case-dataKey-3');
var case_4 = require('./test_cases/case-dataKey-4');

var expect = chai.expect;

Expand Down Expand Up @@ -60,4 +61,13 @@ describe('dataKey', function () {
}
});

it('case-dataKey-4: ', function () {
var template = case_4.template;
var n = case_4.inputs.length;
for (var i = 0; i < n; ++i) {
var actual = engine.run(template, case_4.inputs[i]);
// console.log(JSON.stringify(actual, null, 2));
expect(actual).to.deep.equal(case_4.expecteds[i]);
}
});
});
4 changes: 2 additions & 2 deletions test/test-error-mode-slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var expect = chai.expect;
var _ = require('lodash');
// var j2j = bbj2j.instance();
var bbj2j = require('../index');
var j2j = bbj2j.instance(null, null, {mode: ""});
var j2j = bbj2j.instance(null, null, { mode: "" });

describe('example error', function () {

Expand Down Expand Up @@ -155,4 +155,4 @@ describe('example error', function () {
}
});

});
});
2 changes: 1 addition & 1 deletion test/test-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,4 @@ describe('example error', function () {
}
});

});
});
Loading