-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathsugar.js
More file actions
120 lines (103 loc) · 3.07 KB
/
sugar.js
File metadata and controls
120 lines (103 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var assert = require('assert');
var jp = require('../');
var util = require('util');
suite('sugar', function() {
test('parent gets us parent value', function() {
var data = { a: 1, b: 2, c: 3, z: { a: 100, b: 200 } };
var parent = jp.parent(data, '$.z.b');
assert.equal(parent, data.z);
});
test('apply method sets values', function() {
var data = { a: 1, b: 2, c: 3, z: { a: 100, b: 200 } };
jp.apply(data, '$..a', function(v) { return v + 1 });
assert.equal(data.a, 2);
assert.equal(data.z.a, 101);
});
test('filter method deletes object from Array specified by remove function', function() {
var data = {
a: [
{
id: 'book',
price: 100
},
{
id: 'car',
price: 3456
}
],
b: 2
};
jp.filter(data, '$..a[0]', function(v) {
return true;
});
assert.equal(data.a[0].id, 'car');
});
test('filter method deletes object from Array specified by special remove object', function() {
var data = {
a: [
{
id: 'book',
price: 100
},
{
id: 'car',
price: 3456
}
],
b: {
c: 2,
d: 1
}
};
jp.filter(data, '$..b.c', function(v) {
return true;
});
assert.equal(data.b.c, undefined);
assert.equal(data.b.d, 1);
});
test('apply method applies survives structural changes', function() {
var data = {a: {b: [1, {c: [2,3]}]}};
jp.apply(data, '$..*[?(@.length > 1)]', function(array) {
return array.reverse();
});
assert.deepEqual(data.a.b, [{c: [3, 2]}, 1]);
});
test('value method gets us a value', function() {
var data = { a: 1, b: 2, c: 3, z: { a: 100, b: 200 } };
var b = jp.value(data, '$..b')
assert.equal(b, data.b);
});
test('value method sets us a value', function() {
var data = { a: 1, b: 2, c: 3, z: { a: 100, b: 200 } };
var b = jp.value(data, '$..b', '5000')
assert.equal(b, 5000);
assert.equal(data.b, 5000);
});
test('value method sets new key and value', function() {
var data = {};
var a = jp.value(data, '$.a', 1);
var c = jp.value(data, '$.b.c', 2);
assert.equal(a, 1);
assert.equal(data.a, 1);
assert.equal(c, 2);
assert.equal(data.b.c, 2);
});
test('value method sets new array value', function() {
var data = {};
var v1 = jp.value(data, '$.a.d[0]', 4);
var v2 = jp.value(data, '$.a.d[1]', 5);
assert.equal(v1, 4);
assert.equal(v2, 5);
assert.deepEqual(data.a.d, [4, 5]);
});
test('value method sets non-literal key', function() {
var data = { "list": [ { "index": 0, "value": "default" }, { "index": 1, "value": "default" } ] };
jp.value(data, '$.list[?(@.index == 1)].value', "test");
assert.equal(data.list[1].value, "test");
});
test('paths with a count gets us back count many paths', function() {
data = [ { a: [ 1, 2, 3 ], b: [ -1, -2, -3 ] }, { } ]
paths = jp.paths(data, '$..*', 3)
assert.deepEqual(paths, [ ['$', '0'], ['$', '1'], ['$', '0', 'a'] ]);
});
});