Skip to content

Commit

Permalink
fix(undefined check): a) undefined check in JS 2) when 2nd para is a …
Browse files Browse the repository at this point in the history
…function, only apply when extra
  • Loading branch information
jhe committed Jul 7, 2016
1 parent 7ca52fd commit c37430d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const isUndefined = (value) => typeof value === 'undefined'

const evalMatchedPatternByStatmentsType = {
'string': (_obj, value, _args) => value,
'number': (_obj, value, _args) => value,
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions test/twice_class.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
12 changes: 11 additions & 1 deletion test/twice_class.spec.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
});
});
10 changes: 9 additions & 1 deletion test/twice_class.spec.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
});
});

0 comments on commit c37430d

Please sign in to comment.