1 | var expect = require('expect.js'),
|
2 | util = require('util'),
|
3 | jpath = require('..')
|
4 | ;
|
5 |
|
6 |
|
7 | var data = {
|
8 | store: {
|
9 | book: [
|
10 | { category: "reference",
|
11 | author: "Nigel Rees",
|
12 | title: "Sayings of the Century",
|
13 | price: 8.95
|
14 | },
|
15 | { category: "fiction",
|
16 | author: "Evelyn Waugh",
|
17 | title: "Sword of Honour",
|
18 | price: 12.99
|
19 | },
|
20 | { category: "fiction",
|
21 | author: "Herman Melville",
|
22 | title: "Moby Dick",
|
23 | isbn: "0-553-21311-3",
|
24 | price: 8.99
|
25 | },
|
26 | { category: "fiction",
|
27 | author: "J. R. R. Tolkien",
|
28 | title: "The Lord of the Rings",
|
29 | isbn: "0-395-19395-8",
|
30 | price: 22.99
|
31 | }
|
32 | ],
|
33 | bicycle: {
|
34 | color: "red",
|
35 | price: 19.95
|
36 | }
|
37 | }
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | var p = jpath.create("#");
|
46 | var res = p.resolve(data);
|
47 | expect(res).to.contain(data);
|
48 |
|
49 |
|
50 |
|
51 | p = jpath.parseSelector("#/store/book[*][#/author]");
|
52 | res = jpath.executeSelectors(data, p);
|
53 | expect(res).to.contain('Evelyn Waugh');
|
54 | expect(res).to.contain('Nigel Rees');
|
55 | expect(res).to.contain('Herman Melville');
|
56 | expect(res).to.contain('J. R. R. Tolkien');
|
57 |
|
58 |
|
59 | p = jpath.parseSelector("[..#/author]");
|
60 | res = jpath.executeSelectors(data, p);
|
61 | expect(res).to.contain('Evelyn Waugh');
|
62 | expect(res).to.contain('Nigel Rees');
|
63 | expect(res).to.contain('Herman Melville');
|
64 | expect(res).to.contain('J. R. R. Tolkien');
|
65 |
|
66 |
|
67 | p = jpath.parseSelector("#/store[*]");
|
68 | res = jpath.executeSelectors(data, p);
|
69 | expect(res).to.contain(data.store.book);
|
70 | expect(res).to.contain(data.store.bicycle);
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | p = jpath.parseSelector("#/store[..#/price]");
|
76 | res = jpath.executeSelectors(data, p);
|
77 | expect(res).to.contain(8.95);
|
78 | expect(res).to.contain(12.99);
|
79 | expect(res).to.contain(8.99);
|
80 | expect(res).to.contain(22.99);
|
81 | expect(res).to.contain(19.95);
|
82 |
|
83 |
|
84 |
|
85 | p = jpath.parseSelector("[..#/book/2]");
|
86 | res = jpath.executeSelectors(data, p);
|
87 | expect(res).to.contain(data.store.book[2]);
|
88 |
|
89 | p = jpath.parseSelector("[..#/book[2]]");
|
90 | res = jpath.executeSelectors(data, p);
|
91 | expect(res).to.contain(data.store.book[2]);
|
92 |
|
93 |
|
94 | p = jpath.parseSelector("[..#/book[last]]");
|
95 | res = jpath.executeSelectors(data, p);
|
96 | expect(res).to.contain(data.store.book[3]);
|
97 |
|
98 |
|
99 | p = jpath.parseSelector("[..#/book[0,1]]");
|
100 | res = jpath.executeSelectors(data, p);
|
101 | expect(res).to.contain(data.store.book[0]);
|
102 | expect(res).to.contain(data.store.book[1]);
|
103 |
|
104 |
|
105 | p = jpath.parseSelector("[..#/book[*][#/isbn]]");
|
106 | res = jpath.executeSelectors(data, p);
|
107 |
|
108 |
|
109 | p = jpath.parseSelector("..#/book[*][take(/author,/title)]");
|
110 | res = jpath.executeSelectors(data, p);
|
111 | expect(res[0]).to.eql({
|
112 | author: "Nigel Rees",
|
113 | title: "Sayings of the Century"
|
114 | });
|
115 | expect(res[1]).to.eql({
|
116 | author: "Evelyn Waugh",
|
117 | title: "Sword of Honour"
|
118 | });
|
119 | expect(res[2]).to.eql({
|
120 | author: "Herman Melville",
|
121 | title: "Moby Dick"
|
122 | });
|
123 | expect(res[3]).to.eql({
|
124 | author: "J. R. R. Tolkien",
|
125 | title: "The Lord of the Rings"
|
126 | });
|
127 |
|
128 |
|
129 | p = jpath.parseSelector("..#/book[*][take(/title,cost=/price)]");
|
130 | res = jpath.executeSelectors(data, p);
|
131 | expect(res[0]).to.eql({
|
132 | title: "Sayings of the Century",
|
133 | cost: 8.95
|
134 | });
|
135 | expect(res[1]).to.eql({
|
136 | title: "Sword of Honour",
|
137 | cost: 12.99
|
138 | });
|
139 | expect(res[2]).to.eql({
|
140 | title: "Moby Dick",
|
141 | cost: 8.99
|
142 | });
|
143 | expect(res[3]).to.eql({
|
144 | title: "The Lord of the Rings",
|
145 | cost: 22.99
|
146 | });
|
147 |
|
148 |
|
149 |
|
150 | p = jpath.parseSelector("[..#/book[*][@myfn]]");
|
151 | res = jpath.executeSelectors(data, p, {
|
152 | myfn: function(obj, accum, sel) {
|
153 | if (obj.price && obj.price < 10)
|
154 | accum.push(obj);
|
155 | return accum;
|
156 | }
|
157 | });
|
158 | expect(res).to.contain(data.store.book[0]);
|
159 | expect(res).to.contain(data.store.book[2]);
|
160 |
|
161 | p = jpath.create("[..#/book[*][@myfn][#/category]]");
|
162 | res = p.resolve(data, {
|
163 | myfn: function(obj, accum, sel) {
|
164 | if (obj.price && obj.price < 10)
|
165 | accum.push(obj);
|
166 | return accum;
|
167 | }
|
168 | });
|
169 | expect(res).to.contain(data.store.book[0].category);
|
170 | expect(res).to.contain(data.store.book[2].category);
|
171 |
|
172 | p = jpath.resolve(data, '/store/book[first(2)]');
|
173 |
|
174 | p = jpath.create("#/store/book[*][@]");
|
175 | var res = p.resolve(data, function(obj, accum, sel) {
|
176 | if (obj.price && obj.price < 10)
|
177 | accum.push(obj);
|
178 | return accum;
|
179 | });
|
180 | expect(res).to.contain(data["store"]["book"][0]);
|
181 | expect(res).to.contain(data["store"]["book"][2]);
|
182 | expect(res).to.have.length(2);
|
183 |
|
184 |
|