Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Apr 3, 2022
1 parent 6f4205f commit abd2397
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/iterate/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ export const iteratePath = function* (
pathArray,
{ missing: missingOpt, entries },
) {
const { value, missing } = iterateLevel(target, pathArray, 0)
const { value, missing } = getDeepValue(target, pathArray)

if (!missing || missingOpt) {
yield entries ? { value, path: pathArray, missing } : value
}
}

const iterateLevel = function (value, pathArray, index) {
const prop = pathArray[index]
const getDeepValue = function (value, pathArray) {
// eslint-disable-next-line fp/no-loops
for (const prop of pathArray) {
// eslint-disable-next-line max-depth
if (!isPresent(value, prop) || !isAllowedProp(prop)) {
return { value: undefined, missing: true }
}

if (prop === undefined) {
return { value, missing: false }
// eslint-disable-next-line no-param-reassign, fp/no-mutation
value = value[prop]
}

return isPresent(value, prop) && isAllowedProp(prop)
? iterateLevel(value[prop], pathArray, index + 1)
: { value: undefined, missing: true }
return { value, missing: false }
}

const isPresent = function (value, prop) {
Expand Down

0 comments on commit abd2397

Please sign in to comment.