Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 6, 2022
1 parent c625da0 commit ce6e527
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
6 changes: 4 additions & 2 deletions src/config/normalize/lib/star_dot_path/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const listTokenEntries = function (entries, token) {

const getTokenEntries = function ({ value, path }, token) {
const tokenType = getObjectTokenType(token)
const { value: valueA, missing } = tokenType.handleMissingValue(value)
const missing = !tokenType.isDefined(value)
const valueA = missing ? tokenType.defaultValue : value
const entries = tokenType.getEntries(valueA, path, token)
return entries.map((entry) => ({
value: entry.value,
Expand All @@ -39,7 +40,8 @@ const getTokenEntries = function ({ value, path }, token) {
// - Negative are converted to index 0
export const handleMissingValue = function (value, token) {
const tokenType = getObjectTokenType(token)
const { value: valueA } = tokenType.handleMissingValue(value)
const missing = !tokenType.isDefined(value)
const valueA = missing ? tokenType.defaultValue : value
return valueA
}

Expand Down
24 changes: 10 additions & 14 deletions src/config/normalize/lib/star_dot_path/tokens/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,26 @@ Otherwise, please escape it with a "${ESCAPE}".`,
}

// When the token is missing a target value, add a default one.
// When missing, there are no entries, so no need to add missing entries.
const handleMissingValue = function (value) {
return { value, missing: false }
const isDefined = function (value) {
return isRecurseObject(value)
}

const defaultValue = {}

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

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

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

// Check if two tokens are the same
Expand All @@ -71,7 +66,8 @@ export const ANY_TOKEN = {
serialize,
testString,
parse,
handleMissingValue,
isDefined,
defaultValue,
getEntries,
equals,
}
11 changes: 6 additions & 5 deletions src/config/normalize/lib/star_dot_path/tokens/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ const parse = function (chars) {
}

// When the token is missing a target value, add a default one.
const handleMissingValue = function (value) {
const missing = !Array.isArray(value)
const valueA = missing ? [] : value
return { value: valueA, missing }
const isDefined = function (value) {
return Array.isArray(value)
}

const defaultValue = []

// Use the token to list entries against a target value.
const getEntries = function (value, path, token) {
const index = getArrayIndex(value, token)
Expand All @@ -56,7 +56,8 @@ export const ARRAY_TOKEN = {
serialize,
testString,
parse,
handleMissingValue,
isDefined,
defaultValue,
getEntries,
equals,
}
11 changes: 6 additions & 5 deletions src/config/normalize/lib/star_dot_path/tokens/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ const parse = function (chars) {
}

// When the token is missing a target value, add a default one.
const handleMissingValue = function (value) {
const missing = !isRecurseObject(value)
const valueA = missing ? {} : value
return { value: valueA, missing }
const isDefined = function (value) {
return isRecurseObject(value)
}

const defaultValue = {}

// Use the token to list entries against a target value.
const getEntries = function (value, path, token) {
return [{ value: value[token], path: [...path, token] }]
Expand All @@ -44,7 +44,8 @@ export const PROP_TOKEN = {
serialize,
testString,
parse,
handleMissingValue,
isDefined,
defaultValue,
getEntries,
equals,
}
20 changes: 7 additions & 13 deletions src/config/normalize/lib/star_dot_path/tokens/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,17 @@ const parse = function (chars) {
}

// When the token is missing a target value, add a default one.
// When missing, there are no entries, so no need to add missing entries.
const handleMissingValue = function (value) {
return { value, missing: false }
const isDefined = function (value) {
return isRecurseObject(value)
}

const defaultValue = {}

// Use the token to list entries against a target value.
const getEntries = function (value, path, token) {
if (!isRecurseObject(value)) {
return []
}

return Object.keys(value)
.filter((childKey) => token.test(childKey))
.map((childKey) => ({
value: value[childKey],
path: [...path, childKey],
missing: false,
}))
.map((childKey) => ({ value: value[childKey], path: [...path, childKey] }))
}

// Check if two tokens are the same
Expand All @@ -65,7 +58,8 @@ export const REGEXP_TOKEN = {
serialize,
testString,
parse,
handleMissingValue,
isDefined,
defaultValue,
getEntries,
equals,
}

0 comments on commit ce6e527

Please sign in to comment.