Skip to content

Commit

Permalink
Merge pull request #61 from fastcodejava/master
Browse files Browse the repository at this point in the history
Template default for array
  • Loading branch information
fastcodejava committed Apr 18, 2017
2 parents fbc214c + b20e325 commit cd7535e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lib/jsonapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ var prototype = {
input = input[0];
} else {
var resultForArray = this.runForArray(template, input, parent, params);
if (resultForArray === null && (template.default !== null && template.default !== undefined)) {
resultForArray = template.default;
if (resultForArray === null && !util.isNullOrUndefined(template.default)) {
resultForArray = _.isFunction(template.default) ? template.default(input, parent, params) : template.default;
}
return resultForArray;
}
Expand All @@ -300,12 +300,13 @@ var prototype = {
}
}

if (result === null && (template.default !== null && template.default !== undefined)) {
if (result === null && !util.isNullOrUndefined(template.default)) {
result = _.isFunction(template.default) ? template.default(input, parent, params) : template.default;
}
if ((result !== null) && template.multiple) {
result = [result];
}

return result;
},
actionKeys: ['content', 'value', 'template', 'arrayIndex', 'assign', 'firstOf', 'constant', 'arrayContent'],
Expand Down
9 changes: 8 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*/
var _ = require('lodash');

function isNullOrUndefined(input) {
return input === null || input === undefined;
}

function getSize(input) {
if (_.isArray(input) || _.isString(input)) {
return input.length;
Expand All @@ -11,4 +15,7 @@ function getSize(input) {
}
}

module.exports.getSize = getSize;
module.exports = {
getSize: getSize,
isNullOrUndefined: isNullOrUndefined
};
8 changes: 4 additions & 4 deletions test/test-boolean-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('templates with boolean, 0, or "" default values', function () {
};

var r = j2j.run(template, {
id: 'test',
id: 'test'
});

expect(r).to.deep.equal({
Expand All @@ -47,7 +47,7 @@ describe('templates with boolean, 0, or "" default values', function () {
};

var r = j2j.run(template, {
id: 'test',
id: 'test'
});

expect(r).to.deep.equal({
Expand All @@ -71,7 +71,7 @@ describe('templates with boolean, 0, or "" default values', function () {
};

var r = j2j.run(template, {
id: 'test',
id: 'test'
});

expect(r).to.deep.equal({
Expand All @@ -95,7 +95,7 @@ describe('templates with boolean, 0, or "" default values', function () {
};

var r = j2j.run(template, {
id: 'test',
id: 'test'
});

expect(r).to.deep.equal({
Expand Down
9 changes: 9 additions & 0 deletions test/test-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ describe('content', function () {
}
});

it('case-content-0: basic default for array with function', function () {
var template = case_0.template;
template.default = function () {
return [];
};
var actual = engine.run(template, []);
expect(actual).to.deep.equal([]);
});

it('case-content-1: array', function () {
var template = case_1.template;
var n = case_1.inputs.length;
Expand Down

0 comments on commit cd7535e

Please sign in to comment.