Skip to content
Permalink
Browse files

feat: added isUndefined choice

  • Loading branch information...
meenahoda committed Jul 19, 2018
1 parent 153dad6 commit 31ebdbf351065964587a51542e94b48823f025f5
Showing with 61 additions and 20 deletions.
  1. +13 −20 lib/get-top-level-choices.js
  2. +1 −0 lib/operators/index.js
  3. +15 −0 lib/operators/is-undefined.js
  4. +32 −0 test/tests.js
@@ -6,30 +6,23 @@ const operators = require('./operators')
module.exports = function getTopLevelChoices (choicesDefinition) {
const topLevelChoices = []

choicesDefinition.forEach(
function (choiceDefinition) {
choicesDefinition.forEach(choiceDefinition => {
// Find first operator
let operator
let operatorValue
_.forOwn(
choiceDefinition,
function (value, key) {
if (_.isUndefined(operator)) {
if (operators.hasOwnProperty(key)) {
operator = operators[key]
operatorValue = value
}
let operator, operatorValue
_.forOwn(choiceDefinition, (value, key) => {
if (_.isUndefined(operator)) {
if (operators.hasOwnProperty(key)) {
operator = operators[key]
operatorValue = value
}
}
)
})

topLevelChoices.push(
{
operator: operator,
operatorValue: operatorValue,
definition: _.cloneDeep(choiceDefinition)
}
)
topLevelChoices.push({
operator: operator,
operatorValue: operatorValue,
definition: _.cloneDeep(choiceDefinition)
})
}
)
return topLevelChoices
@@ -2,6 +2,7 @@ module.exports = {
And: require('./and'),
BooleanEquals: require('./boolean-equals'),
Includes: require('./includes'),
IsUndefined: require('./is-undefined'),
Not: require('./not'),
NumericEquals: require('./numeric-equals'),
NumericGreaterThan: require('./numeric-greater-than'),
@@ -0,0 +1,15 @@
'use strict'

module.exports = function isUndefinedOperator (inputValue, comparisonValue, candidateStateName, cache) {
let nextState
if (comparisonValue === true) {
if (inputValue === undefined) {
nextState = candidateStateName
}
} else if (comparisonValue === false) {
if (inputValue !== undefined) {
nextState = candidateStateName
}
}
return nextState
}
@@ -80,4 +80,36 @@ describe('Choice tests', function () {
expect(calculateNextState({foo: ['B', 'C']})).to.equal('SecondMatchState')
expect(calculateNextState({foo: ['E', 'F']})).to.equal('DefaultMatchState')
})

it('Should test the state using IsUndefined', () => {
const calculateNextState = choiceProcessor(
{
Choices: [
{
Variable: '$.foo',
IsUndefined: true,
Next: 'UndefinedState'
}
],
Default: 'DefinedState'
}
)
expect(calculateNextState({foo: undefined})).to.eql('UndefinedState')
expect(calculateNextState({foo: 'HELLO_WORLD'})).to.eql('DefinedState')

const calculateNextState_1 = choiceProcessor(
{
Choices: [
{
Variable: '$.foo',
IsUndefined: false,
Next: 'DefinedState'
}
],
Default: 'UndefinedState'
}
)
expect(calculateNextState_1({foo: undefined})).to.eql('UndefinedState')
expect(calculateNextState_1({foo: 'HELLO_WORLD'})).to.eql('DefinedState')
})
})

0 comments on commit 31ebdbf

Please sign in to comment.
You can’t perform that action at this time.