@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">
69 lines (53 loc) • 1.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.modify = modify;
exports.choice = choice;
exports.asOptional = asOptional;
exports.composeLens = composeLens;
var _Either = require('./Either');
var either = _interopRequireWildcard(_Either);
var _Getter = require('./Getter');
var getter = _interopRequireWildcard(_Getter);
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; } }
/*
A Lens is an optic used to zoom inside a Product, e.g. case class, Tuple, HList or even Map.
Lenses have two type parameters generally called S and A: Lens[S, A] where S represents the Product and A an element inside of S.
*/
function modify(lens, f, s) {
return lens.set(f(lens.get(s)), s);
}
/** join two PLens with the same target */
function choice(lens1, lens2) {
return {
get: getter.choice(lens1, lens2).get,
set: function set(b, s) {
return either.bimap(function (s1) {
return lens1.set(b, s1);
}, function (s2) {
return lens2.set(b, s2);
}, s);
}
};
}
function asOptional(lens) {
return {
getOrModify: function getOrModify(s) {
return either.right(lens.get(s));
},
set: lens.set
};
}
function composeLens(abcd, stab) {
return {
get: function get(s) {
return abcd.get(stab.get(s));
},
set: function set(d, s) {
return modify(stab, function (a) {
return abcd.set(d, a);
}, s);
}
};
}