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 e5d07ea commit e263d44
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
26 changes: 14 additions & 12 deletions src/report/reporters/histogram/abscissa.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@ import { TICK_MIDDLE, HORIZONTAL_LINE } from './characters.js'

// Retrieve the horizontal line and the abscissa below the main content.
// Includes the tick above the median and its label.
export const getAbscissa = function (width, position) {
const bottomLine = getBottomLine(width, position)
const labels = getLabels(width, position)
export const getAbscissa = function (width, median, medianIndex) {
const bottomLine = getBottomLine(width, medianIndex)
const labels = getLabels(width, median, medianIndex)
return `${separatorColor(bottomLine)}
${labels}`
}

const getBottomLine = function (width, position) {
return addTick(width, '', position).padEnd(width, HORIZONTAL_LINE)
const getBottomLine = function (width, medianIndex) {
return addTick(width, '', medianIndex).padEnd(width, HORIZONTAL_LINE)
}

const addTick = function (width, bottomLine, { index }) {
const lineWidth = index - bottomLine.length
const addTick = function (width, bottomLine, medianIndex) {
const lineWidth = medianIndex - bottomLine.length
return lineWidth < 0
? bottomLine
: `${bottomLine}${HORIZONTAL_LINE.repeat(lineWidth)}${TICK_MIDDLE}`
}

const getLabels = function (width, position) {
const getLabels = function (width, median, medianIndex) {
const { labels, labelsLength } = addLabel(
width,
{ labelsLength: 0, labels: '' },
position,
median,
medianIndex,
)
const labelsA = trimWidth(labels, labelsLength, width)
return labelsA
Expand All @@ -38,12 +39,13 @@ const getLabels = function (width, position) {
const addLabel = function (
width,
{ labelsLength, labels },
{ index, pretty, prettyColor },
{ pretty, prettyColor },
medianIndex,
) {
const spacesWidth =
labelsLength === 0
? Math.max(index - pretty.length + 1, 0)
: Math.max(index - labelsLength, 1)
? Math.max(medianIndex - pretty.length + 1, 0)
: Math.max(medianIndex - labelsLength, 1)
const labelsLengthA = labelsLength + spacesWidth + pretty.length
const spaces = ' '.repeat(spacesWidth)
const labelsA = `${labels}${spaces}${prettyColor}`
Expand Down
18 changes: 9 additions & 9 deletions src/report/reporters/histogram/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { getHistogramRows } from './rows.js'

// Retrieve histogram main content
export const getContent = function ({
stats,
stats: { histogram },
stats: { histogram, median, min, max },
height,
width,
mini,
}) {
const { position, medianIndex, medianMaxWidth } = getMedianPositions(
stats,
const { medianIndex, medianMaxWidth } = getMedianPositions({
median,
min,
max,
width,
)
})
const rows = getHistogramRows({
histogram,
width,
Expand All @@ -25,7 +26,7 @@ export const getContent = function ({
return rows
}

const abscissa = getAbscissa(width, position)
const abscissa = getAbscissa(width, median, medianIndex)
return `${rows}
${abscissa}`
}
Expand All @@ -34,11 +35,10 @@ ${abscissa}`
// When `histogram` has a single item, it is in the first bucket.
// Also compute the maximum width between the median and either the start or end
// Also computes `medianIndex|medianMaxWidth` used for the color gradient.
const getMedianPositions = function ({ median, min, max }, width) {
const getMedianPositions = function ({ median, min, max, width }) {
const medianPercentage =
max.raw === min.raw ? 0 : (median.raw - min.raw) / (max.raw - min.raw)
const medianIndex = Math.round((width - 1) * medianPercentage)
const position = { ...median, index: medianIndex }
const medianMaxWidth = Math.max(medianIndex, width - 1 - medianIndex)
return { position, medianIndex, medianMaxWidth }
return { medianIndex, medianMaxWidth }
}

0 comments on commit e263d44

Please sign in to comment.