-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Scholtes
committed
Nov 11, 2016
1 parent
2d2ad7f
commit a226167
Showing
10 changed files
with
2,259 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/index.js | ||
/caseof-eq.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
language: node_js | ||
node_js: | ||
- "6" | ||
- "4" | ||
node_js: [4, 6] | ||
install: | ||
- npm install --global yarn | ||
- yarn install | ||
script: | ||
- npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function caseof (target, cases) { | ||
for (const i in cases) { | ||
const patterns = cases[i].slice() | ||
const handler = patterns.pop() | ||
const matches = patterns.some((p) => p === target || p === otherwise) | ||
if (matches) { | ||
return handler(target) | ||
} | ||
} | ||
|
||
throw new Error('Unmatched case for ' + target) | ||
} | ||
|
||
export const otherwise = {} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {otherwise, caseof} from '../src/caseof-eq.js' | ||
import assert from 'assert' | ||
|
||
describe('#caseof() with equality match', function () { | ||
it('matches correct case', function () { | ||
const cases = [ | ||
['A', () => 'A'], | ||
['B', () => 'B'], | ||
['C', 'D', () => 'CD'], | ||
[otherwise, () => null], | ||
] | ||
|
||
assert.equal(caseof('A', cases), 'A') | ||
assert.equal(caseof('B', cases), 'B') | ||
assert.equal(caseof('C', cases), 'CD') | ||
assert.equal(caseof('D', cases), 'CD') | ||
assert.equal(caseof('X', cases), null) | ||
}) | ||
|
||
it('throws for unmatched target', function () { | ||
assert.throws(() => { | ||
caseof('A', [['B', () => null]]) | ||
}, /Unmatched case for/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.