Skip to content

Commit 61542bf

Browse files
committed
initial, 0.1.0
0 parents  commit 61542bf

File tree

12 files changed

+615
-0
lines changed

12 files changed

+615
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
node_modules/*
2+
typings/
3+
test/bin/*
4+
test/*.js
5+
test/*.js.map
6+
.idea/
7+
*.js
8+
*.js.*
9+
!gulpfile.js
10+
!dist/*

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Łukasz Biały <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## TsPatternMatching
2+
3+
Typesafe pattern matching emulation for TypeScript and JavaScript.
4+
5+
###### Syntax example:
6+
7+
```javascript
8+
class Whisky {
9+
constructor(public brand:string, public age:number, public origin:string, public price:number) {}
10+
}
11+
12+
const dalwhinnie = new Whisky('Dalwhinnie', 15, 'Highlands', 32);
13+
14+
var result = match<Whisky, string>(dalwhinnie)
15+
.caseOf(w => w.age > 24 && w.age <= 40, () => 'Epic!')
16+
.caseOf(w => w.age > 14 && w.age <= 24, () => 'Great')
17+
.caseOf(({brand,age}) => age > 9 && age <= 14, () => 'Neat') // Destructuring
18+
.caseOf(w => w.age <= 9, () => 'Meh...')
19+
._( _ => 'Legendary!') // Wildcard
20+
.resolve();
21+
22+
result.should.be.equal('Great');
23+
```
24+
25+
If no wildcard value is provided and no match is provided MatchError is thrown. More examples are available in mocha.js testsuite (see below).
26+
27+
###### Testing
28+
29+
You need node.js. Clone repository and then:
30+
```bash
31+
$ cd TsPatternMatching && npm install && gulp
32+
```
33+
Navigate to 'test/bin' and open index.html to see mocha tests.
34+
35+
36+
37+
##### Author => Łukasz Biały => [[email protected]](mailto:[email protected])
38+
39+
##### License => [MIT](LICENSE)

dist/tspatternmatching.d.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
declare module TsPatternMatching {
2+
interface Case<X> {
3+
(x: X): boolean;
4+
}
5+
interface Matched<X, Y> {
6+
(x: X): Y;
7+
}
8+
interface Resolvable<X, Y> {
9+
resolve(): Y;
10+
}
11+
interface Caseable<X, Y> extends Resolvable<X, Y> {
12+
caseOf(_case: Case<X>, _matched: Matched<X, Y>): Matchable<X, Y>;
13+
}
14+
interface Matchable<X, Y> extends Caseable<X, Y> {
15+
_(_matched: Matched<X, Y>): Default<X, Y>;
16+
}
17+
class MatchError extends Error {
18+
}
19+
class Subject<X, Y> implements Caseable<X, Y> {
20+
protected subject: X;
21+
constructor(subject: X);
22+
caseOf(_case: Case<X>, _matched: Matched<X, Y>): Match<X, Y>;
23+
resolve(): Y;
24+
}
25+
class Default<X, Y> implements Resolvable<X, Y> {
26+
protected _subject: X;
27+
protected _matched: Matched<X, Y>;
28+
protected _parent: Resolvable<X, Y>;
29+
constructor(_subject: X, _matched: Matched<X, Y>, _parent: Resolvable<X, Y>);
30+
resolve(): Y;
31+
}
32+
class Match<X, Y> implements Matchable<X, Y> {
33+
protected _subject: X;
34+
protected _case: Case<X>;
35+
protected _matched: Matched<X, Y>;
36+
protected _parent: Resolvable<X, Y>;
37+
constructor(_subject: X, _case: Case<X>, _matched: Matched<X, Y>, _parent: Resolvable<X, Y>);
38+
caseOf(_case: Case<X>, _matched: Matched<X, Y>): Matchable<X, Y>;
39+
_(_matched: Matched<X, Y>): Default<X, Y>;
40+
resolve(): Y;
41+
}
42+
function match<X, Y>(subject: X): Subject<X, Y>;
43+
}
44+
declare var module: {
45+
exports: any;
46+
require(id: string): any;
47+
id: string;
48+
filename: string;
49+
loaded: boolean;
50+
parent: any;
51+
children: any[];
52+
};
53+
/**
54+
* @name tspatternmatching
55+
* @namespace Hold classes and functions related to TsPatternMatching library.
56+
*/
57+
declare module 'tspatternmatching' {
58+
export = TsPatternMatching;
59+
}

dist/tspatternmatching.js

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tspatternmatching.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var gulp = require('gulp');
2+
var typescript = require('gulp-tsc');
3+
var maps = require('gulp-sourcemaps');
4+
var del = require('del');
5+
var seq = require('run-sequence');
6+
var tsd = require('gulp-tsd');
7+
8+
var tsOpts = {
9+
outDir: 'dist',
10+
declaration: true,
11+
noImplicitAny: true,
12+
removeComments: false,
13+
target: 'ES5',
14+
sourceMap: true,
15+
module: 'commonjs'
16+
};
17+
18+
var paths = {
19+
dist: './dist',
20+
test: './test',
21+
testBin: './test/bin'
22+
};
23+
24+
var files = {
25+
main: 'tspatternmatching.ts',
26+
test: {index: 'index.html', ts: 'test.ts', js: 'test.js', jsmap: 'test.js.map'}
27+
};
28+
29+
gulp.task('compile:src', function () {
30+
return gulp.src(files.main)
31+
.pipe(maps.init())
32+
.pipe(typescript(tsOpts))
33+
.pipe(maps.write('./'))
34+
.pipe(gulp.dest(paths.dist));
35+
});
36+
37+
gulp.task('compile:test', function () {
38+
var tsTest = gulp.src([
39+
paths.test + '/' + files.test.ts
40+
]);
41+
42+
// clone opts
43+
var testOpts = JSON.parse(JSON.stringify(tsOpts));
44+
testOpts.outDir = paths.test;
45+
testOpts.declaration = false;
46+
tsTest.sourceMap = false;
47+
48+
return tsTest
49+
.pipe(typescript(testOpts))
50+
.pipe(gulp.dest((paths.test)));
51+
});
52+
53+
gulp.task('build-tests', function () {
54+
return gulp.src([
55+
'./dist/*.js',
56+
paths.test + '/' + files.test.js,
57+
paths.test + '/' + files.test.index
58+
])
59+
.pipe(gulp.dest(paths.testBin));
60+
});
61+
62+
gulp.task('compile', function () {
63+
return seq('tsd:install', 'clean', 'compile:src', 'compile:test', 'build-tests');
64+
});
65+
66+
gulp.task('tsd:install', function (callback) {
67+
tsd({
68+
command: 'reinstall',
69+
config: './tsd.json'
70+
}, callback);
71+
});
72+
73+
gulp.task('clean', function () {
74+
return del([
75+
paths.test + '/' + files.test.js,
76+
paths.test + '/' + files.test.jsmap,
77+
paths.testBin
78+
]);
79+
});
80+
81+
gulp.task('default', ['compile']);

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "tspatternmatching",
3+
"version": "0.1.0",
4+
"description": "Typesafe pattern matching emulation for TypeScript",
5+
"main": "dist/tspatternmatching.js",
6+
"files": [
7+
"dist",
8+
"tspatternmatching.ts",
9+
"LICENSE",
10+
"README.md"
11+
],
12+
"directories": {},
13+
"scripts": {
14+
"test": "gulp test"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/lbialy/TsPatternMatching"
19+
},
20+
"keywords": [
21+
"pattern matching",
22+
"typesafe",
23+
"typescript"
24+
],
25+
"author": {
26+
"name": "Łukasz Biały",
27+
"url": "https://github.com/lbialy"
28+
},
29+
"license": "MIT",
30+
"devDependencies": {
31+
"del": "^2.0.2",
32+
"gulp": "^3.9.0",
33+
"gulp-sourcemaps": "^1.6.0",
34+
"gulp-tsc": "^1.1.1",
35+
"gulp-tsd": "0.0.4",
36+
"mocha": "^2.3.3",
37+
"run-sequence": "^1.1.4",
38+
"should": "^7.1.1",
39+
"tsd": "^0.6.5",
40+
"typescript": "^1.6.2"
41+
}
42+
}

test/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<title>Pattern Matching tests</title>
5+
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
6+
</head>
7+
<body>
8+
<div id="mocha"></div>
9+
10+
<script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script>
11+
<script src="https://cdn.rawgit.com/shouldjs/should.js/master/should.min.js"></script>
12+
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
13+
14+
<script>mocha.setup('bdd')</script>
15+
<script src="tspatternmatching.js"></script>
16+
<script src="test.js"></script>
17+
<script>
18+
mocha.checkLeaks();
19+
mocha.globals(['jQuery']);
20+
mocha.run();
21+
</script>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)