From 149f450baf35f9a956f886a7254543a62afbf4f2 Mon Sep 17 00:00:00 2001 From: jhe Date: Wed, 6 Jul 2016 15:56:43 +0100 Subject: [PATCH] feat(API): support CASE(,,,) as an alias to array[,,,] close#2 --- .eslintrc.js | 4 ---- src/index.js | 6 +++++- test/misc.spec.coffee | 34 ++++++++++++++++++++++----------- test/misc.spec.es5.js | 16 +++++++++++++--- test/misc.spec.es6.js | 34 ++++++++++++++++++++------------- test/primitive_type.spec.coffee | 28 +++++++++++++-------------- test/primitive_type.spec.es6.js | 28 +++++++++++++-------------- test/twice_class.spec.coffee | 32 +++++++++++++++---------------- test/twice_class.spec.es6.js | 32 +++++++++++++++---------------- 9 files changed, 122 insertions(+), 92 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index b44a71b..684a20e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,10 +10,6 @@ module.exports = { "error", 2 ], - "linebreak-style": [ - "error", - "unix" - ], "quotes": [ "error", "single" diff --git a/src/index.js b/src/index.js index 6a7c179..1d1c617 100644 --- a/src/index.js +++ b/src/index.js @@ -97,11 +97,15 @@ const _ = { return null; } } +const CASE = (...args) => args const otherwise = _ const id = (x) => x + + export default { match, _, - otherwise, + CASE, id, + otherwise, } diff --git a/test/misc.spec.coffee b/test/misc.spec.coffee index 99615a6..ddc522f 100644 --- a/test/misc.spec.coffee +++ b/test/misc.spec.coffee @@ -4,6 +4,7 @@ match = matchJS.match _ = matchJS._ id = matchJS.id otherwise = matchJS.otherwise +CASE = matchJS.CASE chai.expect() expect = chai.expect @@ -11,15 +12,15 @@ 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' @@ -27,25 +28,36 @@ describe 'Miscellaneous useages', -> 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 diff --git a/test/misc.spec.es5.js b/test/misc.spec.es5.js index 6059a1e..79bc1f3 100644 --- a/test/misc.spec.es5.js +++ b/test/misc.spec.es5.js @@ -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; @@ -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'); @@ -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'); @@ -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'); + }); }); diff --git a/test/misc.spec.es6.js b/test/misc.spec.es6.js index b6117eb..8c5e75c 100644 --- a/test/misc.spec.es6.js +++ b/test/misc.spec.es6.js @@ -1,5 +1,5 @@ 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; @@ -7,9 +7,9 @@ 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'); @@ -17,7 +17,7 @@ describe('Miscellaneous useages', function () { 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'); @@ -25,29 +25,37 @@ describe('Miscellaneous useages', function () { 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'); + }); }); diff --git a/test/primitive_type.spec.coffee b/test/primitive_type.spec.coffee index fa38dfa..42dc15f 100644 --- a/test/primitive_type.spec.coffee +++ b/test/primitive_type.spec.coffee @@ -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 diff --git a/test/primitive_type.spec.es6.js b/test/primitive_type.spec.es6.js index 14ae838..fca034d 100644 --- a/test/primitive_type.spec.es6.js +++ b/test/primitive_type.spec.es6.js @@ -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'); }); diff --git a/test/twice_class.spec.coffee b/test/twice_class.spec.coffee index d2432f5..1f8c54e 100644 --- a/test/twice_class.spec.coffee +++ b/test/twice_class.spec.coffee @@ -39,25 +39,25 @@ describe 'Matching on class contructor Twice:', -> it '10 should match Twice(x), where x is assigned to 5', -> m = match(10)( - [1, "ONE"], - [2, "TWO"], + [1, 'ONE'], + [2, 'TWO'], [Twice, (x) => x] ) expect(m).to.be.equal 5 it '10 should match Twice(5)', -> m = match(10)( - [1, "ONE"], - [2, "TWO"], - [new Twice(5), "Twice(5)"] + [1, 'ONE'], + [2, 'TWO'], + [new Twice(5), 'Twice(5)'] ) - expect(m).to.be.equal "Twice(5)" + expect(m).to.be.equal 'Twice(5)' it 'Twice(5) should match Twice(x), where x is assigned to 5', -> twice5 = new Twice(5) m = match(twice5)( - [1, "ONE"], - [2, "TWO"], + [1, 'ONE'], + [2, 'TWO'], [Twice, (x) => x] ) expect(m).to.be.equal 5 @@ -65,17 +65,17 @@ describe 'Matching on class contructor Twice:', -> it 'Twice(5) should match new Twice(5), before match with Twice(x)', -> twice5 = new Twice(5) m = match(twice5)( - [1, "ONE"], - [new Twice(5), "Twice(5)"], + [1, 'ONE'], + [new Twice(5), 'Twice(5)'], [Twice, (x) => x] ) - expect(m).to.be.equal "Twice(5)" + expect(m).to.be.equal 'Twice(5)' it 'Twice(5) should not match Twice(6), but Twice(x)', -> twice5 = new Twice(5) m = match(twice5)( - [1, "ONE"], - [new Twice(6), "Twice(6)"], + [1, 'ONE'], + [new Twice(6), 'Twice(6)'], [Twice, (x) => x] ) expect(m).to.be.equal 5 @@ -83,8 +83,8 @@ describe 'Matching on class contructor Twice:', -> it 'Twice(6) should match value: 12', -> twice6 = new Twice(6) m = match(twice6)( - [1, "ONE"], - [value: 12, "Twice(6) matches value:12"], + [1, 'ONE'], + [value: 12, 'Twice(6) matches value:12'], [Twice, (x) => x] ) - expect(m).to.be.equal "Twice(6) matches value:12" + expect(m).to.be.equal 'Twice(6) matches value:12' diff --git a/test/twice_class.spec.es6.js b/test/twice_class.spec.es6.js index a7fde50..ce9ed7e 100644 --- a/test/twice_class.spec.es6.js +++ b/test/twice_class.spec.es6.js @@ -42,25 +42,25 @@ describe('Matching on class contructor Twice:', function () { it('10 should match Twice(x), where x is assigned to 5', () => { const m = match(10)( - [1, "ONE"], - [2, "TWO"], + [1, 'ONE'], + [2, 'TWO'], [Twice, (x) => x] ) expect(m).to.be.equal(5); }); it('10 should match Twice(5)', () => { const m = match(10)( - [1, "ONE"], - [2, "TWO"], - [new Twice(5), "Twice(5)"] + [1, 'ONE'], + [2, 'TWO'], + [new Twice(5), 'Twice(5)'] ) - expect(m).to.be.equal("Twice(5)"); + expect(m).to.be.equal('Twice(5)'); }); it('Twice(5) should match Twice(x), where x is assigned to 5', () => { const twice5 = new Twice(5); const m = match(twice5)( - [1, "ONE"], - [2, "TWO"], + [1, 'ONE'], + [2, 'TWO'], [Twice, (x) => x] ) expect(m).to.be.equal(5); @@ -68,17 +68,17 @@ describe('Matching on class contructor Twice:', function () { it('Twice(5) should match new Twice(5), before match with Twice(x)', () => { const twice5 = new Twice(5); const m = match(twice5)( - [1, "ONE"], - [new Twice(5), "Twice(5)"], + [1, 'ONE'], + [new Twice(5), 'Twice(5)'], [Twice, (x) => x] ) - expect(m).to.be.equal("Twice(5)"); + expect(m).to.be.equal('Twice(5)'); }); it('Twice(5) should not match Twice(6), but Twice(x)', () => { const twice5 = new Twice(5); const m = match(twice5)( - [1, "ONE"], - [new Twice(6), "Twice(6)"], + [1, 'ONE'], + [new Twice(6), 'Twice(6)'], [Twice, (x) => x] ) expect(m).to.be.equal(5); @@ -86,10 +86,10 @@ describe('Matching on class contructor Twice:', function () { it('Twice(6) should match {value: 12}', () => { const twice6 = new Twice(6); const m = match(twice6)( - [1, "ONE"], - [{value: 12}, "Twice(6) matches {value:12}"], + [1, 'ONE'], + [{value: 12}, 'Twice(6) matches {value:12}'], [Twice, (x) => x] ) - expect(m).to.be.equal("Twice(6) matches {value:12}"); + expect(m).to.be.equal('Twice(6) matches {value:12}'); }); });