-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
backslash in prop should work #17
Conversation
614dc2f
to
f17f0bb
Compare
I tried to fix this issue as well, but instead tried using a regex to split the string which results in much cleaner code. var pathArr = path.split(/[^\\\\]\./);
for (var i = 0; i < pathArr.length; i++) {
obj = obj[pathArr[i]];
if (obj === undefined) {
break;
}
} The downside is that it isn't working entirely (yet) as it eats up the last character per token.
I was looking into using something like a lookbehind/lookahead, but I'm not very familiar with the concept. Just wanted to share because maybe someone else has an idea to simplify this. |
I tried to write a customised |
sadly js regexs don't support lookbehind. here is my for-loop-based approach to a split function. Backslash can escape dots and backslashes here. /**
* Split str by dot - supports backslash escaped dots and backslashes
* @param {string} str
* @returns {Array.<string>}
*/
function split(str) {
str = '' + str;
// use native split if possible
if (str.indexOf('\\.') === -1) return str.split('.');
var res = []; // the result array
var pos = 0; // the start position of the first chunk
function chunk(start, end) {
// slice, unescape and push onto result array
res.push(str.slice(start, end).replace(/\\\\/g, '\\').replace(/\\\./g, '.'));
// set position of next chunk.
pos = end + 1;
}
var esc; // boolean indicating if a dot is escaped
var j;
for (var i = 0, l = str.length; i < l; i++) {
if (str[i] === '.') {
esc = false;
// walk over preceding backslashes in reverse direction
for (j = i - 1; str[j] === '\\'; j--) esc = !esc;
// dot is escaped only if preceded by an odd number of backslashes
if (!esc) chunk(pos, i);
}
}
// process the last chunk
chunk(pos, i);
return res;
} |
@hobbyquaker Can you do a PR and we can benchmark it. Thanks. |
will do tomorrow :) |
I just compared my split method, as expected it is much slower... getPathSegments from current master branch:
my implementation:
But I think these results aren't really comparable. Current implementation only escapes dots and can't guarantee predictable results on some edge cases like
My implementation can handle this case with escaped-backslashes:
I would suggest before comparing performance we should clearify the escape-behaviour and implement the necessary tests. Only escaping dots (like statet in #18) isn't enough, imho backslashes themselves have to be escaped too. |
Can you do a PR? The current tests are not complete. |
ping @hobbyquaker :) |
Can we merge this and if anyone can make a faster implementation, do a follow-up PR. |
Fixes #15