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 2ee62d5 commit c3084f7
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/report/reporters/boxplot/box.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
import { goodColor } from '../../utils/colors.js'

// Retrieve the box plot
// eslint-disable-next-line complexity, max-statements
export const getBox = function ({
positions: { min, q1, median, q3, max },
minBlockWidth,
combinationTitles,
mini,
}) {
const leftSpace = getLeftSpace(min, minBlockWidth, mini)
const minPadded = mini ? '' : addStatPadding(min.prettyColor)
const minLine = getMinLine(min, q1)
const q1Box = getQ1Box(q1, median)
const medianCharacter = goodColor(MEDIAN_CHARACTER)
const q3Box = getQ3Box(median, q3)
const maxLine = getMaxLine(q3, max)
const maxPadded = mini ? '' : addStatPadding(max.prettyColor)
return `${combinationTitles}${leftSpace}${minPadded}${minLine}${q1Box}${medianCharacter}${q3Box}${maxLine}${maxPadded}\n`
}

const getLeftSpace = function (min, minBlockWidth, mini) {
const leftSpaceWidth = Math.max(
minBlockWidth + min.index - (mini ? 0 : addStatPadding(min.pretty).length),
0,
)
const leftSpace = ' '.repeat(leftSpaceWidth)
const minPadded = mini ? '' : addStatPadding(min.prettyColor)
return ' '.repeat(leftSpaceWidth)
}

const getMinLine = function (min, q1) {
const minCharacter = min.index === q1.index ? '' : MIN_CHARACTER
const leftLineWidth = Math.max(q1.index - min.index - minCharacter.length, 0)
const leftLine = LINE_CHARACTER.repeat(leftLineWidth)
return `${minCharacter}${leftLine}`
}

const getQ1Box = function (q1, median) {
const q1BoxWidth = Math.max(median.index - q1.index, 0)
const q1Box = BOX_CHARACTER.repeat(q1BoxWidth)
const medianCharacter = goodColor(MEDIAN_CHARACTER)
return BOX_CHARACTER.repeat(q1BoxWidth)
}

const getQ3Box = function (median, q3) {
const q3BoxWidth = Math.max(q3.index - median.index, 0)
const q3Box = BOX_CHARACTER.repeat(q3BoxWidth)
return BOX_CHARACTER.repeat(q3BoxWidth)
}

const getMaxLine = function (q3, max) {
const maxCharacter = q3.index === max.index ? '' : MAX_CHARACTER
const rightLineWidth = Math.max(max.index - q3.index - maxCharacter.length, 0)
const rightLine = LINE_CHARACTER.repeat(rightLineWidth)
const maxPadded = mini ? '' : addStatPadding(max.prettyColor)
return `${combinationTitles}${leftSpace}${minPadded}${minCharacter}${leftLine}${q1Box}${medianCharacter}${q3Box}${rightLine}${maxCharacter}${maxPadded}\n`
return `${maxCharacter}${rightLine}`
}

// Works on most terminals
Expand Down

0 comments on commit c3084f7

Please sign in to comment.