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 88e4559 commit 7c23613
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/config/normalize/lib/wild_wild_path/iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ const iterateLevel = function (entries, index) {
const iteratePath = function ({ path, value, props }, index) {
const token = path[index]
const { tokenType, missing, value: valueA } = handleMissingValue(value, token)
const levelEntries = tokenType.iterate(valueA, token, missing)
const levelEntries = tokenType.iterate(valueA, token)
return levelEntries.map(({ value: childValue, prop, missing: missingA }) => ({
path,
value: childValue,
props: [...props, prop],
missing: missingA,
missing: missing || missingA,
}))
}

Expand Down
6 changes: 3 additions & 3 deletions src/config/normalize/lib/wild_wild_path/tokens/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ const normalize = function ({ type }) {

// Use the token to list entries against a target value.
// We purposely ignore symbol properties by using `Object.keys()`.
const iterate = function (value, token, missing) {
const iterate = function (value) {
if (Array.isArray(value)) {
return value.map((childValue, index) => ({
value: childValue,
prop: index,
missing,
missing: false,
}))
}

return Object.keys(value).map((childKey) => ({
value: value[childKey],
prop: childKey,
missing,
missing: false,
}))
}

Expand Down
10 changes: 2 additions & 8 deletions src/config/normalize/lib/wild_wild_path/tokens/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ const normalize = function (token) {
}

// Use the token to list entries against a target value.
const iterate = function (value, token, missing) {
const iterate = function (value, token) {
const index = getArrayIndex(value, token)
return [
{
value: value[index],
prop: index,
missing: missing || index >= value.length,
},
]
return [{ value: value[index], prop: index, missing: index >= value.length }]
}

// Negative array indices start from the end.
Expand Down
6 changes: 2 additions & 4 deletions src/config/normalize/lib/wild_wild_path/tokens/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ const normalize = function (token) {
// We distinguish between:
// - Missing property name: return no entries
// - Property exists but has `undefined` value: return an entry
const iterate = function (value, token, missing) {
return [
{ value: value[token], prop: token, missing: missing || !(token in value) },
]
const iterate = function (value, token) {
return [{ value: value[token], prop: token, missing: !(token in value) }]
}

// Check if two tokens are the same
Expand Down
8 changes: 6 additions & 2 deletions src/config/normalize/lib/wild_wild_path/tokens/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ const equals = function (tokenA, tokenB) {
}

// Use the token to list entries against a target value.
const iterate = function (value, token, missing) {
const iterate = function (value, token) {
return Object.keys(value)
.filter((childKey) => token.test(childKey))
.map((childKey) => ({ value: value[childKey], prop: childKey, missing }))
.map((childKey) => ({
value: value[childKey],
prop: childKey,
missing: false,
}))
}

export const REGEXP_TOKEN = {
Expand Down
4 changes: 2 additions & 2 deletions src/config/normalize/lib/wild_wild_path/tokens/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ const normalize = function ({ type, from = 0, to }) {
}

// Use the token to list entries against a target value.
const iterate = function (value, { from, to }, missing) {
const iterate = function (value, { from, to }) {
const fromIndex = getBoundedIndex(value, from)
const toIndex = Math.max(getBoundedIndex(value, to), fromIndex)
return new Array(toIndex - fromIndex).fill().map((_, index) => ({
value: value[index + fromIndex],
prop: index + fromIndex,
missing,
missing: false,
}))
}

Expand Down

0 comments on commit 7c23613

Please sign in to comment.