Skip to content

Commit

Permalink
Negative look behind Regexp removal. (#23)
Browse files Browse the repository at this point in the history
* Remove negative lookbehind regexp logic.

As reported in mrodrig/json-2-csv#197, many
browsers don't support negative look behind RegExps, which causes both
calls to this module and dependency modules to fail when that logic was
invoked. In order to fix that, this commit reworks the RegExp to be use
a string character parser to find first index where a non-escaped '.'
character is found.

* chore(release): 3.0.1
  • Loading branch information
mrodrig committed Jun 13, 2021
1 parent 706873b commit 0507723
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/path.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,20 @@ function _sp(obj, kp, v) {
* @returns {{dotIndex: Number, key: String, remaining: String}}
*/
function state(kp) {
let match = (/(?<!\\)\./).exec(kp),
dotIndex = match ? match.index : -1;
let dotIndex = findFirstNonEscapedDotIndex(kp);

return {
dotIndex,
key: kp.slice(0, dotIndex >= 0 ? dotIndex : undefined).replace(/\\./g, '.'),
remaining: kp.slice(dotIndex + 1)
};
}

function findFirstNonEscapedDotIndex(kp) {
for (let i = 0; i < kp.length; i++) {
const previousChar = i > 0 ? kp[i - 1] : '',
currentChar = kp[i];
if (currentChar === '.' && previousChar !== '\\') return i;
}
return -1;
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "mrodrig",
"name": "doc-path",
"description": "A document path library for Node",
"version": "3.0.0",
"version": "3.0.1",
"homepage": "https://mrodrig.github.io/doc-path",
"repository": {
"type": "git",
Expand Down

0 comments on commit 0507723

Please sign in to comment.