Codebase list libjs-objectpath / 9273643
Import upstream version 2.0.0 Debian Janitor 2 years ago
6 changed file(s) with 204 addition(s) and 127 deletion(s). Raw diff Collapse all Expand all
+0
-28
.gitignore less more
0 # Logs
1 logs
2 *.log
3
4 # Runtime data
5 pids
6 *.pid
7 *.seed
8
9 # Directory for instrumented libs generated by jscoverage/JSCover
10 lib-cov
11
12 # Coverage directory used by tools like istanbul
13 coverage
14
15 # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
16 .grunt
17
18 # Compiled binary addons (http://nodejs.org/api/addons.html)
19 build/Release
20
21 # Dependency directory
22 # Commenting this out is preferred by some people, see
23 # https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
24 node_modules
25
26 # Users Environment Variables
27 .lock-wscript
00 language: node_js
11 node_js:
2 - "0.11"
3 - "0.10"
2 - "6"
3 - "8"
4 - "10"
00 'use strict';
11
22 ;!function(undefined) {
3 var regex = {
4 "'": /\\\'/g,
5 '"': /\\\"/g,
6 };
37
48 var ObjectPath = {
59 parse: function(str){
913
1014 var i = 0;
1115 var parts = [];
12 var d, b, q, c;
13 while (i < str.length){
14 d = str.indexOf('.', i);
15 b = str.indexOf('[', i);
16 var dot, bracket, quote, closing;
17 while (i < str.length) {
18 dot = str.indexOf('.', i);
19 bracket = str.indexOf('[', i);
1620
1721 // we've reached the end
18 if (d === -1 && b === -1){
22 if (dot === -1 && bracket === -1) {
1923 parts.push(str.slice(i, str.length));
2024 i = str.length;
2125 }
22
2326 // dots
24 else if (b === -1 || (d !== -1 && d < b)) {
25 parts.push(str.slice(i, d));
26 i = d + 1;
27 else if (bracket === -1 || (dot !== -1 && dot < bracket)) {
28 parts.push(str.slice(i, dot));
29 i = dot + 1;
2730 }
28
2931 // brackets
3032 else {
31 if (b > i){
32 parts.push(str.slice(i, b));
33 i = b;
33 if (bracket > i) {
34 parts.push(str.slice(i, bracket));
35 i = bracket;
3436 }
35 q = str.slice(b+1, b+2);
36 if (q !== '"' && q !=='\'') {
37 c = str.indexOf(']', b);
38 if (c === -1) c = str.length;
39 parts.push(str.slice(i + 1, c));
40 i = (str.slice(c + 1, c + 2) === '.') ? c + 2 : c + 1;
37 quote = str.slice(bracket + 1, bracket + 2);
38
39 if (quote !== '"' && quote !== "'") {
40 closing = str.indexOf(']', bracket);
41 if (closing === -1) {
42 closing = str.length;
43 }
44 parts.push(str.slice(i + 1, closing));
45 i = (str.slice(closing + 1, closing + 2) === '.') ? closing + 2 : closing + 1;
4146 } else {
42 c = str.indexOf(q+']', b);
43 if (c === -1) c = str.length;
44 while (str.slice(c - 1, c) === '\\' && b < str.length){
45 b++;
46 c = str.indexOf(q+']', b);
47 closing = str.indexOf(quote + ']', bracket);
48 if (closing === -1) {
49 closing = str.length;
4750 }
48 parts.push(str.slice(i + 2, c).replace(new RegExp('\\'+q,'g'), q));
49 i = (str.slice(c + 2, c + 3) === '.') ? c + 3 : c + 2;
51 while (str.slice(closing - 1, closing) === '\\' && bracket < str.length) {
52 bracket++;
53 closing = str.indexOf(quote + ']', bracket);
54 }
55 parts.push(str.slice(i + 2, closing).replace(regex[quote], quote).replace(/\\+/g, function (backslash) {
56 return new Array(Math.ceil(backslash.length / 2) + 1).join('\\');
57 }));
58 i = (str.slice(closing + 2, closing + 3) === '.') ? closing + 3 : closing + 2;
5059 }
5160 }
5261 }
5564
5665 // root === true : auto calculate root; must be dot-notation friendly
5766 // root String : the string to use as root
58 stringify: function(arr, quote){
67 stringify: function(arr, quote, forceQuote){
68 if(!Array.isArray(arr))
69 arr = [ arr ];
5970
60 if(!Array.isArray(arr))
61 arr = [arr.toString()];
71 quote = (quote === '"') ? '"' : "'";
72 var regexp = new RegExp('(\\\\|' + quote + ')', 'g'); // regex => /(\\|')/g
6273
63 quote = quote === '"' ? '"' : '\'';
64
65 return arr.map(function(n){ return '[' + quote + (n.toString()).replace(new RegExp(quote, 'g'), '\\' + quote) + quote + ']'; }).join('');
74 return arr.map(function(value, key) {
75 let property = value.toString();
76 if (!forceQuote && /^[A-z_]\w*$/.exec(property)) { // str with only A-z0-9_ chars will display `foo.bar`
77 return (key !== 0) ? '.' + property : property;
78 } else if (!forceQuote && /^\d+$/.exec(property)) { // str with only numbers will display `foo[0]`
79 return '[' + property + ']';
80 } else {
81 property = property.replace(regexp, '\\$1');
82 return '[' + quote + property + quote + ']';
83 }
84 }).join('');
6685 },
6786
68 normalize: function(data, quote){
69 return ObjectPath.stringify(Array.isArray(data) ? data : ObjectPath.parse(data), quote);
87 normalize: function(data, quote, forceQuote){
88 return ObjectPath.stringify(Array.isArray(data) ? data : ObjectPath.parse(data), quote, forceQuote);
7089 },
7190
7291 // Angular
85104 // AMD
86105 if (typeof define === 'function' && define.amd) {
87106 define(function() {
88 return {ObjectPath: ObjectPath};
107 return {
108
109 // this is only namespaced for backwards compatibility when fixing issue #8
110 ObjectPath: ObjectPath,
111
112 // export these as top-level functions
113 parse: ObjectPath.parse,
114 stringify: ObjectPath.stringify,
115 normalize: ObjectPath.normalize
116 };
89117 });
90118 }
91119
99127 window.ObjectPath = ObjectPath;
100128 }
101129
102 }();
130 }();
11 "name": "objectpath",
22 "author": "Mike Marcacci <mike.marcacci@gmail.com>",
33 "license": "MIT",
4 "version": "1.2.1",
4 "version": "2.0.0",
55 "description": "Parse js object paths using both dot and bracket notation. Stringify an array of properties into a valid path.",
66 "main": "index.js",
77 "directories": {
1111 "test": "mocha test/*"
1212 },
1313 "devDependencies": {
14 "chai": "^1.9.1",
15 "mocha": "^1.21.0"
14 "chai": "^4.2.0",
15 "mocha": "^5.2.0"
1616 },
1717 "keywords": [
1818 "js object",
1919 ```js
2020 var ObjectPath = require('objectpath');
2121
22 ObjectPath.parse('a[1].b.c.d["e"]["f"].g');
23 // => ['a','1','b','c','d','e','f','g']
22 ObjectPath.parse('a[1].b.c.d["e"]["f-f"].g');
23 // => ['a','1','b','c','d','e','f-f','g']
2424 ```
2525
2626 Build a Path String
2727 -------------------
2828
29 ObjectPath.stringify(arr, [quote='\'']);
29 ObjectPath.stringify(arr, [quote="'"], [forceQuote=false]);
3030
3131 ```js
3232 var ObjectPath = require('objectpath');
3333
34 ObjectPath.stringify(['a','1','b','c','d','e','f','g']);
35 // => '[\'a\'][\'1\'][\'b\'][\'c\'][\'d\'][\'e\'][\'f\'][\'g\']'
34 ObjectPath.stringify(['a','1','b','c','d-d','e']);
35 // => "a[1].b.c['d-d'].e"
3636
3737
38 ObjectPath.stringify(['a','1','b','c','d','e','f','g'],'"');
39 // => '["a"]["1"]["b"]["c"]["d"]["e"]["f"]["g"]'
38 ObjectPath.stringify(['a','1','b','c','d-d','e'],'"');
39 // => 'a[1].b.c["d-d"].e'
40
41
42 ObjectPath.stringify(['a','1','b','c','d-d','e'],"'", true);
43 // => "['a']['1']['b']['c']['d-d']['e']"
4044 ```
4145
4246 Normalize a Path
4347 ----------------
4448
45 ObjectPath.normalize(str)
49 ObjectPath.normalize(str, [quote="'"], [forceQuote=false])
4650
4751 ```js
4852 var ObjectPath = require('objectpath');
4953
50 ObjectPath.normalize('a[1].b.c.d["e"]["f"].g');
51 // => '["a"]["1"]["b"]["c"]["d"]["e"]["f"]["g"]'
54 ObjectPath.normalize('a[1].b.c.d["e"]["f-f"].g');
55 // => "a[1].b.c.d.e['f-f'].g"
56
57 ObjectPath.normalize('a[1].b.c.d["e"]["f-f"].g', '"');
58 // => 'a[1].b.c.d.e["f-f"].g'
59
60 ObjectPath.normalize('a[1].b.c.d["e"]["f-f"].g', "'", true);
61 // => "['a']['1']['b']['c']['d']['e']['f-f']['g']"
22 var assert = require('chai').assert;
33 var ObjectPath = require('../index.js');
44
5 var parse = ObjectPath.parse;
6 var stringify = ObjectPath.stringify;
7
58 describe('Parse', function(){
69 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');
912 });
1013
1114 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');
1518 });
1619
1720 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');
2023 });
2124
2225 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']);
2427 });
2528
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');
2932 });
3033
3134 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');
3742 });
3843 });
3944
4045 describe('Stringify', function(){
4146 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');
4651 });
52
4753 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');
5056 });
5157
5258 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');
5561 });
5662
5763 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');
6066 });
6167
6268 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']");
6471 });
6572
6673 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"]');
6876 });
6977
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');
7381 });
7482
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');
7886 });
7987
8088 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');
86102 });
87103
88104 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');
94160 });
95161 });
96162
97163 describe('Normalize', function(){
98164 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');
101167 });
102168
103169 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');
106172 });
107173 });