Skip to content

Commit f482e88

Browse files
committed
support for spaces and dots in keys
1 parent 05d671d commit f482e88

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

app/scripts.babel/inc/resultParsers/ObjectsArrayParser.js

+37-5
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,54 @@ class ObjectsArrayParser extends BaseParser {
110110
console.warn('path is empty');
111111
return '';
112112
}
113-
let parts = path.split(/[\.\[\]]/);
113+
let parts = path.split(/(\.|\[["']?|["']?\])/);
114114
if (parts.length === 1) {
115115
if (!(path in record)) {
116116
console.warn(`${path} not in record`);
117117
return '';
118118
}
119119
return record[path];
120120
} else {
121-
var node = record;
121+
let node = record;
122+
const states = {
123+
START: 0,
124+
IN_PARENTHESES: 1,
125+
}
126+
let state = states.START;
127+
let partialKey = '';
128+
//console.log('[getByPath] ', record, path);
122129
for (let i = 0; i < parts.length; i++) {
123-
const key = parts[i];
124-
// skip empty keys
125-
if (key.length < 1) {
130+
let key = parts[i];
131+
/*
132+
console.log({
133+
key: key,
134+
partialKey: partialKey,
135+
state: state,
136+
});
137+
*/
138+
// combine parts depending on state
139+
if (key.search(/^\[/) >= 0) {
140+
//console.log('-> states.IN_PARENTHESES');
141+
state = states.IN_PARENTHESES;
142+
continue;
143+
}
144+
if (state === states.IN_PARENTHESES && key.search(/\]$/) >= 0) {
145+
//console.log('-> states.START');
146+
state = states.START;
147+
key = partialKey;
148+
partialKey = '';
149+
}
150+
if (state === states.IN_PARENTHESES) {
151+
partialKey += key;
152+
//console.log('-> new partial:', partialKey);
153+
continue;
154+
} else if (key === '.' || key === '') {
155+
//console.log('-> dot/empty-skip');
126156
continue;
127157
}
158+
// resolve part
128159
if (key in node) {
160+
//console.log('-> key in node');
129161
node = node[key];
130162
} else {
131163
console.warn(`${path} not in record`);

test/ObjectsArrayParser.js

+23
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ describe('ObjectsArrayParser', function () {
6363
var result = parser.getByPath({meta:{titles:[{main:expected}, {}]}, description:{}}, 'meta.titles[0].main');
6464
assert.strictEqual(result, expected);
6565
});
66+
it('Should support spaces and dots', function () {
67+
var expected = 'some title';
68+
var result = parser.getByPath({meta:{'original title':expected}, description:{}}, 'meta[\'original title\']');
69+
assert.strictEqual(result, expected, 'single quotes');
70+
var result = parser.getByPath({meta:{'original title':expected}, description:{}}, 'meta["original title"]');
71+
assert.strictEqual(result, expected, 'double quotes');
72+
var result = parser.getByPath({meta:{'original.title':expected}, description:{}}, 'meta["original.title"]');
73+
assert.strictEqual(result, expected, 'dot');
74+
});
75+
it('Should support combo-killer path', function () {
76+
var expected = 'some title';
77+
var killerPath = 'meta.titles[0]["original.title"]';
78+
var result = parser.getByPath(
79+
{
80+
meta:{
81+
titles: [
82+
{'original.title':expected}
83+
]
84+
},
85+
description:{},
86+
}, killerPath);
87+
assert.strictEqual(result, expected, `Should work with: ${killerPath}`);
88+
});
6689
});
6790

6891
describe('init', function () {

0 commit comments

Comments
 (0)