Skip to content

Commit

Permalink
Start distinguishing paths and not paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Apr 3, 2022
1 parent f06d9bd commit f00d8c9
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/iterate/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import moize from 'moize'
import { normalizeQuery } from 'wild-wild-parser'
import { normalizePath, normalizeQuery } from 'wild-wild-parser'

import { iterateChildEntries } from './children.js'
import { removeDuplicates } from './duplicate.js'
Expand All @@ -13,13 +13,35 @@ import { expandRecursiveTokens } from './recurse.js'
// - To keep memory consumption low even on big queries
export const iterate = function* (target, query, opts) {
const optsA = getOptions(opts)
const { queryArrays, pathArray } = mNormalizePathOrQuery(query)
yield* pathArray === undefined
? iterateQuery(target, queryArrays, optsA)
: iteratePath(target, pathArray, optsA)
}

// Distinguish between queries that are paths or not
const normalizePathOrQuery = function (query) {
try {
return { pathArray: normalizePath(query) }
} catch {
return { queryArrays: normalizeQuery(query) }
}
}

// Due to memoization, `entry.path[*]` items should not be mutated by consumers
const mNormalizePathOrQuery = moize(normalizePathOrQuery, { maxSize: 1e3 })

const iteratePath = function* (target, pathArray, opts) {
yield* iterateQuery(target, [pathArray], opts)
}

const iterateQuery = function* (target, queryArrays, opts) {
const parents = new Set([])
const entries = getRootEntries(target, query)
yield* iterateLevel({ entries, index: 0, parents, opts: optsA })
const entries = getRootEntries(target, queryArrays)
yield* iterateLevel({ entries, index: 0, parents, opts })
}

const getRootEntries = function (target, query) {
const queryArrays = mNormalizeQuery(query)
const getRootEntries = function (target, queryArrays) {
return queryArrays.map((queryArray) => ({
queryArray,
value: target,
Expand All @@ -28,9 +50,6 @@ const getRootEntries = function (target, query) {
}))
}

// Due to memoization, `entry.path[*]` items should not be mutated by consumers
const mNormalizeQuery = moize(normalizeQuery, { maxSize: 1e3 })

// `parents` is used to prevent infinite recursions when using ** together with
// a value that includes references to itself
const iterateLevel = function* ({
Expand Down

0 comments on commit f00d8c9

Please sign in to comment.