diff --git a/src/index.js b/src/index.js index 1d1c617..c4c11ba 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,5 @@ +const isUndefined = (value) => typeof value === 'undefined' + const evalMatchedPatternByStatmentsType = { 'string': (_obj, value, _args) => value, 'number': (_obj, value, _args) => value, @@ -6,8 +8,11 @@ const evalMatchedPatternByStatmentsType = { 'object': (_obj, value, _args) => value, } -const evalMatchedPattern = (obj, statements, ...args) => - evalMatchedPatternByStatmentsType[typeof statements](obj, statements, ...args) +const evalMatchedPattern = (obj, statements, ...args) => { + if( !isUndefined(obj) ) { + return evalMatchedPatternByStatmentsType[typeof statements](obj, statements, ...args); + } +} const primitivePatternHandler = (exp, [pattern, statements, ...args]) => { if(primitiveValueEqualTest(exp, pattern)) { @@ -57,7 +62,7 @@ const handlerByPatternType = { 'function': (exp, [pattern, statements, ...args]) => { if ('unapply' in pattern.prototype) { const extractObj = pattern.prototype.unapply(exp); - if (extractObj != undefined) { + if ( !isUndefined(extractObj) ) { return evalMatchedPattern(extractObj, statements, ...args); } } @@ -84,9 +89,9 @@ const handlerByPatternType = { const match = exp => (...pattern_clauses) => { for (const [pattern, statements, ...args] of pattern_clauses) { - const optionPrimitiveResult = handlerByPatternType[typeof pattern](exp, [pattern, statements, ...args]); - if (optionPrimitiveResult != undefined) { - return optionPrimitiveResult; + const optionResult = handlerByPatternType[typeof pattern](exp, [pattern, statements, ...args]); + if (!isUndefined(optionResult)) { + return optionResult; } }; return undefined; diff --git a/test/twice_class.spec.coffee b/test/twice_class.spec.coffee index 1f8c54e..309e758 100644 --- a/test/twice_class.spec.coffee +++ b/test/twice_class.spec.coffee @@ -18,6 +18,7 @@ object TwiceTest extends App chai = require 'chai' matchJS = require '../lib/match-js' match = matchJS.match +otherwise = matchJS.otherwise chai.expect() expect = chai.expect @@ -88,3 +89,11 @@ describe 'Matching on class contructor Twice:', -> [Twice, (x) => x] ) expect(m).to.be.equal 'Twice(6) matches value:12' + + it '9 should not match Twice, but otherwise', -> + m = match(9)( + [1, 'ONE'], + [Twice, (x) => x], + [otherwise, '9 matches otherwise'] + ) + expect(m).to.be.equal '9 matches otherwise' diff --git a/test/twice_class.spec.es5.js b/test/twice_class.spec.es5.js index f90a5e1..9b1bd06 100644 --- a/test/twice_class.spec.es5.js +++ b/test/twice_class.spec.es5.js @@ -16,7 +16,9 @@ object TwiceTest extends App { */ var chai = require('chai'); -var match = require('../lib/match-js').match; +var matchJS = require('../lib/match-js'); +var match = matchJS.match; +var otherwise = matchJS.otherwise; chai.expect(); var expect = chai.expect; @@ -89,4 +91,12 @@ describe('Matching on class contructor Twice:', function() { ); expect(m).to.be.equal('Twice(6) matches {value:12}'); }); + it('9 should not match Twice, but otherwise', function() { + var m = match(9)( + [1, 'ONE'], + [Twice, function(x) { return x; }], + [otherwise, '9 matches otherwise'] + ); + expect(m).to.be.equal('9 matches otherwise'); + }); }); diff --git a/test/twice_class.spec.es6.js b/test/twice_class.spec.es6.js index ce9ed7e..f8b7a95 100644 --- a/test/twice_class.spec.es6.js +++ b/test/twice_class.spec.es6.js @@ -16,7 +16,7 @@ object TwiceTest extends App { */ import chai from 'chai'; -import { match }from '../lib/match-js'; +import { match, otherwise }from '../lib/match-js'; chai.expect(); const expect = chai.expect; @@ -92,4 +92,12 @@ describe('Matching on class contructor Twice:', function () { ) expect(m).to.be.equal('Twice(6) matches {value:12}'); }); + it('9 should not match Twice, but otherwise', () => { + const m = match(9)( + [1, 'ONE'], + [Twice, (x) => x], + [otherwise, '9 matches otherwise'] + ) + expect(m).to.be.equal('9 matches otherwise'); + }); });