UNPKG

agson

Version:

Querying and manipulating JSON graphs

73 lines (63 loc) 1.67 kB
(function() { var Just, Nothing, constant, fromNullable, identity, lens, merge, nothing, property, _ref; merge = require('lodash-node/modern/objects/merge'); _ref = require('data.maybe'), Just = _ref.Just, Nothing = _ref.Nothing, fromNullable = _ref.fromNullable; lens = require('./Lens').of; nothing = lens("nothing", function() { return { modify: Nothing, get: Nothing }; }); identity = lens("identity", function(ma) { return { modify: function(f) { return f(ma); }, get: function() { return ma; } }; }); constant = function(value) { return lens("constant(" + value + ")", function() { return { modify: function() { return fromNullable(value); }, get: function() { return fromNullable(value); } }; }); }; property = function(key) { return lens("property(" + key + ")", function(mo) { return { modify: function(f) { return mo.chain(function(object) { var mv; mv = fromNullable(object[key]); return f(mv).chain(function(value) { var modification; modification = {}; modification[key] = value; return Just(merge({}, object, modification)); }); }); }, get: function() { return mo.chain(function(object) { return fromNullable(object[key]); }); } }; }); }; module.exports = { nothing: nothing, identity: identity, constant: constant, property: property }; }).call(this);