UNPKG

agson

Version:

Querying and manipulating JSON graphs

119 lines (94 loc) 2.99 kB
(function() { var AgsonQuery, combinators, lenses, liftThen, run, traversals, __slice = [].slice; lenses = require('./agson/lenses'); traversals = require('./agson/traversals'); combinators = require('./agson/combinators'); liftThen = function(lensf) { return function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; if (this.lens === lenses.identity) { return new AgsonQuery(lensf.apply(null, args)); } else { return new AgsonQuery(this.lens.then(lensf.apply(null, args))); } }; }; run = function(f) { return function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return { run: (function(_this) { return function(data) { return f(_this.lens.run(data)).apply(null, args); }; })(this) }; }; }; AgsonQuery = (function() { function AgsonQuery(lens) { this.lens = lens; } AgsonQuery.prototype.toString = function() { return "agson(" + (this.lens.toString()) + ")"; }; AgsonQuery.prototype.then = function(query) { return new AgsonQuery(this.lens.then(query.lens)); }; AgsonQuery.prototype.selectMany = function(key) { return new AgsonQuery(this.lens.then(lenses.property(key).then(traversals.list))); }; AgsonQuery.prototype.list = liftThen(function() { return traversals.list; }); AgsonQuery.prototype.object = liftThen(function() { return traversals.object; }); AgsonQuery.prototype.property = liftThen(lenses.property); AgsonQuery.prototype.index = liftThen(lenses.property); AgsonQuery.prototype.where = liftThen(function(predicate) { return combinators.where(function(ma) { return ma.map(predicate).getOrElse(false); }); }); AgsonQuery.prototype.recurse = function() { var lens; lens = this.lens.then(traversals.recurse(function() { return lens; })); return new AgsonQuery(lens); }; AgsonQuery.prototype.validateAs = function(type) { return new AgsonQuery(this.lens.then(combinators.fromValidator(type))); }; AgsonQuery.prototype.choose = function(options) { var query, tag, tagsToLenses; tagsToLenses = {}; for (tag in options) { query = options[tag]; tagsToLenses[tag] = query.lens; } return new AgsonQuery(this.lens.then(combinators.sum.tagged(tagsToLenses))); }; AgsonQuery.prototype.get = run(function(s) { return function() { return s.get(); }; }); AgsonQuery.prototype.set = run(function(s) { return function(v) { return s.set(v); }; }); AgsonQuery.prototype.map = run(function(s) { return function(f) { return s.map(f); }; }); return AgsonQuery; })(); module.exports = new AgsonQuery(lenses.identity); }).call(this);