Skip to content

Commit

Permalink
Improve delta normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent c27d062 commit 1e0b357
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 43 deletions.
12 changes: 2 additions & 10 deletions src/config/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { normalizeDeltas } from '../history/delta/normalize.js'

import { addDefaultConfig } from './default.js'
import { loadConfig } from './load/main.js'
import { normalizeConfig } from './normalize.js'
Expand All @@ -11,17 +9,11 @@ import { validateConfig } from './validate.js'
// Retrieve configuration
export const getConfig = async function (command, configFlags = {}) {
const { config, configInfos } = await loadConfig(configFlags)

validateConfig(config)

const configA = addDefaultConfig(config, command)

const configB = pickCommandConfig(configA, command)

const configC = normalizeConfig(configB)
const configD = await normalizeConfigPaths(configC, configInfos)
const configE = normalizeDeltas(configD)

const configF = await addPlugins(configE, command)
return configF
const configE = await addPlugins(configD, command)
return configE
}
3 changes: 3 additions & 0 deletions src/config/normalize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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'
import { normalizePrecision } from '../run/precision.js'

Expand Down Expand Up @@ -67,6 +68,7 @@ const applyNormalizer = function (value, name, normalizer) {
const NORMALIZERS = {
colors: [checkBoolean],
cwd: [checkString, checkDefinedString],
delta: [normalizeDelta],
inputs: [checkObjectProps.bind(undefined, [checkJson])],
limit: [checkInteger, normalizeLimit],
merge: [validateMerge],
Expand All @@ -88,6 +90,7 @@ const NORMALIZERS = {
showDiff: [checkBoolean],
showPrecision: [checkBoolean],
showTitles: [checkBoolean],
since: [normalizeDelta],
system: [checkObjectProps.bind(undefined, [checkString])],
tasks: [
normalizeOptionalArray,
Expand Down
11 changes: 0 additions & 11 deletions src/config/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ const DYNAMIC_OBJECT_PROPS = [
'inputs',
]

const VALID_TIMESTAMPS = [
'yyyy-mm-dd',
'yyyymmdd',
'yyyy-mm-dd hh:mm:ss',
'yyyy-mm-dd hh:mm:ssZ',
]

const VALID_DELTA = multipleValidOptions(true, 3, ...VALID_TIMESTAMPS)

const optionalArray = function (value) {
return multipleValidOptions(value, [value])
}
Expand All @@ -44,7 +35,6 @@ const optionalConfigSelectors = function (valueA, valueB) {

const EXAMPLE_CONFIG = {
...DEFAULT_CONFIG,
delta: VALID_DELTA,
force: true,
inputs: { inputId: 'inputValue' },
limit: optionalConfigSelectors(3, 10),
Expand All @@ -63,7 +53,6 @@ const EXAMPLE_CONFIG = {
showPrecision: optionalConfigSelectors(true, false),
showSystem: true,
showTitles: optionalConfigSelectors(true, false),
since: VALID_DELTA,
system: { os: 'linux', node_version: 'node_8' },
tasks: optionalArray('./benchmark/tasks.js'),
titles: { taskId: 'taskTitle' },
Expand Down
28 changes: 7 additions & 21 deletions src/history/delta/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,9 @@ import { getDeltaProp, addDeltaError } from './error.js'
import { FORMATS } from './formats/main.js'

// Several configuration properties targets a previous rawResult using a delta,
// which can an integer, date/time/duration, rawResult.id or git reference.
export const normalizeDeltas = function (config) {
return DELTA_PROPS.reduce(normalizeDelta, config)
}

const DELTA_PROPS = ['delta', 'since']

const normalizeDelta = function (config, name) {
const delta = config[name]

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

return { ...config, [name]: normalizeDeltaValue(delta, name) }
}

const normalizeDeltaValue = function (delta, name) {
// which can an integer, "first", date/time/duration, rawResult.id or git
// reference.
export const normalizeDelta = function (delta, name) {
if (delta === '') {
const deltaProp = getDeltaProp(delta, name)
throw new UserError(`${deltaProp} must not be an empty string`)
Expand All @@ -35,17 +20,18 @@ const normalizeDeltaValue = function (delta, name) {
if (deltaReturn === undefined) {
const deltaProp = getDeltaProp(delta, name)
throw new UserError(
`${deltaProp} must be a number, a date, a time, an id or a git commit/tag/branch.`,
`${deltaProp} must be an integer, "first", a date/time/duration, a result id or a git commit/tag/branch.`,
)
}

const [value, { type }] = deltaReturn
const { type, value } = deltaReturn
return { type, value, delta, name }
}

const parseDelta = function ({ format: { parse, type }, delta, name }) {
try {
return parse(delta)
const value = parse(delta)
return value === undefined ? undefined : { type, value }
} catch (error) {
throw addDeltaError(error, { type, delta, name })
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const findValue = function (iterable, func) {

// eslint-disable-next-line max-depth
if (returnValue !== undefined) {
return [returnValue, value]
return returnValue
}
}
}
Expand Down

0 comments on commit 1e0b357

Please sign in to comment.