Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent 7af8fe3 commit 5b44171
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/config/normalize/lib/wild_wild_path_parser/tokens/any.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
import isPlainObj from 'is-plain-obj'

// Check the type of a parsed token
const testObject = function (token) {
return isPlainObj(token) && token.type === ANY_TYPE
// Create a token type with:
// - The string format being a specific string
// - The array format being a plain object with a single `type` property
const createSimpleTokenType = function (name, tokenString) {
return {
name,
testObject: testObject.bind(undefined, name),
serialize: serialize.bind(undefined, tokenString),
testString: testString.bind(undefined, tokenString),
parse: parse.bind(undefined, name),
normalize,
equals,
}
}

const ANY_TYPE = 'any'
// Check the type of a parsed token
const testObject = function (type, token) {
return isPlainObj(token) && token.type === type
}

// Serialize a token to a string
const serialize = function () {
return ANY
const serialize = function (tokenString) {
return tokenString
}

// Check the type of a serialized token
const testString = function (chars) {
return chars === ANY
const testString = function (tokenString, chars) {
return chars === tokenString
}

// Parse a string into a token
const parse = function () {
return { type: ANY_TYPE }
const parse = function (type) {
return { type }
}

const ANY = '*'

// Normalize value after parsing or serializing
const normalize = function ({ type }) {
return { type }
Expand All @@ -34,12 +45,4 @@ const equals = function () {
return true
}

export const ANY_TOKEN = {
name: 'any',
testObject,
serialize,
testString,
parse,
normalize,
equals,
}
export const ANY_TOKEN = createSimpleTokenType('any', '*')

0 comments on commit 5b44171

Please sign in to comment.