agson
Version:
Querying and manipulating JSON graphs
85 lines (75 loc) • 1.91 kB
JavaScript
(function() {
var Just, Nothing, constant, fromNullable, identity, lens, nothing, property, withProperty, _ref,
__hasProp = {}.hasOwnProperty;
_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) {
var setProperty;
setProperty = withProperty(key);
return {
modify: function(f) {
return mo.chain(function(object) {
var mv;
mv = fromNullable(object[key]);
return f(mv).chain(function(value) {
return Just(setProperty(object, value));
});
});
},
get: function() {
return mo.chain(function(object) {
return fromNullable(object[key]);
});
}
};
});
};
withProperty = function(key) {
return function(object, value) {
var k, result, v;
result = {};
for (k in object) {
if (!__hasProp.call(object, k)) continue;
v = object[k];
result[k] = v;
}
result[key] = value;
return result;
};
};
module.exports = {
nothing: nothing,
identity: identity,
constant: constant,
property: property
};
}).call(this);