@fpjs/overture
Version:
A Javascript prelude
76 lines (75 loc) • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toMaybe = exports.maybeToArray = exports.fromMaybe = exports.Nothing = exports.Maybe = exports.Just = void 0;
var _base = require("@fpjs/overture/base");
var _functor = require("@fpjs/overture/algebras/functor");
var _setoid = require("@fpjs/overture/algebras/setoid");
var _ord = require("@fpjs/overture/algebras/ord");
const Maybe = exports.Maybe = {
'@@type': 'Maybe'
};
const self = function () {
return this;
};
const Nothing = exports.Nothing = {
'@@type': 'Maybe',
'@@name': 'Nothing',
'constructor': Maybe,
'fantasy-land/map': self,
'fantasy-land/ap': self,
'fantasy-land/chain': self,
'fantasy-land/reduce': (_, z) => z,
'fantasy-land/equals': y => y.caseOf({
'Nothing': (0, _base.constant)(true),
'Just': (0, _base.constant)(false)
}),
'fantasy-land/lte': y => y.caseOf({
'Nothing': (0, _base.constant)(true),
'Just': (0, _ord.lte)(x)
}),
'caseOf': cases => cases['Nothing']()
};
const Just = x => ({
'@@type': 'Maybe',
'@@name': 'Just',
'constructor': Maybe,
'fantasy-land/map': f => Just(f(x)),
'fantasy-land/ap': (0, _functor.map)(f => f(x)),
'fantasy-land/chain': f => f(x),
'fantasy-land/reduce': (f, z) => f(z, x),
'fantasy-land/equals': y => y.caseOf({
'Nothing': (0, _base.constant)(true),
'Just': (0, _setoid.equals)(x)
}),
'fantasy-land/lte': y => y.caseOf({
'Nothing': (0, _base.constant)(true),
'Just': (0, _ord.lte)(x)
}),
'caseOf': cases => cases['Just'](x)
});
exports.Just = Just;
Maybe['fantasy-land/of'] = Just;
const fromMaybe = def => x => {
if ((0, _base.type)(x) !== 'Maybe') {
throw TypeError(`${(0, _base.type)(x)} is not Maybe.`);
}
return x.caseOf({
'Just': _base.id,
'Nothing': (0, _base.constant)(def)
});
};
exports.fromMaybe = fromMaybe;
const toMaybe = x => x != null ? Just(x) : Nothing;
exports.toMaybe = toMaybe;
const maybeToArray = x => {
if ((0, _base.type)(x) !== 'Maybe') {
throw TypeError(`${(0, _base.type)(x)} is not Maybe.`);
}
return x.caseOf({
'Just': x => [x],
'Nothing': (0, _base.constant)([])
});
};
exports.maybeToArray = maybeToArray;