diff --git a/src/index.js b/src/index.js index 27f3bb2..6a7c179 100644 --- a/src/index.js +++ b/src/index.js @@ -97,9 +97,11 @@ const _ = { return null; } } +const otherwise = _ const id = (x) => x export default { match, _, + otherwise, id, } diff --git a/test/misc.spec.coffee b/test/misc.spec.coffee index 2c17856..df6a123 100644 --- a/test/misc.spec.coffee +++ b/test/misc.spec.coffee @@ -3,6 +3,7 @@ matchJS = require '../lib/match-js' match = matchJS.match _ = matchJS._ id = matchJS.id +otherwise = matchJS.otherwise chai.expect() expect = chai.expect @@ -24,14 +25,22 @@ describe 'Miscellaneous useages', -> ) expect(m).to.be.equal 'Match 1 2 3' - it 'default pattern that matches everything', -> + it 'default pattern (_) that matches everything', -> i = 3 m = match(i)( [1, "ONE"], [2, "TOW"], - [_, ()->"ANY is "+i+""] + [_, ()->"_ matches "+i] ) - expect(m).to.be.equal 'ANY is 3' + expect(m).to.be.equal '_ matches 3' + it 'default pattern (otherwise) that matches everything', -> + i = 3 + m = match(i)( + [1, "ONE"], + [2, "TOW"], + [otherwise, ()->"otherwise matches "+i] + ) + expect(m).to.be.equal 'otherwise matches 3' it 'id functions returns the same input value', -> x = 10 diff --git a/test/misc.spec.es5.js b/test/misc.spec.es5.js index 13a3683..24717e0 100644 --- a/test/misc.spec.es5.js +++ b/test/misc.spec.es5.js @@ -1,8 +1,9 @@ var chai = require('chai'); -var matchjs = require('../lib/match-js'); -var match = matchjs.match; -var _ = matchjs._; -var id = matchjs.id; +var matchJS = require('../lib/match-js'); +var match = matchJS.match; +var _ = matchJS._; +var id = matchJS.id; +var otherwise = matchJS.otherwise; chai.expect(); var expect = chai.expect; @@ -25,15 +26,25 @@ describe('Miscellaneous useages', function() { ) expect(m).to.be.equal('Match 1 2 3'); }); - it('wildcard pattern that matches everything', function() { + it('default pattern (_) that matches everything', function() { var i = 3; var m = match(i)( [1, "ONE"], [2, "TOW"], - [_, function(){ return "ANY is "+i; }] + [_, function(){ return "_ matches "+i; }] ) - expect(m).to.be.equal('ANY is 3'); + expect(m).to.be.equal('_ matches 3'); }); + it('default pattern (otherwise) that matches everything', function() { + var i = 3; + var m = match(i)( + [1, "ONE"], + [2, "TOW"], + [otherwise, function(){ return "otherwise matches "+i; }] + ) + expect(m).to.be.equal('otherwise matches 3'); + }); + it('id functions returns the same input value', function() { var x = 10; var m = match(x)( diff --git a/test/misc.spec.es6.js b/test/misc.spec.es6.js index 4aa9a37..b6117eb 100644 --- a/test/misc.spec.es6.js +++ b/test/misc.spec.es6.js @@ -1,5 +1,5 @@ import chai from 'chai'; -import {_, id, match} from '../lib/match-js'; +import {_, otherwise, id, match} from '../lib/match-js'; chai.expect(); const expect = chai.expect; @@ -22,14 +22,23 @@ describe('Miscellaneous useages', function () { ) expect(m).to.be.equal('Match 1 2 3'); }); - it('default pattern that matches everything', () => { + it('default pattern (_) that matches everything', () => { const i = 3; const m = match(i)( [1, "ONE"], [2, "TOW"], - [_, ()=>"ANY is "+i+""] + [_, ()=>"_ matches "+i] ) - expect(m).to.be.equal('ANY is 3'); + expect(m).to.be.equal('_ matches 3'); + }); + it('default pattern (otherwise) that matches everything', () => { + const i = 3; + const m = match(i)( + [1, "ONE"], + [2, "TOW"], + [otherwise, ()=>"otherwise matches "+i+""] + ) + expect(m).to.be.equal('otherwise matches 3'); }); it('id functions returns the same input value', () => { const x = 10;