mezzanine
Version:
Fantasy land union types with pattern matching
59 lines (51 loc) • 1.41 kB
JavaScript
import { equals, merge } from 'ramda';
import { append, cons } from '../utils/list';
import './type-container';
export var fantasyMethods = {
equals: (ctx, Record) => val => {
if (!Record.is(val)) return false;
var asTyped = Record.of(val);
var json = ctx.toJSON();
return equals(json, asTyped.toJSON());
},
concat: (ctx, Record) => val => {
var struct = Record.
of(val).
toJSON();
return Record(merge(ctx.toJSON(), struct));
},
extract: ctx => function extract() {return ctx.toJSON();},
ap: ctx =>
function ap(m) {return m.chain(f => ctx.map(f));},
extend: (ctx, Record) =>
function extend(fn) {return Record(fn(ctx));},
chain: ctx =>
fn =>
fn(ctx.toJSON()),
map: (ctx, Record) =>
fn =>
Record(fn(ctx)) };
export var fantasyStatic = {
of: Record => val =>
typeof val === 'object' &&
val != null &&
Record.ಠ_ಠ === val.ಠ_ಠ ?
val :
Record(val),
equals: () => (a, b) => a.equals(b),
contramap: Record => fn => {
//$FlowIssue
var newStack = append(fn, Record.stack);
//$FlowIssue
var NewRecord = Record.stackUpdate(newStack);
return NewRecord;
},
map: Record => fn => {
//$FlowIssue
var newStack = cons(fn, Record.stack);
//$FlowIssue
var NewRecord = Record.stackUpdate(newStack);
return NewRecord;
} };
export default fantasyMethods;
//# sourceMappingURL=fantasy-land.js.map