Skip to content

Commit

Permalink
[added] allow function then/otherwise bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jul 20, 2016
1 parent 0f43122 commit 9b5232a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Condition.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import has from 'lodash/has';
import isSchema from './util/isSchema';

function callOrConcat(schema) {
if (typeof schema === 'function')
return schema

return base => base.concat(schema)
}

class Conditional {

constructor(refs, options) {
let { is, then, otherwise } = options;

this.refs = [].concat(refs)

then = callOrConcat(then);
otherwise = callOrConcat(otherwise);

if (typeof options === 'function')
this.fn = options
else
Expand All @@ -24,8 +34,10 @@ class Conditional {
? is : ((...values) => values.every(value => value === is))

this.fn = function (...values) {
let ctx = values.pop();
return isFn(...values) ? ctx.concat(then) : ctx.concat(otherwise)
let currentSchema = values.pop();
let option = isFn(...values) ? then : otherwise

return option(currentSchema)
}
}
}
Expand Down

0 comments on commit 9b5232a

Please sign in to comment.