Skip to content

Commit

Permalink
Rename normalize() to transform()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 23, 2022
1 parent d3dc50a commit 64c58f4
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 64 deletions.
23 changes: 8 additions & 15 deletions src/config/normalize/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pProps from 'p-props'

import { UserError } from '../../error/main.js'
import { cleanObject } from '../../utils/clean.js'
import { maybeFunction } from '../../utils/function.js'
import { mapValues } from '../../utils/map.js'
import { then } from '../../utils/then.js'

Expand Down Expand Up @@ -89,15 +90,15 @@ const handleGetUserError = function (message) {
const normalizePropValue = async function ({
value,
name,
configProp: { default: defaultValue, normalize },
configProp: { default: defaultValue, transform },
configInfos,
get,
}) {
const path = getPath(name)
const opts = { name, path, configInfos, get }

const valueA = await addDefaultValue(value, defaultValue, opts)
return await runPropNormalizer(valueA, normalize, opts)
return await transformProp(valueA, transform, opts)
}

const getPath = function (name) {
Expand All @@ -109,24 +110,16 @@ const getPathKey = function ({ key }) {
}

const addDefaultValue = async function (value, defaultValue, opts) {
if (value !== undefined) {
return value
}

if (typeof defaultValue !== 'function') {
return defaultValue
}

return await defaultValue(opts)
return value === undefined ? await maybeFunction(defaultValue, opts) : value
}

// Calls `normalize(value)` which transforms the value.
const runPropNormalizer = async function (value, normalize, opts) {
if (value === undefined || normalize === undefined) {
// Calls `transform(value)`
const transformProp = async function (value, transform, opts) {
if (value === undefined || transform === undefined) {
return value
}

const newValue = await normalize(value, opts)
const newValue = await transform(value, opts)
return newValue === undefined ? value : newValue
}

Expand Down
88 changes: 44 additions & 44 deletions src/config/normalize/properties.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable max-lines */
import { cwd as getCwd } from 'process'

import { normalizeLimit } from '../../history/compare/normalize.js'
import { normalizeDelta } from '../../history/delta/normalize.js'
import { transformLimit } from '../../history/compare/transform.js'
import { transformDelta } from '../../history/delta/transform.js'
import { getDefaultId, validateMerge } from '../../history/merge/id.js'
import { isOutputPath } from '../../report/output.js'
import { isTtyInput } from '../../report/tty.js'
import { normalizePrecision } from '../../run/precision.js'
import { transformPrecision } from '../../run/precision.js'
import { getDefaultConfig } from '../load/default.js'
import { recurseConfigSelectors } from '../select/normalize.js'

Expand All @@ -27,19 +27,19 @@ const config = {
async default() {
return await getDefaultConfig()
},
normalize(value) {
transform(value) {
return normalizeOptionalArray(value)
},
}

const configAny = {
normalize(value, { name }) {
transform(value, { name }) {
checkDefinedString(value, name)
},
}

const colors = {
normalize(value, { name }) {
transform(value, { name }) {
checkBoolean(value, name)
},
}
Expand All @@ -48,46 +48,46 @@ const cwd = {
default() {
return getCwd()
},
normalize(value, { name, configInfos }) {
transform(value, { name, configInfos }) {
checkDefinedString(value, name)
return normalizeConfigPath(value, name, configInfos)
},
}

const delta = {
default: 1,
normalize(value, { name }) {
return normalizeDelta(value, name)
transform(value, { name }) {
return transformDelta(value, name)
},
}

const force = {
default() {
return !isTtyInput()
},
normalize(value, { name }) {
transform(value, { name }) {
checkBoolean(value, name)
},
}

const inputs = {
default: {},
normalize(value, { name }) {
transform(value, { name }) {
checkObject(value, name)
},
}

const inputsAny = {
normalize(value, { name }) {
transform(value, { name }) {
checkJson(value, name)
},
}

const limit = {
normalize(value, { name }) {
transform(value, { name }) {
return recurseConfigSelectors(value, name, (childValue, childName) => {
checkInteger(childValue, childName)
return normalizeLimit(childValue, childName)
return transformLimit(childValue, childName)
})
},
}
Expand All @@ -96,14 +96,14 @@ const merge = {
default() {
return getDefaultId()
},
normalize(value, { name }) {
transform(value, { name }) {
checkDefinedString(value, name)
validateMerge(value, name)
},
}

const output = {
normalize(value, { name, configInfos }) {
transform(value, { name, configInfos }) {
checkDefinedString(value, name)
return isOutputPath(value)
? normalizeConfigPath(value, name, configInfos)
Expand All @@ -113,52 +113,52 @@ const output = {

const outliers = {
default: false,
normalize(value, { name }) {
transform(value, { name }) {
recurseConfigSelectors(value, name, checkBoolean)
},
}

const precision = {
default: 5,
normalize(value, { name }) {
transform(value, { name }) {
return recurseConfigSelectors(value, name, (childValue, childName) => {
checkInteger(childValue, childName)
return normalizePrecision(childValue, childName)
return transformPrecision(childValue, childName)
})
},
}

const quiet = {
normalize(value, { name }) {
transform(value, { name }) {
checkBoolean(value, name)
},
}

const reporter = {
default: ['debug'],
normalize(value) {
transform(value) {
return normalizeOptionalArray(value)
},
}

// `reporter` configuration property specific logic for the `remove` command
const reporterRemove = {
...reporter,
async normalize(value, { name, get }) {
async transform(value, { name, get }) {
const forceValue = await get('force')
return forceValue ? [] : reporter.normalize(value, { name })
return forceValue ? [] : reporter.transform(value, { name })
},
}

const reporterAny = {
normalize(value, { name }) {
transform(value, { name }) {
checkDefinedString(value, name)
},
}

const reporterConfig = {
default: {},
normalize(value, { name }) {
transform(value, { name }) {
checkObject(value, name)
},
}
Expand All @@ -169,55 +169,55 @@ const runner = {
// file, instead of to an optional one. This makes behavior easier to
// understand for users and provides with better error messages.
default: ['node'],
normalize(value, { name }) {
transform(value, { name }) {
const valueA = normalizeOptionalArray(value)
checkArrayLength(valueA, name)
return valueA
},
}

const runnerAny = {
normalize(value, { name }) {
transform(value, { name }) {
checkDefinedString(value, name)
},
}

const runnerConfig = {
default: {},
normalize(value, { name }) {
transform(value, { name }) {
checkObject(value, name)
},
}

const save = {
default: false,
normalize(value, { name }) {
transform(value, { name }) {
checkBoolean(value, name)
},
}

const select = {
default: [],
normalize(value) {
transform(value) {
return normalizeOptionalArray(value)
},
}

const selectAny = {
normalize(value, { name }) {
transform(value, { name }) {
checkString(value, name)
},
}

const showDiff = {
normalize(value, { name }) {
transform(value, { name }) {
recurseConfigSelectors(value, name, checkBoolean)
},
}

const showMetadata = {
default: true,
normalize(value, { name }) {
transform(value, { name }) {
checkBoolean(value, name)
},
}
Expand All @@ -230,67 +230,67 @@ const showMetadataRun = {

const showPrecision = {
default: false,
normalize(value, { name }) {
transform(value, { name }) {
recurseConfigSelectors(value, name, checkBoolean)
},
}

const showSystem = {
normalize(value, { name }) {
transform(value, { name }) {
checkBoolean(value, name)
},
}

const showTitles = {
default: false,
normalize(value, { name }) {
transform(value, { name }) {
recurseConfigSelectors(value, name, checkBoolean)
},
}

const since = {
default: 1,
normalize(value, { name }) {
return normalizeDelta(value, name)
transform(value, { name }) {
return transformDelta(value, name)
},
}

const system = {
default: {},
normalize(value, { name }) {
transform(value, { name }) {
checkObject(value, name)
},
}

const systemAny = {
normalize(value, { name }) {
transform(value, { name }) {
checkDefinedString(value, name)
},
}

const tasks = {
default: [],
normalize(value) {
transform(value) {
return normalizeOptionalArray(value)
},
}

const tasksAny = {
async normalize(value, { name, configInfos }) {
async transform(value, { name, configInfos }) {
checkDefinedString(value, name)
return await normalizeConfigGlob(value, name, configInfos)
},
}

const titles = {
default: {},
normalize(value, { name }) {
transform(value, { name }) {
checkObject(value, name)
},
}

const titlesAny = {
normalize(value, { name }) {
transform(value, { name }) {
checkDefinedString(value, name)
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/history/compare/diff.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getMatchingCombination } from '../../combination/result.js'
import { haveSimilarMeans } from '../../stats/similar.js'

import { isNegativeLimit } from './normalize.js'
import { isNegativeLimit } from './transform.js'

// Add `combination.stats.diff` which compares each combination with another
// result.
Expand Down
2 changes: 1 addition & 1 deletion src/history/compare/limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getCombinationName } from '../../combination/ids/name.js'
import { LimitError } from '../../error/main.js'
import { groupBy } from '../../utils/group.js'

import { isNegativeLimit } from './normalize.js'
import { isNegativeLimit } from './transform.js'

// If any `combination.stats.diff` is too slow compared to the `limit`
// configuration property, we fail.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Parse the `limit` configuration property.
// It is an integer representing a percentage.
export const normalizeLimit = function (limit) {
export const transformLimit = function (limit) {
return limit / PERCENTAGE_RATIO
}

Expand Down
Loading

0 comments on commit 64c58f4

Please sign in to comment.