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 3a4c0ee commit f9f646b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
39 changes: 3 additions & 36 deletions src/config/normalize/lib/star_dot_path/parsing/parse.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { parseAnyToken } from '../tokens/any.js'
import { hasIndex, parseIndexToken } from '../tokens/array.js'
import { parseEscapedChar } from '../tokens/escape.js'
import { parsePropToken } from '../tokens/prop.js'
import { parseRegExpToken } from '../tokens/regexp.js'
import { getCharsTokenType } from '../tokens/main.js'
import {
ESCAPE,
SEPARATOR,
Expand Down Expand Up @@ -108,36 +105,6 @@ const parseQuery = function (query) {
fp/no-mutation, fp/no-mutating-methods, fp/no-let */

const parseToken = function (state) {
if (testAnyChars(state)) {
return parseAnyToken(state.chars)
}

if (testRegExpChars(state)) {
return parseRegExpToken(state.chars)
}

if (testIndexChars(state)) {
return parseIndexToken(state.chars)
}

if (testPropChars(state)) {
return parsePropToken(state.chars)
}
}

const testAnyChars = function ({ hasAny }) {
return hasAny
}

const testRegExpChars = function ({ hasRegExp }) {
return hasRegExp
}

const testIndexChars = function ({ chars, hasMinus }) {
const hasEscapedMinus = chars[0] === MINUS && !hasMinus
return hasIndex(chars, hasEscapedMinus)
}

const testPropChars = function () {
return true
const tokenType = getCharsTokenType(state)
return tokenType.parse(state.chars)
}
8 changes: 7 additions & 1 deletion src/config/normalize/lib/star_dot_path/tokens/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ const serialize = function () {
return ANY
}

const testChars = function ({ hasAny }) {
return hasAny
}

// Parse a * string into a token
export const parseAnyToken = function (chars) {
const parse = function (chars) {
if (chars !== ANY) {
throw new Error(
`character "${ANY}" must not be preceded or followed by other characters except "${SEPARATOR}"
Expand Down Expand Up @@ -57,6 +61,8 @@ const getEntries = function (value, path) {
export const ANY_TOKEN = {
test,
serialize,
testChars,
parse,
handleMissingValue,
getEntries,
}
9 changes: 7 additions & 2 deletions src/config/normalize/lib/star_dot_path/tokens/array.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MINUS } from './special.js'

// Users can specify integers either:
// - stringified for object properties
// - left as is for array indices
Expand All @@ -16,14 +18,15 @@ const serialize = function (token) {
}

// Check if a string should be parsed as an index token
export const hasIndex = function (chars, hasEscapedMinus) {
const testChars = function ({ chars, hasMinus }) {
const hasEscapedMinus = chars[0] === MINUS && !hasMinus
return !hasEscapedMinus && INTEGER_REGEXP.test(chars)
}

const INTEGER_REGEXP = /^-?\d+$/u

// Parse an array index string into a token
export const parseIndexToken = function (chars) {
const parse = function (chars) {
return Number(chars)
}

Expand Down Expand Up @@ -51,6 +54,8 @@ const getArrayIndex = function (array, token) {
export const ARRAY_TOKEN = {
test,
serialize,
testChars,
parse,
handleMissingValue,
getEntries,
}
7 changes: 6 additions & 1 deletion src/config/normalize/lib/star_dot_path/tokens/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { REGEXP_TOKEN } from './regexp.js'
// Order is significant as they are tested serially
const TOKEN_TYPES = [ANY_TOKEN, REGEXP_TOKEN, ARRAY_TOKEN, PROP_TOKEN]

// Retrieve the type of a given token
// Retrieve the type of a given token object
export const getTokenType = function (token) {
return TOKEN_TYPES.find((tokenType) => tokenType.test(token))
}

// Retrieve the type of a given token string of characters
export const getCharsTokenType = function (token) {
return TOKEN_TYPES.find((tokenType) => tokenType.testChars(token))
}
8 changes: 7 additions & 1 deletion src/config/normalize/lib/star_dot_path/tokens/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ const serialize = function (token, index) {
return token === '' && index === 0 ? SEPARATOR : escapeSpecialChars(token)
}

const testChars = function () {
return true
}

// Parse a property string into a token
export const parsePropToken = function (chars) {
const parse = function (chars) {
return chars
}

Expand All @@ -32,6 +36,8 @@ const getEntries = function (value, path, token) {
export const PROP_TOKEN = {
test,
serialize,
testChars,
parse,
handleMissingValue,
getEntries,
}
8 changes: 7 additions & 1 deletion src/config/normalize/lib/star_dot_path/tokens/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ const serialize = function (token) {
return `${REGEXP_DELIM}${source}${REGEXP_DELIM}${token.flags}`
}

const testChars = function ({ hasRegExp }) {
return hasRegExp
}

// Parse a RegExp string into a token.
// This might throw if the RegExp is invalid.
export const parseRegExpToken = function (chars) {
const parse = function (chars) {
const endIndex = chars.lastIndexOf(REGEXP_DELIM)

if (endIndex === 0) {
Expand Down Expand Up @@ -52,6 +56,8 @@ const getEntries = function (value, path, token) {
export const REGEXP_TOKEN = {
test,
serialize,
testChars,
parse,
handleMissingValue,
getEntries,
}

0 comments on commit f9f646b

Please sign in to comment.