Skip to content

Commit

Permalink
Improve normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent 5211ca1 commit 332fdfd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 49 deletions.
6 changes: 6 additions & 0 deletions src/config/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export const checkBoolean = function (value, name) {
}
}

export const checkInteger = function (value, name) {
if (!Number.isInteger(value)) {
throw new UserError(`'${name}' must be an integer: ${inspect(value)}`)
}
}

// Many array configuration properties can optionally a single element, for
// simplicity providing it is the most common use case.
// We also allow duplicate values but remove them.
Expand Down
44 changes: 12 additions & 32 deletions src/config/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { normalizePrecision } from '../run/precision.js'

import {
checkBoolean,
checkInteger,
checkStringsObject,
normalizeOptionalArray,
checkArrayLength,
Expand Down Expand Up @@ -52,36 +53,15 @@ const normalizePropValue = function (value, propName, name) {
return value
}

const newValue = normalizers.reduce(
(valueA, normalizer) => normalizer(valueA, name),
return normalizers.reduce(
(valueA, normalizer) => applyNormalizer(valueA, name, normalizer),
value,
)
return newValue === undefined ? value : newValue
}

const normalizeReporter = function (value, name) {
const valueA = normalizeOptionalArray(value)
checkDefinedStringArray(valueA, name)
return valueA
}

const normalizeRunner = function (value, name) {
const valueA = normalizeOptionalArray(value)
checkDefinedStringArray(valueA, name)
checkArrayLength(valueA, name)
return valueA
}

const normalizeSelect = function (value, name) {
const valueA = normalizeOptionalArray(value)
checkStringArray(valueA, name)
return valueA
}

const normalizeTasks = function (value, name) {
const valueA = normalizeOptionalArray(value)
checkDefinedStringArray(valueA, name)
return valueA
const applyNormalizer = function (value, name, normalizer) {
const newValue = normalizer(value, name)
return newValue === undefined ? value : newValue
}

const validateTitles = function (value, name) {
Expand All @@ -92,17 +72,17 @@ const validateTitles = function (value, name) {

const NORMALIZERS = {
inputs: [checkJsonObject],
limit: [normalizeLimit],
limit: [checkInteger, normalizeLimit],
merge: [validateMerge],
outliers: [checkBoolean],
precision: [normalizePrecision],
reporter: [normalizeReporter],
runner: [normalizeRunner],
select: [normalizeSelect],
precision: [checkInteger, normalizePrecision],
reporter: [normalizeOptionalArray, checkDefinedStringArray],
runner: [normalizeOptionalArray, checkDefinedStringArray, checkArrayLength],
select: [normalizeOptionalArray, checkStringArray],
showDiff: [checkBoolean],
showPrecision: [checkBoolean],
showTitles: [checkBoolean],
system: [checkStringsObject],
tasks: [normalizeTasks],
tasks: [normalizeOptionalArray, checkDefinedStringArray],
titles: [validateTitles],
}
10 changes: 1 addition & 9 deletions src/history/compare/normalize.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { UserError } from '../../error/main.js'

// Parse the `limit` configuration property.
// It is an integer representing a percentage.
export const normalizeLimit = function (limit, name) {
if (!Number.isInteger(limit)) {
throw new UserError(
`'${name}' must be an integer (representing a percentage): ${limit}`,
)
}

export const normalizeLimit = function (limit) {
return limit / PERCENTAGE_RATIO
}

Expand Down
4 changes: 2 additions & 2 deletions src/history/merge/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { v4 as uuidv4, validate as isUuid } from 'uuid'
import { UserError } from '../../error/main.js'

// Validate `merge` property
export const validateMerge = function (value, propName) {
export const validateMerge = function (value, name) {
if (!isValidId(value) && value !== LAST_ID) {
throw new UserError(
`'${propName}' must be "${LAST_ID}" or a UUID: ${inspect(value)}`,
`'${name}' must be "${LAST_ID}" or a UUID: ${inspect(value)}`,
)
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/run/precision.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ import { UserError } from '../error/main.js'
// - this can happen inside the same run (due to previous runs being
// merged to current one)
export const normalizePrecision = function (precision, propName) {
if (!Number.isInteger(precision)) {
throw new UserError(
`'${propName}' must be a positive integer: ${precision}`,
)
}

const precisionA = PRECISION_TARGETS[precision]

if (precisionA === undefined) {
Expand Down

0 comments on commit 332fdfd

Please sign in to comment.