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 112c890 commit 4fbfa9d
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/config/normalize/lib/star_dot_path/parsing/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const safeParseQuery = function (query) {

// Use imperative logic for performance
/* eslint-disable complexity, max-depth, max-statements, fp/no-loops,
fp/no-mutation, fp/no-mutating-methods, fp/no-let */
fp/no-mutation, fp/no-let */
const parseQuery = function (query) {
if (query === '') {
return []
Expand All @@ -80,31 +80,41 @@ const parseQuery = function (query) {
const char = query[index]

if (char === ESCAPE) {
index += 1
state.chars += parseEscapedChar(query[index])
index = addEscapedChar(query, index, state)
} else if (char === SEPARATOR || index === query.length) {
path.push(parseToken(state))
state.hasAny = false
state.hasMinus = false
state.hasRegExp = false
state.chars = ''
addToken(path, state)
} else {
if (state.chars.length === 0) {
state.hasAny = state.hasAny || char === ANY
state.hasMinus = state.hasMinus || char === MINUS
state.hasRegExp = state.hasRegExp || char === REGEXP_DELIM
}

state.chars += char
addChar(char, state)
}
}

return path
}
/* eslint-enable complexity, max-depth, max-statements, fp/no-loops,
fp/no-mutation, fp/no-mutating-methods, fp/no-let */
fp/no-mutation, fp/no-let */

const addEscapedChar = function (query, index, state) {
const indexA = index + 1
state.chars += parseEscapedChar(query[indexA])
return indexA
}

const parseToken = function (state) {
const addToken = function (path, state) {
const tokenType = getStringTokenType(state)
return tokenType.parse(state.chars)
const token = tokenType.parse(state.chars)
path.push(token)
state.hasAny = false
state.hasMinus = false
state.hasRegExp = false
state.chars = ''
}

const addChar = function (char, state) {
if (state.chars.length === 0) {
state.hasAny = state.hasAny || char === ANY
state.hasMinus = state.hasMinus || char === MINUS
state.hasRegExp = state.hasRegExp || char === REGEXP_DELIM
}

state.chars += char
}

0 comments on commit 4fbfa9d

Please sign in to comment.