Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 5, 2021
1 parent f9e4ddb commit 5432ffc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/report/reporters/boxplot/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ const MEDIAN_CHARACTER = '\u2588'
const MAX_CHARACTER = '\u2524'

// Surround stats with padding
export const getPaddedStat = function (string, mini) {
const getPaddedStat = function (string, mini) {
return mini ? '' : `${STAT_PADDING}${string}${STAT_PADDING}`
}

export const getPaddedStatLength = function (string) {
return string.length + STAT_PADDING_WIDTH * 2
}

const STAT_PADDING_WIDTH = 1
const STAT_PADDING = ' '.repeat(STAT_PADDING_WIDTH)
24 changes: 15 additions & 9 deletions src/report/reporters/boxplot/width.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { getCombinationPaddedName } from '../../utils/name.js'

import { getPaddedStat } from './box.js'
import { getPaddedStatLength } from './box.js'

// Retrieve the width of all blocks, in order:
// - Combination titles
// - Padding space reserved for the leftmost `min`
// - Content, i.e. box plot and their labels
// - Padding space reserved for the rightmost `max`
// Compute the width of each column for reporters which show the following ones
// for each combination:
// - Title
// - Min
// - Main content
// - Max
// This is used for example by the `histogram` and `boxplot` reporters.
export const getWidths = function (combinations, mini, screenWidth) {
const titlesWidth = getCombinationPaddedName(combinations[0]).length
const minBlockWidth = getMinMaxBlockWidth(combinations, mini, 'min')
Expand All @@ -19,15 +21,19 @@ export const getWidths = function (combinations, mini, screenWidth) {
}

const getMinMaxBlockWidth = function (combinations, mini, statName) {
if (mini) {
return 0
}

return Math.max(
...combinations.map((combination) =>
getSingleMinMaxWidth(combination, mini, statName),
getSingleMinMaxWidth(combination, statName),
),
)
}

const getSingleMinMaxWidth = function ({ quantiles }, mini, statName) {
const getSingleMinMaxWidth = function ({ quantiles }, statName) {
return quantiles === undefined
? 0
: getPaddedStat(quantiles[statName].pretty, mini).length
: getPaddedStatLength(quantiles[statName].pretty)
}
22 changes: 14 additions & 8 deletions src/report/reporters/histogram/width.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { getCombinationPaddedName } from '../../utils/name.js'

import { getPaddedStatLength } from './abscissa.js'

// Retrieve the width of all blocks, in order:
// - Combination titles
// - Padding space reserved for the leftmost `min`
// - Content, i.e. box plot and their labels
// - Padding space reserved for the rightmost `max`
// Compute the width of each columns for reporters which show the following ones
// for each combination:
// - Title
// - Min
// - Main content
// - Max
// This is used for example by the `histogram` and `boxplot` reporters.
export const getWidths = function (combinations, mini, screenWidth) {
const titlesWidth = getCombinationPaddedName(combinations[0]).length
const minBlockWidth = getMinMaxBlockWidth(combinations, mini, 'min')
Expand All @@ -19,15 +21,19 @@ export const getWidths = function (combinations, mini, screenWidth) {
}

const getMinMaxBlockWidth = function (combinations, mini, statName) {
if (mini) {
return 0
}

return Math.max(
...combinations.map((combination) =>
getSingleMinMaxWidth(combination, mini, statName),
getSingleMinMaxWidth(combination, statName),
),
)
}

const getSingleMinMaxWidth = function ({ stats }, mini, statName) {
return mini || stats[statName] === undefined
const getSingleMinMaxWidth = function ({ stats }, statName) {
return stats[statName] === undefined
? 0
: getPaddedStatLength(stats[statName])
}

0 comments on commit 5432ffc

Please sign in to comment.