From 05077237e55e702a97c165fe4e19979999cd555f Mon Sep 17 00:00:00 2001 From: Michael Rodrigues Date: Sun, 13 Jun 2021 13:15:58 -0400 Subject: [PATCH] Negative look behind Regexp removal. (#23) * Remove negative lookbehind regexp logic. As reported in https://github.com/mrodrig/json-2-csv/issues/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 --- dist/path.js | 2 +- lib/path.js | 12 ++++++++++-- package-lock.json | 8 ++++---- package.json | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/dist/path.js b/dist/path.js index 8fa1902..66f2fac 100644 --- a/dist/path.js +++ b/dist/path.js @@ -3,4 +3,4 @@ * doc-path * Copyright (c) 2015-present, Michael Rodrigues. */ -"use strict";function evaluatePath(t,e){if(!t)return null;let{dotIndex:r,key:a,remaining:i}=state(e);return r>=0&&!t[e]?Array.isArray(t[a])?t[a].map(t=>evaluatePath(t,i)):evaluatePath(t[a],i):Array.isArray(t)?t.map(t=>evaluatePath(t,e)):r>=0&&e!==a&&t[a]?evaluatePath(t[a],i):-1===r&&t[a]&&!t[e]?t[a]:t[e]}function setPath(t,e,r){if(!t)throw new Error("No object was provided.");if(!e)throw new Error("No keyPath was provided.");return _sp(t,e,r)}function _sp(t,e,r){let{dotIndex:a,key:i,remaining:n}=state(e);if(e.startsWith("__proto__")||e.startsWith("constructor")||e.startsWith("prototype"))return t;if(a>=0){if(!t[i]&&Array.isArray(t))return t.forEach(t=>_sp(t,e,r));t[i]||(t[i]={}),_sp(t[i],n,r)}else{if(Array.isArray(t))return t.forEach(t=>_sp(t,n,r));t[i]=r}return t}function state(t){let e=/(?=0?r:void 0).replace(/\\./g,"."),remaining:t.slice(r+1)}}module.exports={evaluatePath:evaluatePath,setPath:setPath}; \ No newline at end of file +"use strict";function evaluatePath(t,e){if(!t)return null;let{dotIndex:r,key:a,remaining:n}=state(e);return r>=0&&!t[e]?Array.isArray(t[a])?t[a].map(t=>evaluatePath(t,n)):evaluatePath(t[a],n):Array.isArray(t)?t.map(t=>evaluatePath(t,e)):r>=0&&e!==a&&t[a]?evaluatePath(t[a],n):-1===r&&t[a]&&!t[e]?t[a]:t[e]}function setPath(t,e,r){if(!t)throw new Error("No object was provided.");if(!e)throw new Error("No keyPath was provided.");return _sp(t,e,r)}function _sp(t,e,r){let{dotIndex:a,key:n,remaining:i}=state(e);if(e.startsWith("__proto__")||e.startsWith("constructor")||e.startsWith("prototype"))return t;if(a>=0){if(!t[n]&&Array.isArray(t))return t.forEach(t=>_sp(t,e,r));t[n]||(t[n]={}),_sp(t[n],i,r)}else{if(Array.isArray(t))return t.forEach(t=>_sp(t,i,r));t[n]=r}return t}function state(t){let e=findFirstNonEscapedDotIndex(t);return{dotIndex:e,key:t.slice(0,e>=0?e:void 0).replace(/\\./g,"."),remaining:t.slice(e+1)}}function findFirstNonEscapedDotIndex(t){for(let e=0;e0?t[e-1]:"";if("."===t[e]&&"\\"!==r)return e}return-1}module.exports={evaluatePath:evaluatePath,setPath:setPath}; \ No newline at end of file diff --git a/lib/path.js b/lib/path.js index ce13ffa..8e084e9 100644 --- a/lib/path.js +++ b/lib/path.js @@ -101,8 +101,7 @@ function _sp(obj, kp, v) { * @returns {{dotIndex: Number, key: String, remaining: String}} */ function state(kp) { - let match = (/(? 0 ? kp[i - 1] : '', + currentChar = kp[i]; + if (currentChar === '.' && previousChar !== '\\') return i; + } + return -1; +} diff --git a/package-lock.json b/package-lock.json index 5d9d137..fb88e5e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "doc-path", - "version": "3.0.0", + "version": "3.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1047,9 +1047,9 @@ } }, "glob-parent": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.0.tgz", - "integrity": "sha512-qjtRgnIVmOfnKUE3NJAQEdk+lKrxfw8t5ke7SXtfMTHcjsBfOfWXCQfdb30zfDoZQ2IRSIiidmjtbHZPZ++Ihw==", + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", "dev": true, "requires": { "is-glob": "^4.0.1" diff --git a/package.json b/package.json index 54bc7e6..9a89c77 100755 --- a/package.json +++ b/package.json @@ -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",