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 62b3979 commit e61d217
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 54 deletions.
41 changes: 39 additions & 2 deletions src/config/normalize/lib/wild_wild_path/iterate/expand.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isPlainObj from 'is-plain-obj'

import { getObjectTokenType } from '../tokens/main.js'

// Iteration among siglings is not sorted, for performance reasons.
Expand Down Expand Up @@ -39,7 +41,42 @@ export const expandToken = function (
// - But are handled by the other ones
export const handleMissingValue = function (value, token, classes) {
const tokenType = getObjectTokenType(token)
const missing = tokenType.isMissing(value, classes)
const valueA = missing ? tokenType.defaultValue : value
const { missing, value: valueA } = tokenType.array
? handleMissingArrayValue(tokenType, value)
: handleMissingObjectValue(tokenType, value, classes)
return { tokenType, missing, value: valueA }
}

const handleMissingArrayValue = function (tokenType, value) {
return Array.isArray(value)
? { missing: false, value }
: { missing: true, value: [] }
}

const handleMissingObjectValue = function (tokenType, value, classes) {
return isRecurseObject(value, classes)
? { missing: false, value }
: { missing: true, value: {} }
}

// Whether a property is considered an object that can:
// - Be recursed over
// - Be cloned with `{...}`
// - Therefore we do not allow class instances
// Values that are not recursed are considered atomic, like simple types, i.e.:
// - Properties, even if they exist, will be considered missing
// - With tokens like *, no entries will be returned
// - Setting will override the value, not merge it
// This must return `false` for arrays.
// By default, we only consider plain objects
// - Excluding:
// - Class instances, including native ones (RegExp, Error, etc.)
// - `Object.create({})`
// - `import * as object from ...` (`Module` instance)
// - This is because only plain objects are clonable, i.e. do not require
// `mutate` to be `true`
const isRecurseObject = function (value, classes) {
return classes
? typeof value === 'object' && value !== null
: isPlainObj(value)
}
4 changes: 1 addition & 3 deletions src/config/normalize/lib/wild_wild_path/tokens/any.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import isPlainObj from 'is-plain-obj'

import { objectProps } from './common.js'

// Check the type of a parsed token
const testObject = function (token) {
return isPlainObj(token) && token.type === ANY_TYPE
Expand Down Expand Up @@ -53,12 +51,12 @@ const equals = function () {
}

export const ANY_TOKEN = {
array: false,
testObject,
serialize,
testString,
parse,
normalize,
...objectProps,
iterate,
equals,
}
4 changes: 1 addition & 3 deletions src/config/normalize/lib/wild_wild_path/tokens/array.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { arrayProps } from './common.js'

// Check the type of a parsed token.
// Integers specified as string tokens are assumed to be property names, not
// array indices.
Expand Down Expand Up @@ -58,12 +56,12 @@ const equals = function (tokenA, tokenB) {
}

export const ARRAY_TOKEN = {
array: true,
testObject,
serialize,
testString,
parse,
normalize,
...arrayProps,
iterate,
equals,
}
17 changes: 0 additions & 17 deletions src/config/normalize/lib/wild_wild_path/tokens/common.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/config/normalize/lib/wild_wild_path/tokens/prop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { objectProps } from './common.js'
import { ESCAPE, TOKEN_SEPARATOR, escapeSpecialChars } from './escape.js'
import { getOtherStringTokenType } from './other.js'

Expand Down Expand Up @@ -48,12 +47,12 @@ const equals = function (tokenA, tokenB) {
}

export const PROP_TOKEN = {
array: false,
testObject,
serialize,
testString,
parse,
normalize,
...objectProps,
iterate,
equals,
}
23 changes: 0 additions & 23 deletions src/config/normalize/lib/wild_wild_path/tokens/recurse.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/config/normalize/lib/wild_wild_path/tokens/regexp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { objectProps } from './common.js'
import { escapeSpecialChars } from './escape.js'

// Check the type of a parsed token
Expand Down Expand Up @@ -54,12 +53,12 @@ const iterate = function (value, token) {
}

export const REGEXP_TOKEN = {
array: false,
testObject,
serialize,
testString,
parse,
normalize,
...objectProps,
iterate,
equals,
}
3 changes: 1 addition & 2 deletions src/config/normalize/lib/wild_wild_path/tokens/slice.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import isPlainObj from 'is-plain-obj'

import { ARRAY_TOKEN, getArrayIndex } from './array.js'
import { arrayProps } from './common.js'

// Check the type of a parsed token.
const testObject = function (token) {
Expand Down Expand Up @@ -77,12 +76,12 @@ const equals = function (tokenA, tokenB) {
}

export const SLICE_TOKEN = {
array: true,
testObject,
serialize,
testString,
parse,
normalize,
...arrayProps,
iterate,
equals,
}

0 comments on commit e61d217

Please sign in to comment.