Skip to content

Commit

Permalink
feat(pattern): add otherwise as alias to _, both matches everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiansen committed Jul 3, 2016
1 parent 1fa4753 commit e3ac117
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ const _ = {
return null;
}
}
const otherwise = _
const id = (x) => x
export default {
match,
_,
otherwise,
id,
}
15 changes: 12 additions & 3 deletions test/misc.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ matchJS = require '../lib/match-js'
match = matchJS.match
_ = matchJS._
id = matchJS.id
otherwise = matchJS.otherwise

chai.expect()
expect = chai.expect
Expand All @@ -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
Expand Down
25 changes: 18 additions & 7 deletions test/misc.spec.es5.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)(
Expand Down
17 changes: 13 additions & 4 deletions test/misc.spec.es6.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit e3ac117

Please sign in to comment.