Skip to content

Commit

Permalink
Merge pull request #90 from amida-tech/pick-omit-array
Browse files Browse the repository at this point in the history
get pick and omit work for arrays
  • Loading branch information
au2 committed Jun 7, 2018
2 parents 3abe1da + ca9f5c5 commit bf9ec37
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
59 changes: 29 additions & 30 deletions lib/jsonapter.js
Expand Up @@ -16,6 +16,33 @@ function templateHasKey(template) {
};
}

function pickOrOmit(result, input, template) {
if (input && (template.omit || template.pick)) {
if (template.pick) {
var propsPick = _.pick(input, template.pick);
if (!_.isEmpty(propsPick)) {
if (result) {
_.assign(result, propsPick);
} else {
result = propsPick;
}
}
}

if (template.omit) {
var propsOmit = _.omit(input, template.omit);
if (!_.isEmpty(propsOmit)) {
if (result) {
_.assign(result, propsOmit);
} else {
result = propsOmit;
}
}
}
}
return result;
}

var prototype = {
dataKeyToInput: function (input, dataKey) {
if (typeof dataKey === 'function') {
Expand Down Expand Up @@ -252,13 +279,7 @@ var prototype = {
r[actionKey] = template[actionKey];
}
return r;
}, {});

// if (_.isEmpty(modifiedTemplate)) {
// return input;
// }

modifiedTemplate.params = template.params;
}, _.pick(template, ['pick', 'omit', 'params']));

var that = this;
var index = 0;
Expand Down Expand Up @@ -397,29 +418,7 @@ var prototype = {
// }

var result = input !== null && actionKey ? this[actionKey](template, input, parent, params, arrayIndex, arraySize, options, extraParams) : input;

if (template.pick) {
var propsPick = _.pick(input, template.pick);
if (!_.isEmpty(propsPick)) {
if (result) {
_.assign(result, propsPick);
} else {
result = propsPick;
}
}
}

if (template.omit) {
var propsOmit = _.omit(input, template.omit);
console.log(propsOmit);
if (!_.isEmpty(propsOmit)) {
if (result) {
_.assign(result, propsOmit);
} else {
result = propsOmit;
}
}
}
result = pickOrOmit(result, input, template);

if (result === null && !util.isNullOrUndefined(template.default)) {
result = _.isFunction(template.default) ? template.default(input, parent, params) : template.default;
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "jsonapter",
"version": "2.0.5",
"version": "2.0.6",
"description": "Template based JSON to JSON transformer",
"main": "./index.js",
"directories": {
Expand All @@ -10,7 +10,7 @@
"test": "grunt mocha",
"verify": "grunt"
},
"author": "Afsin Ustundag <afsin.ustundag@us.pwc.com>",
"author": "Afsin Ustundag <afsin@amida.com>",
"contributors": [
"Gautam Dev <gdind2003@gmail.com>"
],
Expand Down
4 changes: 4 additions & 0 deletions test/test_cases/case-omit-0.js
Expand Up @@ -37,6 +37,8 @@ exports.inputs = [{
hairColor: 'brown'
}];

exports.inputs.push(exports.inputs.slice());

exports.expecteds = [{
fullName: 'Joe Doe',
age: 15,
Expand All @@ -51,3 +53,5 @@ exports.expecteds = [{
}, {
hairColor: 'brown'
}];

exports.expecteds.push(exports.expecteds.slice());
4 changes: 4 additions & 0 deletions test/test_cases/case-pick-0.js
Expand Up @@ -36,6 +36,8 @@ exports.inputs = [{
hairColor: 'brown'
}];

exports.inputs.push(exports.inputs.slice());

exports.expecteds = [{
fullName: 'Joe Doe',
age: 15,
Expand All @@ -51,3 +53,5 @@ exports.expecteds = [{
eyeColor: 'blue',
hairColor: 'brown'
}];

exports.expecteds.push(exports.expecteds.slice());

0 comments on commit bf9ec37

Please sign in to comment.