UNPKG

2.53 kBJavaScriptView Raw
1(function() {
2 var AgsonQuery, combinators, lenses, liftThen, run, traversals,
3 __slice = [].slice;
4
5 lenses = require('./agson/lenses');
6
7 traversals = require('./agson/traversals');
8
9 combinators = require('./agson/combinators');
10
11 liftThen = function(lensf) {
12 return function() {
13 var args;
14 args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
15 if (this.lens === lenses.identity) {
16 return new AgsonQuery(lensf.apply(null, args));
17 } else {
18 return new AgsonQuery(this.lens.then(lensf.apply(null, args)));
19 }
20 };
21 };
22
23 run = function(f) {
24 return function() {
25 var args;
26 args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
27 return {
28 run: (function(_this) {
29 return function(data) {
30 return f(_this.lens.run(data)).apply(null, args);
31 };
32 })(this)
33 };
34 };
35 };
36
37 AgsonQuery = (function() {
38 function AgsonQuery(lens) {
39 this.lens = lens;
40 }
41
42 AgsonQuery.prototype.toString = function() {
43 return "agson(" + (this.lens.toString()) + ")";
44 };
45
46 AgsonQuery.prototype.then = function(query) {
47 return new AgsonQuery(this.lens.then(query.lens));
48 };
49
50 AgsonQuery.prototype.selectMany = function(key) {
51 return new AgsonQuery(this.lens.then(lenses.property(key).then(traversals.list)));
52 };
53
54 AgsonQuery.prototype.list = liftThen(function() {
55 return traversals.list;
56 });
57
58 AgsonQuery.prototype.object = liftThen(function() {
59 return traversals.object;
60 });
61
62 AgsonQuery.prototype.property = liftThen(lenses.property);
63
64 AgsonQuery.prototype.index = liftThen(lenses.property);
65
66 AgsonQuery.prototype.where = liftThen(function(predicate) {
67 return combinators.where(function(ma) {
68 return ma.map(predicate).getOrElse(false);
69 });
70 });
71
72 AgsonQuery.prototype.recurse = function() {
73 var lens;
74 lens = this.lens.then(traversals.recurse(function() {
75 return lens;
76 }));
77 return new AgsonQuery(lens);
78 };
79
80 AgsonQuery.prototype.get = run(function(s) {
81 return function() {
82 return s.get();
83 };
84 });
85
86 AgsonQuery.prototype.set = run(function(s) {
87 return function(v) {
88 return s.set(v);
89 };
90 });
91
92 AgsonQuery.prototype.map = run(function(s) {
93 return function(f) {
94 return s.map(f);
95 };
96 });
97
98 return AgsonQuery;
99
100 })();
101
102 module.exports = new AgsonQuery(lenses.identity);
103
104}).call(this);