Skip to content

Instantly share code, notes, and snippets.

@davidchase
Created September 26, 2016 15:45
Show Gist options
  • Save davidchase/94cc7f8ca3eee4f6df4908d3070668a2 to your computer and use it in GitHub Desktop.
Save davidchase/94cc7f8ca3eee4f6df4908d3070668a2 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
console.clear()
import t from 'tcomb'
import Type from 'union-type'
const log = console.log.bind(console)
const tType = obj => Object
.keys(obj)
.reduce((o, key) => (o[key] = t.subtype(obj[key], x => obj[key](x) === x, key), o), {})
const Actions = tType({'Up': t.String, 'Down': t.Any, 'Fire': t.Number})
const result = action => t.match(action,
Actions.Up, x => x,
Actions.Down, y => 'downer',
t.Any, x => 'boom'
)
log(result('Down'))
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"tcomb": "3.2.14",
"union-type": "0.3.2",
"babel-runtime": "6.11.6"
}
}
'use strict';
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _tcomb = require('tcomb');
var _tcomb2 = _interopRequireDefault(_tcomb);
var _unionType = require('union-type');
var _unionType2 = _interopRequireDefault(_unionType);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.clear();
var log = console.log.bind(console);
var tType = function tType(obj) {
return (0, _keys2.default)(obj).reduce(function (o, key) {
return o[key] = _tcomb2.default.subtype(obj[key], function (x) {
return obj[key](x) === x;
}, key), o;
}, {});
};
var Actions = tType({ 'Up': _tcomb2.default.String, 'Down': _tcomb2.default.Any, 'Fire': _tcomb2.default.Number });
var result = function result(action) {
return _tcomb2.default.match(action, Actions.Up, function (x) {
return x;
}, Actions.Down, function (y) {
return 'downer';
}, _tcomb2.default.Any, function (x) {
return 'boom';
});
};
log(result('Down'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment