agson
Version:
Querying and manipulating JSON graphs
73 lines (63 loc) • 1.77 kB
JavaScript
(function() {
var Just, Nothing, Traversal, fromNullable, isArray, isObject, list, maybeMap, maybeMapValues, nothing, object, traversal, _ref, _ref1;
_ref = require('data.maybe'), Just = _ref.Just, Nothing = _ref.Nothing, fromNullable = _ref.fromNullable;
_ref1 = require('./util'), maybeMap = _ref1.maybeMap, maybeMapValues = _ref1.maybeMapValues, isArray = _ref1.isArray, isObject = _ref1.isObject;
Traversal = require('./Traversal');
traversal = Traversal.of;
nothing = {
modify: Nothing,
get: Nothing
};
list = traversal("list", function(mta) {
return {
modify: function(f) {
return mta.chain(function(ta) {
if (!isArray(ta)) {
return Nothing();
} else {
return Just(maybeMap(ta, function(a) {
return f(fromNullable(a));
}));
}
});
},
get: function() {
return mta.chain(function(ta) {
if (!isArray(ta)) {
return Nothing();
} else {
return Just(ta);
}
});
}
};
});
object = traversal("object", function(mo) {
return {
modify: function(f) {
return mo.map(function(object) {
if (!isObject(object)) {
return Nothing();
} else {
return maybeMapValues(object, function(value) {
return f(fromNullable(value));
});
}
});
},
get: function() {
return mo.chain(function(object) {
if (!isObject(object)) {
return Nothing();
} else {
return Just(object);
}
});
}
};
});
module.exports = {
list: list,
object: object
};
}).call(this);