-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 61542bf
Showing
12 changed files
with
615 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
node_modules/* | ||
typings/ | ||
test/bin/* | ||
test/*.js | ||
test/*.js.map | ||
.idea/ | ||
*.js | ||
*.js.* | ||
!gulpfile.js | ||
!dist/* |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Łukasz Biały <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,39 @@ | ||
## TsPatternMatching | ||
|
||
Typesafe pattern matching emulation for TypeScript and JavaScript. | ||
|
||
###### Syntax example: | ||
|
||
```javascript | ||
class Whisky { | ||
constructor(public brand:string, public age:number, public origin:string, public price:number) {} | ||
} | ||
|
||
const dalwhinnie = new Whisky('Dalwhinnie', 15, 'Highlands', 32); | ||
|
||
var result = match<Whisky, string>(dalwhinnie) | ||
.caseOf(w => w.age > 24 && w.age <= 40, () => 'Epic!') | ||
.caseOf(w => w.age > 14 && w.age <= 24, () => 'Great') | ||
.caseOf(({brand,age}) => age > 9 && age <= 14, () => 'Neat') // Destructuring | ||
.caseOf(w => w.age <= 9, () => 'Meh...') | ||
._( _ => 'Legendary!') // Wildcard | ||
.resolve(); | ||
|
||
result.should.be.equal('Great'); | ||
``` | ||
|
||
If no wildcard value is provided and no match is provided MatchError is thrown. More examples are available in mocha.js testsuite (see below). | ||
|
||
###### Testing | ||
|
||
You need node.js. Clone repository and then: | ||
```bash | ||
$ cd TsPatternMatching && npm install && gulp | ||
``` | ||
Navigate to 'test/bin' and open index.html to see mocha tests. | ||
|
||
|
||
|
||
##### Author => Łukasz Biały => [[email protected]](mailto:[email protected]) | ||
|
||
##### License => [MIT](LICENSE) |
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,59 @@ | ||
declare module TsPatternMatching { | ||
interface Case<X> { | ||
(x: X): boolean; | ||
} | ||
interface Matched<X, Y> { | ||
(x: X): Y; | ||
} | ||
interface Resolvable<X, Y> { | ||
resolve(): Y; | ||
} | ||
interface Caseable<X, Y> extends Resolvable<X, Y> { | ||
caseOf(_case: Case<X>, _matched: Matched<X, Y>): Matchable<X, Y>; | ||
} | ||
interface Matchable<X, Y> extends Caseable<X, Y> { | ||
_(_matched: Matched<X, Y>): Default<X, Y>; | ||
} | ||
class MatchError extends Error { | ||
} | ||
class Subject<X, Y> implements Caseable<X, Y> { | ||
protected subject: X; | ||
constructor(subject: X); | ||
caseOf(_case: Case<X>, _matched: Matched<X, Y>): Match<X, Y>; | ||
resolve(): Y; | ||
} | ||
class Default<X, Y> implements Resolvable<X, Y> { | ||
protected _subject: X; | ||
protected _matched: Matched<X, Y>; | ||
protected _parent: Resolvable<X, Y>; | ||
constructor(_subject: X, _matched: Matched<X, Y>, _parent: Resolvable<X, Y>); | ||
resolve(): Y; | ||
} | ||
class Match<X, Y> implements Matchable<X, Y> { | ||
protected _subject: X; | ||
protected _case: Case<X>; | ||
protected _matched: Matched<X, Y>; | ||
protected _parent: Resolvable<X, Y>; | ||
constructor(_subject: X, _case: Case<X>, _matched: Matched<X, Y>, _parent: Resolvable<X, Y>); | ||
caseOf(_case: Case<X>, _matched: Matched<X, Y>): Matchable<X, Y>; | ||
_(_matched: Matched<X, Y>): Default<X, Y>; | ||
resolve(): Y; | ||
} | ||
function match<X, Y>(subject: X): Subject<X, Y>; | ||
} | ||
declare var module: { | ||
exports: any; | ||
require(id: string): any; | ||
id: string; | ||
filename: string; | ||
loaded: boolean; | ||
parent: any; | ||
children: any[]; | ||
}; | ||
/** | ||
* @name tspatternmatching | ||
* @namespace Hold classes and functions related to TsPatternMatching library. | ||
*/ | ||
declare module 'tspatternmatching' { | ||
export = TsPatternMatching; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,81 @@ | ||
var gulp = require('gulp'); | ||
var typescript = require('gulp-tsc'); | ||
var maps = require('gulp-sourcemaps'); | ||
var del = require('del'); | ||
var seq = require('run-sequence'); | ||
var tsd = require('gulp-tsd'); | ||
|
||
var tsOpts = { | ||
outDir: 'dist', | ||
declaration: true, | ||
noImplicitAny: true, | ||
removeComments: false, | ||
target: 'ES5', | ||
sourceMap: true, | ||
module: 'commonjs' | ||
}; | ||
|
||
var paths = { | ||
dist: './dist', | ||
test: './test', | ||
testBin: './test/bin' | ||
}; | ||
|
||
var files = { | ||
main: 'tspatternmatching.ts', | ||
test: {index: 'index.html', ts: 'test.ts', js: 'test.js', jsmap: 'test.js.map'} | ||
}; | ||
|
||
gulp.task('compile:src', function () { | ||
return gulp.src(files.main) | ||
.pipe(maps.init()) | ||
.pipe(typescript(tsOpts)) | ||
.pipe(maps.write('./')) | ||
.pipe(gulp.dest(paths.dist)); | ||
}); | ||
|
||
gulp.task('compile:test', function () { | ||
var tsTest = gulp.src([ | ||
paths.test + '/' + files.test.ts | ||
]); | ||
|
||
// clone opts | ||
var testOpts = JSON.parse(JSON.stringify(tsOpts)); | ||
testOpts.outDir = paths.test; | ||
testOpts.declaration = false; | ||
tsTest.sourceMap = false; | ||
|
||
return tsTest | ||
.pipe(typescript(testOpts)) | ||
.pipe(gulp.dest((paths.test))); | ||
}); | ||
|
||
gulp.task('build-tests', function () { | ||
return gulp.src([ | ||
'./dist/*.js', | ||
paths.test + '/' + files.test.js, | ||
paths.test + '/' + files.test.index | ||
]) | ||
.pipe(gulp.dest(paths.testBin)); | ||
}); | ||
|
||
gulp.task('compile', function () { | ||
return seq('tsd:install', 'clean', 'compile:src', 'compile:test', 'build-tests'); | ||
}); | ||
|
||
gulp.task('tsd:install', function (callback) { | ||
tsd({ | ||
command: 'reinstall', | ||
config: './tsd.json' | ||
}, callback); | ||
}); | ||
|
||
gulp.task('clean', function () { | ||
return del([ | ||
paths.test + '/' + files.test.js, | ||
paths.test + '/' + files.test.jsmap, | ||
paths.testBin | ||
]); | ||
}); | ||
|
||
gulp.task('default', ['compile']); |
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,42 @@ | ||
{ | ||
"name": "tspatternmatching", | ||
"version": "0.1.0", | ||
"description": "Typesafe pattern matching emulation for TypeScript", | ||
"main": "dist/tspatternmatching.js", | ||
"files": [ | ||
"dist", | ||
"tspatternmatching.ts", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"directories": {}, | ||
"scripts": { | ||
"test": "gulp test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lbialy/TsPatternMatching" | ||
}, | ||
"keywords": [ | ||
"pattern matching", | ||
"typesafe", | ||
"typescript" | ||
], | ||
"author": { | ||
"name": "Łukasz Biały", | ||
"url": "https://github.com/lbialy" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"del": "^2.0.2", | ||
"gulp": "^3.9.0", | ||
"gulp-sourcemaps": "^1.6.0", | ||
"gulp-tsc": "^1.1.1", | ||
"gulp-tsd": "0.0.4", | ||
"mocha": "^2.3.3", | ||
"run-sequence": "^1.1.4", | ||
"should": "^7.1.1", | ||
"tsd": "^0.6.5", | ||
"typescript": "^1.6.2" | ||
} | ||
} |
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,23 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Pattern Matching tests</title> | ||
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<div id="mocha"></div> | ||
|
||
<script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script> | ||
<script src="https://cdn.rawgit.com/shouldjs/should.js/master/should.min.js"></script> | ||
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script> | ||
|
||
<script>mocha.setup('bdd')</script> | ||
<script src="tspatternmatching.js"></script> | ||
<script src="test.js"></script> | ||
<script> | ||
mocha.checkLeaks(); | ||
mocha.globals(['jQuery']); | ||
mocha.run(); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.