Comparing version 4.0.2 to 4.1.0
@@ -20,6 +20,7 @@ /** | ||
const keyVal = typeof obj === 'object' && key in obj ? obj[key] : undefined; | ||
// If there is a '.' in the key path and the key path doesn't appear in the object, recur on the subobject | ||
if (dotIndex >= 0 && typeof obj === 'object' && !(kp in obj)) { | ||
const { key: nextKey } = state(remaining); | ||
const nextKeyAsInt = parseInt(nextKey); | ||
// If there's an array at the current key in the object, then iterate over those items evaluating the remaining path | ||
if (Array.isArray(keyVal)) { | ||
if (Array.isArray(keyVal) && isNaN(nextKeyAsInt)) { | ||
return keyVal.map((doc) => evaluatePath(doc, remaining)); | ||
@@ -31,2 +32,6 @@ } | ||
else if (Array.isArray(obj)) { | ||
const keyAsInt = parseInt(key); | ||
if (kp === key && dotIndex === -1 && !isNaN(keyAsInt)) { | ||
return keyVal; | ||
} | ||
// If this object is actually an array, then iterate over those items evaluating the path | ||
@@ -69,4 +74,12 @@ return obj.map((doc) => evaluatePath(doc, kp)); | ||
if (dotIndex >= 0) { | ||
const keyAsInt = parseInt(key); | ||
// If there is a '.' in the key path, recur on the subdoc and ... | ||
if (typeof obj === 'object' && obj !== null && !(key in obj) && Array.isArray(obj)) { | ||
if (typeof obj === 'object' && obj !== null && !(key in obj) && Array.isArray(obj) && !isNaN(keyAsInt)) { | ||
// If there's no value at obj[key] then populate an empty object | ||
obj[key] = obj[key] ?? {}; | ||
// Continue iterating on the rest of the key path to set the appropriate value where intended and then return | ||
_sp(obj[key], remaining, v); | ||
return obj; | ||
} | ||
else if (typeof obj === 'object' && obj !== null && !(key in obj) && Array.isArray(obj)) { | ||
// If this is an array and there are multiple levels of keys to iterate over, recur. | ||
@@ -77,4 +90,12 @@ obj.forEach((doc) => _sp(doc, kp, v)); | ||
else if (typeof obj === 'object' && obj !== null && !(key in obj) && !Array.isArray(obj)) { | ||
// If the current key doesn't exist yet, populate it | ||
obj[key] = {}; | ||
const { key: nextKey } = state(remaining); | ||
const nextKeyAsInt = parseInt(nextKey); | ||
if (!isNaN(nextKeyAsInt)) { | ||
// If the current key doesn't exist yet and the next key is a number (likely array index), populate an empty array | ||
obj[key] = []; | ||
} | ||
else { | ||
// If the current key doesn't exist yet, populate it | ||
obj[key] = {}; | ||
} | ||
} | ||
@@ -84,2 +105,8 @@ _sp(obj[key], remaining, v); | ||
else if (Array.isArray(obj)) { | ||
const keyAsInt = parseInt(key); | ||
// If the object is an array and this key is an int (likely array index), then set the value directly and return | ||
if (kp === key && dotIndex === -1 && !isNaN(keyAsInt)) { | ||
obj[key] = v; | ||
return obj; | ||
} | ||
// If this "obj" is actually an array, then we can loop over each of the values and set the path | ||
@@ -86,0 +113,0 @@ obj.forEach((doc) => _sp(doc, remaining, v)); |
@@ -5,3 +5,3 @@ { | ||
"description": "A document path library for Node", | ||
"version": "4.0.2", | ||
"version": "4.1.0", | ||
"homepage": "https://mrodrig.github.io/doc-path", | ||
@@ -8,0 +8,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11825
143