2 | 2 |
var assert = require('chai').assert;
|
3 | 3 |
var ObjectPath = require('../index.js');
|
4 | 4 |
|
|
5 |
var parse = ObjectPath.parse;
|
|
6 |
var stringify = ObjectPath.stringify;
|
|
7 |
|
5 | 8 |
describe('Parse', function(){
|
6 | 9 |
it('parses simple paths in dot notation', function(){
|
7 | |
assert.deepEqual(ObjectPath.parse('a'), ['a'], 'incorrectly parsed single node');
|
8 | |
assert.deepEqual(ObjectPath.parse('a.b.c'), ['a','b','c'], 'incorrectly parsed multi-node');
|
|
10 |
assert.deepEqual(parse('a'), ['a'], 'incorrectly parsed single node');
|
|
11 |
assert.deepEqual(parse('a.b.c'), ['a','b','c'], 'incorrectly parsed multi-node');
|
9 | 12 |
});
|
10 | 13 |
|
11 | 14 |
it('parses simple paths in bracket notation', function(){
|
12 | |
assert.deepEqual(ObjectPath.parse('["c"]'), ['c'], 'incorrectly parsed single headless node');
|
13 | |
assert.deepEqual(ObjectPath.parse('a["b"]["c"]'), ['a','b','c'], 'incorrectly parsed multi-node');
|
14 | |
assert.deepEqual(ObjectPath.parse('["a"]["b"]["c"]'), ['a','b','c'], 'incorrectly parsed headless multi-node');
|
|
15 |
assert.deepEqual(parse('["c"]'), ['c'], 'incorrectly parsed single headless node');
|
|
16 |
assert.deepEqual(parse('a["b"]["c"]'), ['a','b','c'], 'incorrectly parsed multi-node');
|
|
17 |
assert.deepEqual(parse('["a"]["b"]["c"]'), ['a','b','c'], 'incorrectly parsed headless multi-node');
|
15 | 18 |
});
|
16 | 19 |
|
17 | 20 |
it('parses a numberic nodes in bracket notation', function(){
|
18 | |
assert.deepEqual(ObjectPath.parse('[5]'), ['5'], 'incorrectly parsed single headless numeric node');
|
19 | |
assert.deepEqual(ObjectPath.parse('[5]["a"][3]'), ['5','a','3'], 'incorrectly parsed headless numeric multi-node');
|
|
21 |
assert.deepEqual(parse('[5]'), ['5'], 'incorrectly parsed single headless numeric node');
|
|
22 |
assert.deepEqual(parse('[5]["a"][3]'), ['5','a','3'], 'incorrectly parsed headless numeric multi-node');
|
20 | 23 |
});
|
21 | 24 |
|
22 | 25 |
it('parses a combination of dot and bracket notation', function(){
|
23 | |
assert.deepEqual(ObjectPath.parse('a[1].b.c.d["e"]["f"].g'), ['a','1','b','c','d','e','f','g']);
|
|
26 |
assert.deepEqual(parse('a[1].b.c.d["e"]["f"].g'), ['a','1','b','c','d','e','f','g']);
|
24 | 27 |
});
|
25 | 28 |
|
26 | |
it('parses utf8 characters', function(){
|
27 | |
assert.deepEqual(ObjectPath.parse('∑´ƒ©∫∆.ø'), ['∑´ƒ©∫∆','ø'], 'incorrectly parsed utf8 characters from dot notation');
|
28 | |
assert.deepEqual(ObjectPath.parse('["∑´ƒ©∫∆"]["ø"]'), ['∑´ƒ©∫∆','ø'], 'incorrectly parsed utf8 characters from bracket notation');
|
|
29 |
it('parses unicode characters', function(){
|
|
30 |
assert.deepEqual(parse('∑´ƒ©∫∆.ø'), ['∑´ƒ©∫∆','ø'], 'incorrectly parsed unicode characters from dot notation');
|
|
31 |
assert.deepEqual(parse('["∑´ƒ©∫∆"]["ø"]'), ['∑´ƒ©∫∆','ø'], 'incorrectly parsed unicode characters from bracket notation');
|
29 | 32 |
});
|
30 | 33 |
|
31 | 34 |
it('parses nodes with control characters', function(){
|
32 | |
assert.deepEqual(ObjectPath.parse('["a.b."]'), ['a.b.'], 'incorrectly parsed dots from inside brackets');
|
33 | |
assert.deepEqual(ObjectPath.parse('["\""][\'\\\'\']'), ['"','\\\''], 'incorrectly parsed escaped quotes');
|
34 | |
assert.deepEqual(ObjectPath.parse('["\'"][\'"\']'), ['\'','"'], 'incorrectly parsed unescaped quotes');
|
35 | |
assert.deepEqual(ObjectPath.parse('["\\""][\'\\\'\']'), ['\\"','\\\''], 'incorrectly parsed escape character');
|
36 | |
assert.deepEqual(ObjectPath.parse('["[\'a\']"][\'[\\"a\\"]\']'), ['[\'a\']','[\\"a\\"]'], 'incorrectly parsed escape character');
|
|
35 |
assert.deepEqual(parse('["a.b."]'), ['a.b.'], 'incorrectly parsed dots from inside brackets');
|
|
36 |
assert.deepEqual(parse('["\""][\'\\\'\']'), ['"','\''], 'incorrectly parsed escaped quotes');
|
|
37 |
assert.deepEqual(parse('["\'"][\'"\']'), ['\'','"'], 'incorrectly parsed unescaped quotes');
|
|
38 |
assert.deepEqual(parse('["\\""][\'\\\'\']'), ['"','\''], 'incorrectly parsed escaped quotes');
|
|
39 |
assert.deepEqual(parse('[\'\\"\']["\\\'"]'), ['\\"','\\\''], 'incorrectly parsed escape characters');
|
|
40 |
assert.deepEqual(parse('["\\"]"]["\\"]\\"]"]'), ['"]','"]"]'], 'incorrectly parsed escape characters');
|
|
41 |
assert.deepEqual(parse('["[\'a\']"][\'[\\"a\\"]\']'), ['[\'a\']','[\\"a\\"]'], 'incorrectly parsed escape character');
|
37 | 42 |
});
|
38 | 43 |
});
|
39 | 44 |
|
40 | 45 |
describe('Stringify', function(){
|
41 | 46 |
it('stringifys simple paths with single quotes', function(){
|
42 | |
assert.deepEqual(ObjectPath.stringify(['a']), '[\'a\']', 'incorrectly stringified single node');
|
43 | |
assert.deepEqual(ObjectPath.stringify(['a','b','c']), '[\'a\'][\'b\'][\'c\']', 'incorrectly stringified multi-node');
|
44 | |
assert.deepEqual(ObjectPath.stringify(['a'], '\''), '[\'a\']', 'incorrectly stringified single node with excplicit single quote');
|
45 | |
assert.deepEqual(ObjectPath.stringify(['a','b','c'], '\''), '[\'a\'][\'b\'][\'c\']', 'incorrectly stringified multi-node with excplicit single quote');
|
|
47 |
assert.deepEqual(stringify(['a']), 'a', 'incorrectly stringified single node');
|
|
48 |
assert.deepEqual(stringify(['a','b','c']), 'a.b.c', 'incorrectly stringified multi-node');
|
|
49 |
assert.deepEqual(stringify(['a'], '\''), 'a', 'incorrectly stringified single node with excplicit single quote');
|
|
50 |
assert.deepEqual(stringify(['a','b','c'], '\''), 'a.b.c', 'incorrectly stringified multi-node with excplicit single quote');
|
46 | 51 |
});
|
|
52 |
|
47 | 53 |
it('stringifys simple paths with double quotes', function(){
|
48 | |
assert.deepEqual(ObjectPath.stringify(['a'],'"'), '["a"]', 'incorrectly stringified single node');
|
49 | |
assert.deepEqual(ObjectPath.stringify(['a','b','c'],'"'), '["a"]["b"]["c"]', 'incorrectly stringified multi-node');
|
|
54 |
assert.deepEqual(stringify(['a'],'"'), 'a', 'incorrectly stringified single node');
|
|
55 |
assert.deepEqual(stringify(['a','b','c'],'"'), 'a.b.c', 'incorrectly stringified multi-node');
|
50 | 56 |
});
|
51 | 57 |
|
52 | 58 |
it('stringifys a numberic nodes in bracket notation with single quotes', function(){
|
53 | |
assert.deepEqual(ObjectPath.stringify(['5']), '[\'5\']', 'incorrectly stringified single headless numeric node');
|
54 | |
assert.deepEqual(ObjectPath.stringify(['5','a','3']), '[\'5\'][\'a\'][\'3\']', 'incorrectly stringified headless numeric multi-node');
|
|
59 |
assert.deepEqual(stringify(['5']), '[5]', 'incorrectly stringified single headless numeric node');
|
|
60 |
assert.deepEqual(stringify(['5','a','3']), '[5].a[3]', 'incorrectly stringified headless numeric multi-node');
|
55 | 61 |
});
|
56 | 62 |
|
57 | 63 |
it('stringifys a numberic nodes in bracket notation with double quotes', function(){
|
58 | |
assert.deepEqual(ObjectPath.stringify(['5'],'"'), '["5"]', 'incorrectly stringified single headless numeric node');
|
59 | |
assert.deepEqual(ObjectPath.stringify(['5','a','3'],'"'), '["5"]["a"]["3"]', 'incorrectly stringified headless numeric multi-node');
|
|
64 |
assert.deepEqual(stringify(['5'],'"'), '[5]', 'incorrectly stringified single headless numeric node');
|
|
65 |
assert.deepEqual(stringify(['5','a','3'],'"'), '[5].a[3]', 'incorrectly stringified headless numeric multi-node');
|
60 | 66 |
});
|
61 | 67 |
|
62 | 68 |
it('stringifys a combination of dot and bracket notation with single quotes', function(){
|
63 | |
assert.deepEqual(ObjectPath.stringify(['a','1','b','c','d','e','f','g']), '[\'a\'][\'1\'][\'b\'][\'c\'][\'d\'][\'e\'][\'f\'][\'g\']');
|
|
69 |
assert.deepEqual(stringify(['a','1','b','c','d','e','f','g']), 'a[1].b.c.d.e.f.g');
|
|
70 |
assert.deepEqual(stringify(['a','1','b','c','d','e','f','g'],null,true), "['a']['1']['b']['c']['d']['e']['f']['g']");
|
64 | 71 |
});
|
65 | 72 |
|
66 | 73 |
it('stringifys a combination of dot and bracket notation with double quotes', function(){
|
67 | |
assert.deepEqual(ObjectPath.stringify(['a','1','b','c','d','e','f','g'],'"'), '["a"]["1"]["b"]["c"]["d"]["e"]["f"]["g"]');
|
|
74 |
assert.deepEqual(stringify(['a','1','b','c','d','e','f','g'],'"'), 'a[1].b.c.d.e.f.g');
|
|
75 |
assert.deepEqual(stringify(['a','1','b','c','d','e','f','g'],'"',true), '["a"]["1"]["b"]["c"]["d"]["e"]["f"]["g"]');
|
68 | 76 |
});
|
69 | 77 |
|
70 | |
it('stringifys utf8 characters with single quotes', function(){
|
71 | |
assert.deepEqual(ObjectPath.stringify(['∑´ƒ©∫∆']), '[\'∑´ƒ©∫∆\']', 'incorrectly stringified single node path with utf8');
|
72 | |
assert.deepEqual(ObjectPath.stringify(['∑´ƒ©∫∆','ø']), '[\'∑´ƒ©∫∆\'][\'ø\']', 'incorrectly stringified multi-node path with utf8 characters');
|
|
78 |
it('stringifys unicode characters with single quotes', function(){
|
|
79 |
assert.deepEqual(stringify(['∑´ƒ©∫∆']), '[\'∑´ƒ©∫∆\']', 'incorrectly stringified single node path with unicode');
|
|
80 |
assert.deepEqual(stringify(['∑´ƒ©∫∆','ø']), '[\'∑´ƒ©∫∆\'][\'ø\']', 'incorrectly stringified multi-node path with unicode characters');
|
73 | 81 |
});
|
74 | 82 |
|
75 | |
it('stringifys utf8 characters with double quotes', function(){
|
76 | |
assert.deepEqual(ObjectPath.stringify(['∑´ƒ©∫∆'],'"'), '["∑´ƒ©∫∆"]', 'incorrectly stringified single node path with utf8');
|
77 | |
assert.deepEqual(ObjectPath.stringify(['∑´ƒ©∫∆','ø'],'"'), '["∑´ƒ©∫∆"]["ø"]', 'incorrectly stringified multi-node path with utf8 characters');
|
|
83 |
it('stringifys unicode characters with double quotes', function(){
|
|
84 |
assert.deepEqual(stringify(['∑´ƒ©∫∆'],'"'), '["∑´ƒ©∫∆"]', 'incorrectly stringified single node path with unicode');
|
|
85 |
assert.deepEqual(stringify(['∑´ƒ©∫∆','ø'],'"'), '["∑´ƒ©∫∆"]["ø"]', 'incorrectly stringified multi-node path with unicode characters');
|
78 | 86 |
});
|
79 | 87 |
|
80 | 88 |
it("stringifys nodes with control characters and single quotes", function(){
|
81 | |
assert.deepEqual(ObjectPath.stringify(["a.b."],"'"), "['a.b.']", "incorrectly stringified dots from inside brackets");
|
82 | |
assert.deepEqual(ObjectPath.stringify(["'","\\\""],"'"), "['\\\'']['\\\"']", "incorrectly stringified escaped quotes");
|
83 | |
assert.deepEqual(ObjectPath.stringify(["\"","'"],"'"), "['\"']['\\'']", "incorrectly stringified unescaped quotes");
|
84 | |
assert.deepEqual(ObjectPath.stringify(["\\'","\\\""],"'"), "['\\\\'']['\\\"']", "incorrectly stringified escape character");
|
85 | |
assert.deepEqual(ObjectPath.stringify(["[\"a\"]","[\\'a\\']"],"'"), "['[\"a\"]']['[\\\\'a\\\\']']", "incorrectly stringified escape character");
|
|
89 |
assert.deepEqual(stringify(["a.b."],"'"), "['a.b.']", "incorrectly stringified dots from inside brackets");
|
|
90 |
assert.deepEqual(stringify(["'","\\\""],"'"), "['\\\'']['\\\\\"']", "incorrectly stringified escaped quotes");
|
|
91 |
assert.deepEqual(stringify(["\"","'"],"'"), "['\"']['\\'']", "incorrectly stringified unescaped quotes");
|
|
92 |
assert.deepEqual(stringify(["\\'","\\\""],"'"), "['\\\\\\'']['\\\\\"']", "incorrectly stringified escape character");
|
|
93 |
assert.deepEqual(stringify(["[\"a\"]","[\\'a\\']"],"'"), "['[\"a\"]']['[\\\\\\'a\\\\\\']']", "incorrectly stringified escape character");
|
|
94 |
});
|
|
95 |
|
|
96 |
it("stringifys nodes with backslash", function(){
|
|
97 |
const originalProperty = ' \\" \\\\" \\\\\\';
|
|
98 |
const path = stringify([' \\" \\\\" \\\\\\'], '"');
|
|
99 |
const finalProperty = JSON.parse(path.substring(1, path.length - 1));
|
|
100 |
|
|
101 |
assert.deepEqual(finalProperty, originalProperty, 'incorrectly stringified escaped backslash');
|
86 | 102 |
});
|
87 | 103 |
|
88 | 104 |
it('stringifys nodes with control characters and double quotes', function(){
|
89 | |
assert.deepEqual(ObjectPath.stringify(['a.b.'],'"'), '["a.b."]', 'incorrectly stringified dots from inside brackets');
|
90 | |
assert.deepEqual(ObjectPath.stringify(['"','\\\''],'"'), '["\\\""]["\\\'"]', 'incorrectly stringified escaped quotes');
|
91 | |
assert.deepEqual(ObjectPath.stringify(['\'','"'],'"'), '["\'"]["\\""]', 'incorrectly stringified unescaped quotes');
|
92 | |
assert.deepEqual(ObjectPath.stringify(['\\"','\\\''],'"'), '["\\\\""]["\\\'"]', 'incorrectly stringified escape character');
|
93 | |
assert.deepEqual(ObjectPath.stringify(['[\'a\']','[\\"a\\"]'],'"'), '["[\'a\']"]["[\\\\"a\\\\"]"]', 'incorrectly stringified escape character');
|
|
105 |
assert.deepEqual(stringify(['a.b.'],'"'), '["a.b."]', 'incorrectly stringified dots from inside brackets');
|
|
106 |
assert.deepEqual(stringify(['"','\\\''],'"'), '["\\\""]["\\\\\'"]', 'incorrectly stringified escaped quotes');
|
|
107 |
assert.deepEqual(stringify(['\'','"'],'"'), '["\'"]["\\""]', 'incorrectly stringified unescaped quotes');
|
|
108 |
assert.deepEqual(stringify(['\\"','\\\''],'"'), '["\\\\\\""]["\\\\\'"]', 'incorrectly stringified escape character');
|
|
109 |
assert.deepEqual(stringify(['[\'a\']','[\\"a\\"]'],'"'), '["[\'a\']"]["[\\\\\\"a\\\\\\"]"]', 'incorrectly stringified escape character');
|
|
110 |
});
|
|
111 |
});
|
|
112 |
|
|
113 |
describe('Backslash support tests', function(){
|
|
114 |
var property, expected;
|
|
115 |
function noBuckets(path) {
|
|
116 |
return path.replace(/^\[(.*)\]$/, '$1');
|
|
117 |
}
|
|
118 |
function digest(property) {
|
|
119 |
return stringify(parse(property), '"');
|
|
120 |
}
|
|
121 |
|
|
122 |
it('should escape a quote', function(){
|
|
123 |
assert.deepEqual(digest('a"'), String.raw`["a\""]`, 'incorrectly escape quote');
|
|
124 |
});
|
|
125 |
|
|
126 |
it('should escape backslash', function(){
|
|
127 |
assert.deepEqual(digest('lol\\\\eded.ededed.deede'), String.raw`["lol\\\\eded"].ededed.deede`, 'incorrectly escape quote');
|
|
128 |
});
|
|
129 |
|
|
130 |
it('should escape one backslash', function(){
|
|
131 |
property = String.raw`a\"`;
|
|
132 |
expected = String.raw`["a\\\""]`; // equivalent: expected = '["a\\\\\\""]'
|
|
133 |
assert.deepEqual(digest(property), expected, 'incorrectly escape single backslash');
|
|
134 |
assert.deepEqual(JSON.stringify(property), noBuckets(expected), 'incorrectly escape single backslash');
|
|
135 |
});
|
|
136 |
|
|
137 |
it('should escape several backslash', function(){
|
|
138 |
property = String.raw`a\\"`;
|
|
139 |
expected = String.raw`["a\\\\\""]`; // equivalent: expected = '["a\\\\\\""]'
|
|
140 |
assert.deepEqual(digest(property), expected, 'incorrectly escape several backslash');
|
|
141 |
assert.deepEqual(JSON.stringify(property), noBuckets(expected), 'incorrectly escape several backslash');
|
|
142 |
assert.deepEqual(digest(digest(digest(digest(property)))), expected, 'incorrectly escape after several stringify&parse on the same value');
|
|
143 |
});
|
|
144 |
|
|
145 |
it('should be a valid js path (accepted by ECMAScript)', function(){
|
|
146 |
property = String.raw`a\\"`;
|
|
147 |
expected = String.raw`["a\\\\\""]`; // equivalent: expected = '["a\\\\\\""]'
|
|
148 |
assert.deepEqual(digest(property), expected, 'invalid path syntax');
|
|
149 |
assert.doesNotThrow(function () {
|
|
150 |
try {
|
|
151 |
eval(digest(property)); // only with trusted string
|
|
152 |
return true;
|
|
153 |
} catch (e) {
|
|
154 |
if (e.message.indexOf("Cannot read property") !== -1) // mean undefined
|
|
155 |
return true;
|
|
156 |
else // Syntax error
|
|
157 |
throw new Error("wrong with ES");
|
|
158 |
}
|
|
159 |
}, 'invalid path syntax');
|
94 | 160 |
});
|
95 | 161 |
});
|
96 | 162 |
|
97 | 163 |
describe('Normalize', function(){
|
98 | 164 |
it('normalizes a string', function(){
|
99 | |
assert.deepEqual(ObjectPath.normalize('a.b["c"]'), '[\'a\'][\'b\'][\'c\']', 'incorrectly normalized a string with single quotes');
|
100 | |
assert.deepEqual(ObjectPath.normalize('a.b["c"]','"'), '["a"]["b"]["c"]', 'incorrectly normalized a string with double quotes');
|
|
165 |
assert.deepEqual(ObjectPath.normalize('a.b["c"]'), 'a.b.c', 'incorrectly normalized a string with single quotes');
|
|
166 |
assert.deepEqual(ObjectPath.normalize('a.b["c"]','"'), 'a.b.c', 'incorrectly normalized a string with double quotes');
|
101 | 167 |
});
|
102 | 168 |
|
103 | 169 |
it('normalizes an array', function(){
|
104 | |
assert.deepEqual(ObjectPath.normalize(['a','b','c']), '[\'a\'][\'b\'][\'c\']', 'incorrectly normalized an array with single quotes');
|
105 | |
assert.deepEqual(ObjectPath.normalize(['a','b','c'],'"'), '["a"]["b"]["c"]', 'incorrectly normalized an array with double quotes');
|
|
170 |
assert.deepEqual(ObjectPath.normalize(['a','b','c']), 'a.b.c', 'incorrectly normalized an array with single quotes');
|
|
171 |
assert.deepEqual(ObjectPath.normalize(['a','b','c'],'"'), 'a.b.c', 'incorrectly normalized an array with double quotes');
|
106 | 172 |
});
|
107 | 173 |
});
|