Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent c605dd2 commit f05af53
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
19 changes: 15 additions & 4 deletions src/config/normalize/lib/wild_wild_path/iterate/main.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { parseQuery, serializePath } from '../../wild_wild_path_parser/main.js'
import { validateInherited } from '../validate.js'

import { removeDuplicates } from './duplicate.js'
import { expandTokens } from './expand.js'
import { groupSortChildEntries } from './group.js'
import { getOpts } from './options.js'
import { expandRecursiveTokens } from './recurse.js'

// Iterate over all values (and their associated path) matching a specific
// query for on specific target value.
// Uses an iterator:
// - To allow consumers to return only the first matching entry quickly
// - To keep memory consumption low even on big queries
export const iterate = function* (target, query, opts) {
const optsA = getOpts(opts)
export const iterate = function* (
target,
query,
{
childFirst = false,
sort = false,
missing = false,
classes = false,
inherited = false,
} = {},
) {
const opts = { childFirst, sort, missing, classes, inherited }
validateInherited(opts)
const parents = new Set([])
const queryArrays = parseQuery(query)
const entries = queryArrays.map((queryArray) => ({
Expand All @@ -21,7 +32,7 @@ export const iterate = function* (target, query, opts) {
path: [],
missing: false,
}))
yield* iterateLevel({ entries, index: 0, parents, opts: optsA })
yield* iterateLevel({ entries, index: 0, parents, opts })
}

// `parents` is used to prevent infinite recursions when using ** together with
Expand Down
21 changes: 0 additions & 21 deletions src/config/normalize/lib/wild_wild_path/iterate/options.js

This file was deleted.

10 changes: 1 addition & 9 deletions src/config/normalize/lib/wild_wild_path/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isParentPath } from '../wild_wild_path_parser/main.js'

import { list } from './get.js'
import { getMissingValue } from './iterate/missing.js'
import { validateClasses } from './validate.js'

// Set a value to one or multiple properties in `target` using a query string.
// Unless `mutate` is `true`, this returns a new copy
Expand All @@ -19,15 +20,6 @@ export const set = function (
return reduceParents({ target, query, setFunc, missing, classes, inherited })
}

// Class instances are not clonable. Therefore, they require `mutate`.
export const validateClasses = function (classes, mutate) {
if (classes && !mutate) {
throw new Error(
'The "mutate" option must be true when the "classes" option is true.',
)
}
}

// Modify a target object multiple times for each matched property.
export const reduceParents = function ({
target,
Expand Down
17 changes: 17 additions & 0 deletions src/config/normalize/lib/wild_wild_path/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Class instances are not clonable. Therefore, they require `mutate`.
export const validateClasses = function (classes, mutate) {
if (classes && !mutate) {
throw new Error(
'The "mutate" option must be true when the "classes" option is true.',
)
}
}

// Without classes, there are no inherited properties
export const validateInherited = function ({ classes, inherited }) {
if (inherited && !classes) {
throw new Error(
'The "classes" option must be true when the "inherited" option is true.',
)
}
}

0 comments on commit f05af53

Please sign in to comment.