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 0be42b4 commit ad85e46
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/config/normalize/lib/star_dot_path/parsing/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export const pathHasAny = function (path) {
const nodeHasAny = function (node) {
return node === ANY_TOKEN || (Array.isArray(node) && node.includes(ANY_TOKEN))
}

// Check if node is an array index
export const isIndexNode = function (node) {
return Number.isInteger(node)
}
3 changes: 2 additions & 1 deletion src/config/normalize/lib/star_dot_path/parsing/serialize.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isIndexNode } from './path.js'
import { SEPARATOR, ANY, ANY_TOKEN, SPECIAL_CHARS_REGEXP } from './special.js'

// Inverse of `parse()`
Expand All @@ -16,7 +17,7 @@ const serializeToken = function (token, index) {
return ANY
}

if (Number.isInteger(token)) {
if (isIndexNode(token)) {
return String(token)
}

Expand Down
8 changes: 4 additions & 4 deletions src/config/normalize/lib/star_dot_path/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setArray } from '../../../../utils/set.js'
import { listEntries, normalizeEntry } from './entries/main.js'
import { isObject } from './entries/recurse.js'
import { maybeParse } from './parsing/parse.js'
import { pathHasAny } from './parsing/path.js'
import { pathHasAny, isIndexNode } from './parsing/path.js'

// Set a value to one or multiple properties in `target` using a query string
export const set = function (target, queryOrPath, setValue) {
Expand Down Expand Up @@ -54,7 +54,7 @@ const addDefaultTarget = function (target, key) {
return target
}

return Number.isInteger(key) ? [] : {}
return isIndexNode(key) ? [] : {}
}

// Delete one or multiple properties in `target` using a query string
Expand Down Expand Up @@ -96,7 +96,7 @@ const excludeEntry = function (
}

const removeValue = function (value, key) {
if (!Number.isInteger(key)) {
if (!isIndexNode(key)) {
return omitLib.default(value, [key])
}

Expand All @@ -113,7 +113,7 @@ const setNewChildValue = function (value, key, newChildValue) {
return value
}

return Number.isInteger(key)
return isIndexNode(key)
? setArray(value, key, newChildValue)
: { ...value, [key]: newChildValue }
}

0 comments on commit ad85e46

Please sign in to comment.