Skip to content

Commit

Permalink
Improve get logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent a011f30 commit 0231950
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/config/normalize/prop_path/get.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { listEntries } from './entries.js'

// Retrieve all entries (values + paths) matching a query string in `target`
// Retrieve all entries (value + query + path) matching a query string
// in `target`
export const getEntries = function (target, query) {
const entries = listEntries(target, query)
return entries.map(normalizeEntry)
}

const normalizeEntry = function ({ value, path }) {
const pathStr = serializePath({ path })
return { path: pathStr, value }
const entryPath = getEntryPath({ path })
const query = serializeQuery(entryPath)
return { value, query, path: entryPath }
}

// Same but only retrieving the values
Expand All @@ -21,18 +23,19 @@ const getEntryValue = function ({ value }) {
return value
}

// Same but only retrieving the paths
export const getPaths = function (target, query) {
// Same but only retrieving the queries
export const getQueries = function (target, query) {
const entries = listEntries(target, query)
return entries.map(serializePath)
return entries.map(getEntryQuery)
}

const serializePath = function ({ path }) {
return path.map(getPathKey).reduce(appendKey, '')
const getEntryQuery = function ({ path }) {
const entryPath = getEntryPath({ path })
return serializeQuery(entryPath)
}

const getPathKey = function ({ key }) {
return key
const serializeQuery = function (entryPath) {
return entryPath.reduce(appendKey, '')
}

const appendKey = function (pathStr, key) {
Expand All @@ -42,3 +45,17 @@ const appendKey = function (pathStr, key) {

return pathStr === '' ? `${pathStr}${key}` : `${pathStr}.${key}`
}

// Same but only retrieving the paths
export const getPaths = function (target, query) {
const entries = listEntries(target, query)
return entries.map(getEntryPath)
}

const getEntryPath = function ({ path }) {
return path.map(getPathKey)
}

const getPathKey = function ({ key }) {
return key
}

0 comments on commit 0231950

Please sign in to comment.