Skip to content

Commit

Permalink
Merge pull request #52 from fastcodejava/master
Browse files Browse the repository at this point in the history
Params for existsWhen, existsUnless and existsEither
  • Loading branch information
fastcodejava committed Mar 21, 2017
2 parents 1c08a72 + 0c7ee5d commit 00bb5a4
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 15 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ console.log(r); // 'CONST'

This rule must be a predicate or array of predicates. If the predicate evaluates to false, the template is ignored. This rule is evaluated before any other rule on the same level.
The predicate can be a function, an object or a simple property. If it is an object or a simple property it works just like [iteratee](https://lodash.com/docs/4.16.3#iteratee) in lodash.
The property can be in the input or in the params. It is to be noted that this feature is little different from the value function which is supplied with params as well as input.
For the value function the input and the params are available at the same time to the function.

```js
var _ = require('lodash');
Expand Down Expand Up @@ -607,7 +609,22 @@ var r2 = j2j.run(template, {
b: 'value_b',
c: 0
});
console.log(r2); // null
console.log(r2); // null because public is not present


var r3 = j2j.run(template, {
a: 'value_a',
b: 'value_b'
},
{
public: true,
c: 0
}
);

//console.log(r3.dest_a); // 'value_a'
//console.log(r3.dest_b); // 'value_b'

```

If this rule is an array, each predicate in the array must evaluate to true
Expand Down Expand Up @@ -653,7 +670,7 @@ console.log(r2.dest_b); // 'value_b'
<a name="existsEither" />
#### `existsEither` rule

This rule must be ab array of predicates. If all the predicates evaluates to false, the template is ignored. This rule is evaluated before any other rule on the same level.
This rule must be an array of predicates. If all the predicates evaluates to false, the template is ignored. This rule is evaluated before any other rule on the same level.
The predicate can be a function, an object or a simple property. If it is an object or a simple property it works just like [iteratee](https://lodash.com/docs/4.16.3#iteratee) in lodash.

```js
Expand Down
27 changes: 16 additions & 11 deletions lib/jsonapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ var prototype = {
var valueType = (typeof value);
if (valueType === 'function') {
return value(input, parent, params);
//if (parent && params) {
// return value(input, parent, params);
//} else {
// return value(input);
//}
} else if (valueType === 'object') {
return this.runEngine(value, input, parent, params);
} else {
Expand Down Expand Up @@ -185,39 +190,39 @@ var prototype = {
return null;
}
},
evaluateExistsWhen: function (template, input) {
evaluateExistsWhen: function (template, input, params) {
var existsWhen = template.existsWhen;

if (existsWhen) {
if (Array.isArray(existsWhen)) {
return existsWhen.every(function (ew) {
return _.isFunction(ew) ? ew(input) : _.iteratee(ew)(input);
return _.isFunction(ew) ? (ew(input) || ew(params)) : (_.iteratee(ew)(input) || _.iteratee(ew)(params));
});
} else {
return _.isFunction(existsWhen) ? existsWhen(input) : _.iteratee(existsWhen)(input);
return _.isFunction(existsWhen) ? (existsWhen(input) || existsWhen(params)) : (_.iteratee(existsWhen)(input) || _.iteratee(existsWhen)(params));
}
}
return true;
},
evaluateExistsUnless: function (template, input) {
evaluateExistsUnless: function (template, input, params) {
var existsUnless = template.existsUnless;
if (existsUnless) {
if (Array.isArray(existsUnless)) {
return existsUnless.every(function (ew) {
return _.isFunction(ew) ? ew(input) : _.iteratee(ew)(input);
return _.isFunction(ew) ? (ew(input) || ew(params)) : (_.iteratee(ew)(input) || _.iteratee(ew)(params));
});
} else {
return _.isFunction(existsUnless) ? existsUnless(input) : _.iteratee(existsUnless)(input);
return _.isFunction(existsUnless) ? (existsUnless(input) || existsUnless(params)) : (_.iteratee(existsUnless)(input) || _.iteratee(existsUnless)(params));
}
}
return false;
},
evaluateExistsEither: function (template, input) {
evaluateExistsEither: function (template, input, params) {
var existsEither = template.existsEither;

if (Array.isArray(existsEither)) {
return existsEither.some(function (exEith) {
return _.isFunction(exEith) ? exEith(input) : _.iteratee(exEith)(input);
return _.isFunction(exEith) ? (exEith(input) || exEith(params)) : (_.iteratee(exEith)(input) || _.iteratee(exEith)(params));
});
}
return true;
Expand All @@ -226,13 +231,13 @@ var prototype = {
return this.runEngine(template, input, null, params);
},
runEngine: function (template, input, parent, params) {
if (!this.evaluateExistsWhen(template, input)) {
if (!this.evaluateExistsWhen(template, input, params)) {
return null;
}
if (!this.evaluateExistsEither(template, input)) {
if (!this.evaluateExistsEither(template, input, params)) {
return null;
}
if (this.evaluateExistsUnless(template, input)) {
if (this.evaluateExistsUnless(template, input, params)) {
return null;
}
if ((input !== null) && (input !== undefined) && template.dataKey) {
Expand Down
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": "1.0.4",
"version": "1.0.5",
"description": "Template based JSON to JSON transformer",
"main": "./index.js",
"directories": {
Expand Down
86 changes: 86 additions & 0 deletions test/test-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,18 @@ describe('examples', function () {
});
//console.log(r2); // null
expect(r2).to.equal(null);

var r3 = j2j.run(template, {
a: 'value_a',
b: 'value_b'
}, {
public: true,
c: 0
});
//console.log(r3.dest_a); // 'value_a'
//console.log(r3.dest_b); // 'value_b'
expect(r3.dest_a).to.equal('value_a');
expect(r3.dest_b).to.equal('value_b');
});

it('existsWhen - 1', function () {
Expand Down Expand Up @@ -580,6 +592,36 @@ describe('examples', function () {
expect(r0).to.equal(null);
});

it('existsEither - 1 with params', function () {

var template = {
content: {
dest_a: {
dataKey: 'a'
},
dest_b: {
dataKey: 'b'
}
},
existsEither: ['c', 'd']
};

var r0 = j2j.run(template, {
a: 'value_a',
b: 'value_b'
}, {
c: 'available'
});
expect(r0.dest_a).to.equal('value_a');
expect(r0.dest_b).to.equal('value_b');

r0 = j2j.run(template, {
a: 'value_a',
b: 'value_b'
});
expect(r0).to.equal(null);
});

it('existsUnless - 0', function () {
var template = {
content: {
Expand Down Expand Up @@ -677,6 +719,50 @@ describe('examples', function () {
expect(r2).to.equal(null);
});

it('existsUnless - 1 with params', function () {
var template = {
content: {
dest_a: {
dataKey: 'a'
},
dest_b: {
dataKey: 'b'
}
},
existsUnless: [_.partialRight(_.has, 'c'), _.partialRight(_.has, 'd')]
};

var r0 = j2j.run(template, {
a: 'value_a',
b: 'value_b'
}, {
c: 'available'
});
//console.log(r0.dest_a); // 'value_a'
//console.log(r0.dest_b); // 'value_b'
expect(r0.dest_a).to.equal('value_a');
expect(r0.dest_b).to.equal('value_b');

var r1 = j2j.run(template, {
a: 'value_a',
b: 'value_b',
d: 'available'
});
//console.log(r1.dest_a); // 'value_a'
//console.log(r1.dest_b); // 'value_b'
expect(r1.dest_a).to.equal('value_a');
expect(r1.dest_b).to.equal('value_b');

var r2 = j2j.run(template, {
a: 'value_a',
b: 'value_b',
c: 'available',
d: 'available'
});
//console.log(r2); // null
expect(r2).to.equal(null);
});

it('dataTransform - with function', function () {
var nameTemplate = {
content: {
Expand Down
2 changes: 1 addition & 1 deletion test/test-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var case_2 = require('./test_cases/case-value-2');
var case_3 = require('./test_cases/case-value-3');

var expect = chai.expect;
var _ = require('lodash');

describe('value', function () {
var engine = json2json.instance();
Expand Down Expand Up @@ -53,5 +54,4 @@ describe('value', function () {
expect(actual).to.deep.equal(case_3.expecteds[i]);
}
});

});

0 comments on commit 00bb5a4

Please sign in to comment.