Skip to content

Commit

Permalink
Add **
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent 97b1959 commit 29d0821
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/config/normalize/lib/wild_wild_path/iterate/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parseQuery, serializePath } from '../../wild_wild_path_parser/main.js'
import { removeDuplicates } from './duplicate.js'
import { expandTokens } from './expand.js'
import { groupSortChildEntries } from './group.js'
import { expandRecursiveTokens } from './recurse.js'

// Iterate over all values (and their associated path) matching a specific
// query for on specific target value.
Expand All @@ -26,14 +27,15 @@ export const iterate = function* (
}

const iterateLevel = function* (entries, index, opts) {
const entriesA = removeDuplicates(entries)
const parentEntry = getParentEntry(entriesA, index)
const entriesA = expandRecursiveTokens(entries, index)
const entriesB = removeDuplicates(entriesA)
const parentEntry = getParentEntry(entriesB, index)

if (parentEntry !== undefined && !opts.childFirst) {
yield parentEntry
}

yield* iterateChildEntries({ entries: entriesA, parentEntry, index, opts })
yield* iterateChildEntries({ entries: entriesB, parentEntry, index, opts })

if (parentEntry !== undefined && opts.childFirst) {
yield parentEntry
Expand Down
34 changes: 34 additions & 0 deletions src/config/normalize/lib/wild_wild_path/iterate/recurse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getTokenType } from '../../wild_wild_path_parser/main.js'

// Some tokens are recursive. Those are expanded iteratively at each level.
export const expandRecursiveTokens = function (entries, index) {
return entries.flatMap((entry) => expandRecursiveToken(entry, index))
}

const expandRecursiveToken = function (entry, index) {
const { queryArray } = entry
const token = queryArray[index]
const tokenTypeName = getTokenType(token)
const recursor = RECURSORS[tokenTypeName]

if (recursor === undefined) {
return entry
}

const queryArrays = recursor(queryArray, index)
return queryArrays.map((queryArrayA) => ({
...entry,
queryArray: queryArrayA,
}))
}

// Handle ** recursion
const recurseAnyDeep = function (queryArray, index) {
const parentQuery = queryArray.slice(0, index)
const childQuery = queryArray.slice(index)
return [parentQuery, [...parentQuery, { type: 'any' }, ...childQuery]]
}

const RECURSORS = {
anyDeep: recurseAnyDeep,
}

0 comments on commit 29d0821

Please sign in to comment.