Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
existsUnless is added
Browse files Browse the repository at this point in the history
  • Loading branch information
Afsin Ustundag committed Jun 25, 2015
1 parent 4b374f1 commit 5acccf6
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 11 deletions.
94 changes: 92 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ The following are the list of all keys that have special meaning in template obj
- [`arrayContent`](#arrayContent)
- [`constant`](#constant)
- [`existsWhen`](#existsWhen)
- [`existsUnless`](#existsUnless)
- [`dataTransform`](#dataTransform)
- [`default`](#default)
- [`multiple`](#multiple)
Expand Down Expand Up @@ -451,7 +452,7 @@ console.log(r); // 'CONST'
<a name="existsWhen" />
#### `existsWhen` rule

This rule determines if a property or value exists. It must be a predicate or array of predicates. This rule is evaluated before any other rule on the same level.
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.

```js
var _ = require('lodash');
Expand Down Expand Up @@ -496,7 +497,7 @@ var r2 = j2j.run(template, {
console.log(r2); // null
```

If this rule is an array each predicate in the array must evaluate to true
If this rule is an array, each predicate in the array must evaluate to true

```js
var _ = require('lodash');
Expand Down Expand Up @@ -537,6 +538,95 @@ console.log(r2.dest_a); // 'value_a'
console.log(r2.dest_b); // 'value_b'
```

<a name="existsUnless" />
#### `existsUnless` rule

This rule must be a predicate or array of predicates. If the predicate evaluates to true, the template is ignored. This rule is evaluated before any other rule but existsWhen.

```js
var _ = require('lodash');

var template = {
content: {
dest_a: {
dataKey: 'a'
},
dest_b: {
dataKey: 'b',
existsUnless: _.partialRight(_.has, 'c')
},
},
existsUnless: function (input) {
return input && input.private;
}
};

var r0 = j2j.run(template, {
a: 'value_a',
b: 'value_b',
c: 0,
private: false
});
console.log(r0.dest_a); // 'value_a'
console.log(r0.dest_b); // undefined

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

var r2 = j2j.run(template, {
a: 'value_a',
b: 'value_b',
private: true
});
console.log(r2); // null
```

If this rule is an array, each predicate in the array must evaluate to true for the template to evaluate to `null`.

```js
var _ = require('lodash');

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'

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'

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

<a name="dataTransform" />
#### `dataTransform` rule

Expand Down
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- dataKeyPieceOverride functionality is removed
- paths like 'a.b.c' where 'b' is array is not supported. use a.b[*].c with jsonave.
- 'dataKeyFnOptions' replaced with 'context' option.
- existsWhen now accepts arrays.
- existsUnless is added. Opposite of existsWhen

# v1.5.0 - June 12, 2015

Expand Down
38 changes: 29 additions & 9 deletions lib/json2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,39 @@ var prototype = {
return null;
}
},
run: function (template, input) {
if (template.existsWhen) {
if (Array.isArray(template.existsWhen)) {
var ewCheck = template.existsWhen.every(function (ew) {
evaluateExistsWhen: function (template, input) {
var existsWhen = template.existsWhen;
if (existsWhen) {
if (Array.isArray(existsWhen)) {
return existsWhen.every(function (ew) {
return ew(input);
});
if (!ewCheck) {
return null;
}
} else if (!template.existsWhen(input)) {
return null;
} else {
return existsWhen(input);
}
}
return true;
},
evaluateExistsUnless: function (template, input) {
var existsUnless = template.existsUnless;
if (existsUnless) {
if (Array.isArray(existsUnless)) {
return existsUnless.every(function (ew) {
return ew(input);
});
} else {
return existsUnless(input);
}
}
return false;
},
run: function (template, input) {
if (!this.evaluateExistsWhen(template, input)) {
return null;
}
if (this.evaluateExistsUnless(template, input)) {
return null;
}
if ((input !== null) && (input !== undefined) && template.dataKey) {
input = this.evaluateDataKey(input, template.dataKey);
}
Expand Down
92 changes: 92 additions & 0 deletions test/test-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,98 @@ describe('examples', function () {
expect(r2.dest_b).to.equal('value_b');
});

it('existsUnless - 0', function () {
var _ = require('lodash');

var template = {
content: {
dest_a: {
dataKey: 'a'
},
dest_b: {
dataKey: 'b',
existsUnless: _.partialRight(_.has, 'c')
},
},
existsUnless: function (input) {
return input && input.private;
}
};

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

var r1 = j2j.run(template, {
a: 'value_a',
b: 'value_b'
});
//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',
private: true
});
//console.log(r2); // null
expect(r2).to.equal(null);
});

it('existsUnless - 1', function () {
var _ = require('lodash');

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 - 0', function () {
var nameTemplate = {
content: {
Expand Down

0 comments on commit 5acccf6

Please sign in to comment.