Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 29, 2021
1 parent 3380622 commit e743f71
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
50 changes: 4 additions & 46 deletions src/report/finalize.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import stripAnsi from 'strip-ansi'
import stripFinalNewline from 'strip-final-newline'

import { groupBy } from '../utils/group.js'

import { FORMATS } from './formats/list.js'
import { addPadding } from './utils/indent.js'
import { wrapRows } from './utils/wrap.js'
import { handleContent } from './handle.js'

// Remove empty contents, join them by output, fix colors and whitespaces
export const finalizeContents = function (contents) {
const contentsA = contents.filter(hasContent)
const contentsB = Object.values(groupBy(contentsA, 'output')).map(
joinContents,
)
return contentsB
const contentsC = contentsB.map(handleContent)
return contentsC
}

// A reporter can choose not to return anything
Expand All @@ -27,47 +23,9 @@ const joinContents = function (contents) {
const [{ output, format, colors, footerString }] = contents
const content = contents.map(getContentProperty).join('\n')
const contentA = `${content}${footerString}`
const contentB = handleContent(contentA, format, colors)
return { content: contentB, output }
return { content: contentA, output, format, colors }
}

const getContentProperty = function ({ content }) {
return content
}

// Handle content returned by `reporter.report()`.
// We purposely do not remove empty lines before/after the `content` since those
// might be used to avoid vertical jitter when the reporter knows those empty
// lines will be eventually filled (e.g. when combinations stats become
// available).
const handleContent = function (content, format, colors) {
const contentA = handleColors(content, colors)
const contentB = trimEnd(contentA)
const contentC = wrapRows(contentB)
const contentD = padContents(contentC, format)
return contentD
}

// Reporters should always assume `colors` are true, but the core remove this
// from the returned content if not.
const handleColors = function (content, colors) {
return colors ? content : stripAnsi(content)
}

// Trim the end of each line to avoid wrapping-related visual bugs
const trimEnd = function (content) {
return content.split('\n').map(trimEndLine).join('\n')
}

const trimEndLine = function (line) {
return line.trimEnd()
}

const padContents = function (joinedContents, format) {
if (FORMATS[format].padding === undefined) {
return joinedContents
}

const joinedContentsA = `${stripFinalNewline(joinedContents)}\n`
return addPadding(joinedContentsA)
}
43 changes: 43 additions & 0 deletions src/report/handle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import stripAnsi from 'strip-ansi'
import stripFinalNewline from 'strip-final-newline'

import { FORMATS } from './formats/list.js'
import { addPadding } from './utils/indent.js'
import { wrapRows } from './utils/wrap.js'

// Handle content returned by `reporter.report()`.
// We purposely do not remove empty lines before/after the `content` since those
// might be used to avoid vertical jitter when the reporter knows those empty
// lines will be eventually filled (e.g. when combinations stats become
// available).
export const handleContent = function ({ content, output, format, colors }) {
const contentA = handleColors(content, colors)
const contentB = trimEnd(contentA)
const contentC = wrapRows(contentB)
const contentD = padContents(contentC, format)
return { content: contentD, output }
}

// Reporters should always assume `colors` are true, but the core remove this
// from the returned content if not.
const handleColors = function (content, colors) {
return colors ? content : stripAnsi(content)
}

// Trim the end of each line to avoid wrapping-related visual bugs
const trimEnd = function (content) {
return content.split('\n').map(trimEndLine).join('\n')
}

const trimEndLine = function (line) {
return line.trimEnd()
}

const padContents = function (joinedContents, format) {
if (FORMATS[format].padding === undefined) {
return joinedContents
}

const joinedContentsA = `${stripFinalNewline(joinedContents)}\n`
return addPadding(joinedContentsA)
}

0 comments on commit e743f71

Please sign in to comment.