Skip to content

Commit

Permalink
feat(API): support CASE(,,,) as an alias to array[,,,] close#2
Browse files Browse the repository at this point in the history
  • Loading branch information
jhe committed Jul 6, 2016
1 parent ae4f1f9 commit 149f450
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 92 deletions.
4 changes: 0 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ module.exports = {
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ const _ = {
return null;
}
}
const CASE = (...args) => args
const otherwise = _
const id = (x) => x


export default {
match,
_,
otherwise,
CASE,
id,
otherwise,
}
34 changes: 23 additions & 11 deletions test/misc.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,60 @@ match = matchJS.match
_ = matchJS._
id = matchJS.id
otherwise = matchJS.otherwise
CASE = matchJS.CASE

chai.expect()
expect = chai.expect

describe 'Miscellaneous useages', ->
it 'execute statements should be allowed', ->
m = match(10)(
[1, "ONE"],
[10, ( ->"TEN"), null]
[1, 'ONE'],
[10, ( ->'TEN'), null]
)
expect(m).to.be.equal 'TEN'

it 'pass additional parameters to matched patial function', ->
m = match(1)(
[1, (x,y,z)->
"Match "+x+" "+y+" "+z
'Match '+x+' '+y+' '+z
, 2,3]
)
expect(m).to.be.equal 'Match 1 2 3'

it 'default pattern (_) that matches everything', ->
i = 3
m = match(i)(
[1, "ONE"],
[2, "TOW"],
[_, ()->"_ matches "+i]
[1, 'ONE'],
[2, 'TWO'],
[_, ()->'_ matches '+i]
)
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]
[1, 'ONE'],
[2, 'TWO'],
[otherwise, ()->'otherwise matches '+i]
)
expect(m).to.be.equal 'otherwise matches 3'

it 'id functions returns the same input value', ->
x = 10
m = match(x)(
[1, "ONE"],
[2, "TOW"],
[1, 'ONE'],
[2, 'TWO'],
[10, id]
)
expect(m).to.be.equal x

it 'CASE statements test', ->
i = 10;
m = match(i)(
CASE(1, ( (x,y,z)-> 'Match '+x+' '+y+' '+z ), 2,3),
CASE(2, 'TWO'),
CASE(_, ->'_ matches '+i)
)
expect(m).to.be.equal '_ matches 10'

return
16 changes: 13 additions & 3 deletions test/misc.spec.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var match = matchJS.match;
var _ = matchJS._;
var id = matchJS.id;
var otherwise = matchJS.otherwise;
var CASE = matchJS.CASE;

chai.expect();
var expect = chai.expect;
Expand All @@ -30,7 +31,7 @@ describe('Miscellaneous useages', function() {
var i = 3;
var m = match(i)(
[1, 'ONE'],
[2, 'TOW'],
[2, 'TWO'],
[_, function(){ return '_ matches '+i; }]
);
expect(m).to.be.equal('_ matches 3');
Expand All @@ -39,7 +40,7 @@ describe('Miscellaneous useages', function() {
var i = 3;
var m = match(i)(
[1, 'ONE'],
[2, 'TOW'],
[2, 'TWO'],
[otherwise, function(){ return 'otherwise matches '+i; }]
);
expect(m).to.be.equal('otherwise matches 3');
Expand All @@ -49,10 +50,19 @@ describe('Miscellaneous useages', function() {
var x = 10;
var m = match(x)(
[1, 'ONE'],
[2, 'TOW'],
[2, 'TWO'],
[10, id]
);
expect(m).to.be.equal(x);
});

it('CASE statements test', function() {
var i = 10;
var m = match(i)(
CASE(1, function(x,y,z){ return 'Match '+x+' '+y+' '+z; }, 2,3),
CASE(2, 'TWO'),
CASE(_, function(){ return '_ matches '+i; })
);
expect(m).to.be.equal('_ matches 10');
});
});
34 changes: 21 additions & 13 deletions test/misc.spec.es6.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
import chai from 'chai';
import {_, otherwise, id, match} from '../lib/match-js';
import {match, _, otherwise, id, CASE} from '../lib/match-js';

chai.expect();
const expect = chai.expect;

describe('Miscellaneous useages', function () {
it('execute statements should be allowed', () => {
const m = match(10)(
[1, "ONE"],
[1, 'ONE'],
[10, ()=>{
return "TEN";
return 'TEN';
}, null]
)
expect(m).to.be.equal('TEN');
});
it('pass additional parameters to matched patial function', () => {
const m = match(1)(
[1, (x,y,z)=>{
return "Match "+x+" "+y+" "+z;
return 'Match '+x+' '+y+' '+z;
}, 2,3]
)
expect(m).to.be.equal('Match 1 2 3');
});
it('default pattern (_) that matches everything', () => {
const i = 3;
const m = match(i)(
[1, "ONE"],
[2, "TOW"],
[_, ()=>"_ matches "+i]
[1, 'ONE'],
[2, 'TWO'],
[_, ()=>'_ matches '+i]
)
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+""]
[1, 'ONE'],
[2, 'TWO'],
[otherwise, ()=>'otherwise matches '+i+'']
)
expect(m).to.be.equal('otherwise matches 3');
});
it('id functions returns the same input value', () => {
const x = 10;
const m = match(x)(
[1, "ONE"],
[2, "TOW"],
[1, 'ONE'],
[2, 'TWO'],
[10, id]
)
expect(m).to.be.equal(x);
});

it('CASE statements test', function() {
const i = 10;
const m = match(i)(
CASE(1, (x,y,z)=>{ return 'Match '+x+' '+y+' '+z; }, 2,3),
CASE(2, 'TWO'),
CASE(_, ()=>'_ matches '+i)
);
expect(m).to.be.equal('_ matches 10');
});
});
28 changes: 14 additions & 14 deletions test/primitive_type.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,49 @@ expect = chai.expect
describe 'Matching on build-in primitive types', ->
it 'Integer value 10 should match value 10', ->
m = match(10)(
[1, "ONE"],
[10, "TEN"]
[1, 'ONE'],
[10, 'TEN']
)
expect(m).to.be.equal 'TEN'

it 'Integer veriable of value 2 should match the value 2', ->
two = 2;
m = match(two)(
[1, "ONE"],
[2, "TWO"]
[1, 'ONE'],
[2, 'TWO']
)
expect(m).to.be.equal 'TWO'


it 'Boolean value true should match value true', ->
m = match(true)(
[1, "ONE"],
[10, "TEN"],
[true, "TRUE"]
[1, 'ONE'],
[10, 'TEN'],
[true, 'TRUE']
)
expect(m).to.be.equal 'TRUE'


it 'String value \'Hello World\' should match value \'Hello World\'', ->
m = match('Hello World')(
[1, "ONE"],
[10, "TEN"],
['Hello World', "Hello World Matched"]
[1, 'ONE'],
[10, 'TEN'],
['Hello World', 'Hello World Matched']
)
expect(m).to.be.equal 'Hello World Matched'


it 'Integer 1 should match int 1, not String \'1\'', ->
m = match(1)(
['1', "String ONE"],
[1, "Integer ONE"]
['1', 'String ONE'],
[1, 'Integer ONE']
)
expect(m).to.be.equal 'Integer ONE'

it 'String \'1\' should match String \'1\', not int 1', ->
m = match(1)(
[1, "Integer ONE"],
['1', "String ONE"]
[1, 'Integer ONE'],
['1', 'String ONE']
)
expect(m).to.be.equal 'Integer ONE'
return
28 changes: 14 additions & 14 deletions test/primitive_type.spec.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@ const expect = chai.expect;
describe('Matching on build-in primitive types', function () {
it('Integer value 10 should match value 10', () => {
const m = match(10)(
[1, "ONE"],
[10, "TEN"]
[1, 'ONE'],
[10, 'TEN']
)
expect(m).to.be.equal('TEN');
});
it('Integer veriable of value 2 should match the value 2', () => {
const two = 2;
const m = match(two)(
[1, "ONE"],
[2, "TWO"]
[1, 'ONE'],
[2, 'TWO']
)
expect(m).to.be.equal('TWO');
});

it('Boolean value true should match value true', () => {
const m = match(true)(
[1, "ONE"],
[10, "TEN"],
[true, "TRUE"]
[1, 'ONE'],
[10, 'TEN'],
[true, 'TRUE']
)
expect(m).to.be.equal('TRUE');
});

it('String value \'Hello World\' should match value \'Hello World\'', () => {
const m = match('Hello World')(
[1, "ONE"],
[10, "TEN"],
['Hello World', "Hello World Matched"]
[1, 'ONE'],
[10, 'TEN'],
['Hello World', 'Hello World Matched']
)
expect(m).to.be.equal('Hello World Matched');
});

it('Integer 1 should match int 1, not String \'1\'', () => {
const m = match(1)(
['1', "String ONE"],
[1, "Integer ONE"]
['1', 'String ONE'],
[1, 'Integer ONE']
)
expect(m).to.be.equal('Integer ONE');
});
it('String \'1\' should match String \'1\', not int 1', () => {
const m = match(1)(
[1, "Integer ONE"],
['1', "String ONE"]
[1, 'Integer ONE'],
['1', 'String ONE']
)
expect(m).to.be.equal('Integer ONE');
});
Expand Down
Loading

1 comment on commit 149f450

@Jiansen
Copy link
Owner

@Jiansen Jiansen commented on 149f450 Jul 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close #2

Please sign in to comment.