Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 20, 2022
1 parent 891f228 commit 072f555
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 62 deletions.
3 changes: 2 additions & 1 deletion src/config/normalize/lib/wild_wild_parser/normalize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parsePath, parseQuery } from './parse.js'
import { normalizeQueryArrays } from './validate/arrays.js'
import { isQueryString, normalizeArrayPath } from './validate/main.js'
import { isQueryString } from './validate/main.js'
import { normalizeArrayPath } from './validate/path.js'

// There are two formats:
// - Query string
Expand Down
7 changes: 2 additions & 5 deletions src/config/normalize/lib/wild_wild_parser/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import {
SPECIAL_CHARS,
} from './tokens/escape.js'
import { getStringTokenType } from './tokens/main.js'
import {
normalizeArraysPath,
validateEmptyQuery,
validateQueryString,
} from './validate/main.js'
import { validateEmptyQuery, validateQueryString } from './validate/main.js'
import { normalizeArraysPath } from './validate/path.js'
import { throwQueryError } from './validate/throw.js'

// Parse a query string into an array of tokens.
Expand Down
2 changes: 1 addition & 1 deletion src/config/normalize/lib/wild_wild_parser/serialize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TOKEN_SEPARATOR, ARRAY_SEPARATOR } from './tokens/escape.js'
import { getObjectTokenType } from './tokens/main.js'
import { normalizeQueryArrays } from './validate/arrays.js'
import { normalizeArrayPath } from './validate/main.js'
import { normalizeArrayPath } from './validate/path.js'

// Inverse of `parseQuery()`
export const serializeQuery = function (queryArrays) {
Expand Down
56 changes: 1 addition & 55 deletions src/config/normalize/lib/wild_wild_parser/validate/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { getPathObjectTokenType } from '../tokens/main.js'

import { throwQueryError, throwTokenError } from './throw.js'
import { throwQueryError } from './throw.js'

// Validate query string is a string
export const validateQueryString = function (queryString) {
Expand All @@ -21,55 +19,3 @@ export const validateEmptyQuery = function (queryArrays, queryString) {
throwQueryError(queryString, 'It must not be an empty string.')
}
}

// Transform a queryArrays into a path, if possible
// Paths are a subset of query strings|arrays which use:
// - No unions
// - Only prop and index tokens (positive only)
// Those are the ones exposed in output, as opposed to query arrays which are
// exposed in input.
export const normalizeArraysPath = function (queryArrays, query) {
if (queryArrays.length !== 1) {
throwQueryError(query, 'It must not be a union.')
}

const [path] = queryArrays
return normalizeArrayPath(path, query)
}

// Ensure a queryArray is a path
export const normalizeArrayPath = function (path, query) {
if (!Array.isArray(path)) {
throwQueryError(query, 'It must be an array.')
}

if (path.some(Array.isArray)) {
throwQueryError(query, 'It must not be a union.')
}

path.forEach((prop) => {
validateProp(prop, query)
})
return path
}

const validateProp = function (prop, query) {
const tokenType = getPathObjectTokenType(prop)

if (tokenType === undefined) {
throwTokenError(
query,
prop,
'It must be a property name or an array index.',
)
}

if (isNegativeIndex(tokenType, prop)) {
throwTokenError(query, prop, 'It must not be a negative index.')
}
}

// Negative indices are not allowed in paths
const isNegativeIndex = function (tokenType, prop) {
return tokenType.name === 'index' && (Object.is(prop, -0) || prop < 0)
}
55 changes: 55 additions & 0 deletions src/config/normalize/lib/wild_wild_parser/validate/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { getPathObjectTokenType } from '../tokens/main.js'

import { throwQueryError, throwTokenError } from './throw.js'

// Transform a queryArrays into a path, if possible
// Paths are a subset of query strings|arrays which use:
// - No unions
// - Only prop and index tokens (positive only)
// Those are the ones exposed in output, as opposed to query arrays which are
// exposed in input.
export const normalizeArraysPath = function (queryArrays, query) {
if (queryArrays.length !== 1) {
throwQueryError(query, 'It must not be a union.')
}

const [path] = queryArrays
return normalizeArrayPath(path, query)
}

// Ensure a queryArray is a path
export const normalizeArrayPath = function (path, query) {
if (!Array.isArray(path)) {
throwQueryError(query, 'It must be an array.')
}

if (path.some(Array.isArray)) {
throwQueryError(query, 'It must not be a union.')
}

path.forEach((prop) => {
validateProp(prop, query)
})
return path
}

const validateProp = function (prop, query) {
const tokenType = getPathObjectTokenType(prop)

if (tokenType === undefined) {
throwTokenError(
query,
prop,
'It must be a property name or an array index.',
)
}

if (isNegativeIndex(tokenType, prop)) {
throwTokenError(query, prop, 'It must not be a negative index.')
}
}

// Negative indices are not allowed in paths
const isNegativeIndex = function (tokenType, prop) {
return tokenType.name === 'index' && (Object.is(prop, -0) || prop < 0)
}

0 comments on commit 072f555

Please sign in to comment.