@dillonkearns/elm-graphql
Version:
<img src="https://cdn.jsdelivr.net/gh/martimatix/logo-graphqelm/logo.svg" alt="dillonearns/elm-graphql logo" width="40%" align="right">
87 lines (73 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getOption = getOption;
exports.modify = modify;
exports.choice = choice;
exports.asTraversal = asTraversal;
exports.composeOptional = composeOptional;
var _HKT = require('./HKT');
var _Either = require('./Either');
var either = _interopRequireWildcard(_Either);
var _Maybe = require('./Maybe');
var maybe = _interopRequireWildcard(_Maybe);
var _Identity = require('./Identity');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function toMaybe(e) {
return either.either(maybe.toNothing, maybe.of, e);
}
function getOption(stab, s) {
return toMaybe(stab.getOrModify(s));
}
function modify(optional, f, s) {
return either.either(_Identity.id, function (a) {
return optional.set(f(a), s);
}, optional.getOrModify(s));
}
/** join two POptional with the same target */
function choice(optional1, optional2) {
return {
getOrModify: function getOrModify(s) {
return either.either(function (s1) {
return either.leftMap(either.left, optional1.getOrModify(s1));
}, function (s2) {
return either.leftMap(either.right, optional2.getOrModify(s2));
}, s);
},
set: function set(b, s) {
return either.bimap(function (s1) {
return optional1.set(b, s1);
}, function (s2) {
return optional2.set(b, s2);
}, s);
}
};
}
function asTraversal(optional) {
return {
modifyF: function modifyF(applicative, f, s) {
return either.either(applicative.of, function (a) {
return applicative.map(function (b) {
return optional.set(b, s);
}, f(a));
}, optional.getOrModify(s));
}
};
}
function composeOptional(abcd, stab) {
return {
getOrModify: function getOrModify(s) {
return either.chain(function (a) {
return either.bimap(function (b) {
return stab.set(b, s);
}, _Identity.id, abcd.getOrModify(a));
}, stab.getOrModify(s));
},
set: function set(d, s) {
return modify(stab, function (a) {
return abcd.set(d, a);
}, s);
}
};
}