Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 12, 2021
1 parent f7c2877 commit edc512d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/combination/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { getInputIds } from './inputs.js'
// - are checked for duplicates
// As opposed to non-combination identifiers: inputs.
// The order is significant as it defines:
// - The sorting order in reporters
// - The order when printing combinationName in previews and `dev`
// - The sorting order of combinations in reporters
// - The order of dimensions when printing combinationName in reporters,
// previews and `dev`
export const COMBINATION_DIMENSIONS = [
{
dimension: 'task',
Expand Down
43 changes: 24 additions & 19 deletions src/report/normalize/titles_pad.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
import { COMBINATION_DIMENSIONS } from '../../combination/dimensions.js'

// Add:
// - `combination.task|runner|systemTitlePadded`: like
// `combination.*Title` but padded so all combinations vertically align
// - `combination.titles`: combines all *TitlePadded
export const padTitles = function (combinations) {
const propNames = getUniquePropNames(combinations)
const paddings = getPaddings(combinations, propNames)
const titleNames = getUniqueTitleNames(combinations)
const paddings = getPaddings(combinations, titleNames)
return combinations.map((combination) => addPadding(combination, paddings))
}

const getUniquePropNames = function (combinations) {
const propNames = TITLE_PROPS.filter((propName) =>
shouldShowProp(propName, combinations),
const getUniqueTitleNames = function (combinations) {
const titleNames = COMBINATION_DIMENSIONS.map(getTitleName).filter(
(titleName) => shouldShowProp(titleName, combinations),
)
return propNames.length === 0 ? DEFAULT_TITLE_PROPS : propNames
return titleNames.length === 0 ? DEFAULT_TITLE_NAMES : titleNames
}

const getTitleName = function ({ titleName }) {
return titleName
}

const TITLE_PROPS = ['taskTitle', 'runnerTitle', 'systemTitle']
const DEFAULT_TITLE_PROPS = ['taskTitle']
const DEFAULT_TITLE_NAMES = ['taskTitle']

const shouldShowProp = function (propName, combinations) {
const titles = combinations.map((combination) => combination[propName])
const shouldShowProp = function (titleName, combinations) {
const titles = combinations.map((combination) => combination[titleName])
return [...new Set(titles)].length !== 1
}

const getPaddings = function (combinations, propNames) {
return propNames.map((propName) => getPadding(combinations, propName))
const getPaddings = function (combinations, titleNames) {
return titleNames.map((titleName) => getPadding(combinations, titleName))
}

const getPadding = function (combinations, propName) {
const lengths = combinations.map(({ [propName]: value }) => value.length)
const getPadding = function (combinations, titleName) {
const lengths = combinations.map(({ [titleName]: value }) => value.length)
const padding = Math.max(...lengths)
return { propName, padding }
return { titleName, padding }
}

const addPadding = function (combination, paddings) {
const titles = paddings.map(({ propName, padding }) =>
padProp(combination[propName], padding, propName),
const titles = paddings.map(({ titleName, padding }) =>
padProp(combination[titleName], padding, titleName),
)
const titlesA = Object.fromEntries(titles)
const titlesProp = Object.values(titlesA)
return { ...combination, ...titlesA, titles: titlesProp }
}

const padProp = function (title, padding, propName) {
const padProp = function (title, padding, titleName) {
const titleA = title.padEnd(padding)
return [`${propName}${PADDED_PREFIX}`, titleA]
return [`${titleName}${PADDED_PREFIX}`, titleA]
}

const PADDED_PREFIX = 'Padded'

0 comments on commit edc512d

Please sign in to comment.