Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent 50002f3 commit 12a648d
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/config/normalize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import mapObj from 'map-obj'

import { normalizeLimit } from '../history/compare/normalize.js'
import { normalizeDelta } from '../history/delta/normalize.js'
import { validateMerge } from '../history/merge/id.js'
Expand Down Expand Up @@ -29,17 +27,35 @@ import { cRecurseConfigSelectors } from './select/normalize.js'
export const normalizeConfig = function (config, configInfos) {
// eslint-disable-next-line fp/no-mutating-methods
const configInfosA = [...configInfos].reverse()
return mapObj(config, (name, value) => [
name,
normalizePropValue(value, name, configInfosA),
])
return Object.entries(CONFIG_PROPS).reduce(
(configA, [name, normalizer]) =>
normalizePropConfig({
config: configA,
name,
normalizer,
configInfos: configInfosA,
}),
config,
)
}

const normalizePropValue = function (value, name, configInfos) {
const normalizer = CONFIG_PROPS[name]
return normalizer === undefined
? value
: runNormalizer(normalizer, value, name, configInfos)
const normalizePropConfig = function ({
config,
name,
normalizer,
configInfos,
}) {
const value = config[name]

if (value === undefined) {
return config
}

const newValue =
normalizer === undefined
? value
: runNormalizer(normalizer, value, name, configInfos)
return { ...config, [name]: newValue }
}

const CONFIG_PROPS = {
Expand Down

0 comments on commit 12a648d

Please sign in to comment.