All files print.js

100% Statements 32/32
100% Branches 4/4
100% Functions 12/12
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                              4x   41x 178x   4x   820x 820x     4x 136x             4x 41x 41x 41x 41x         4x 137x           4x         545x   4x       4x 136x             4x 136x             4x         4x 135x 135x             135x       4x 173x            
import chalk from "chalk"
import { pipe, keys, map, join, curry } from "f-utility"
import pad from "pad"
// import { sideEffect, trace } from "xtrace"
import {
  CHARACTER_LITERALS,
  SUBJECT_LENGTH,
  AUTHOR_LENGTH,
  BANNER_LENGTH,
  BANNER_INDENT
} from "./constants"
import { filetypes } from "./per-commit"
import { summarize, preferredProp, lens } from "./utils"
import { getCanon } from "./alias"
 
const { BLANK } = CHARACTER_LITERALS
 
const padCharsStart = curry((length, str) => pad(length, str, BLANK))
const padCharsEnd = curry((length, str) => pad(str, length, BLANK))
 
export const drawToken = curry((lookup, analysis, name) => {
  // cold medina
  const { fn, key } = lookup[name]
  return analysis[name] ? chalk.black(fn(` ${key} `)) : `   `
})
 
export const drawTokens = curry((lookup, analysis) =>
  pipe(
    keys,
    map(drawToken(lookup, analysis)),
    join(``)
  )(lookup)
)
 
export const configureAndPrintBanner = curry((lookup, config, { date }) => {
  const grab = preferredProp(config, lookup)
  const bannerIndent = grab(BANNER_INDENT, `bannerIndent`)
  const bannerLength = grab(BANNER_LENGTH, `bannerLength`)
  return chalk.inverse(
    padCharsEnd(bannerLength, padCharsStart(bannerIndent, date))
  )
})
 
export const printAuthor = curry((author, length) =>
  pipe(
    getCanon,
    padCharsEnd(length),
    chalk.red
  )(author)
)
const printTypes = pipe(
  filetypes,
  join(BLANK)
)
 
const prepend = curry((str, str2) => [str, str2].join(BLANK))
 
export const formatHash = pipe(
  prepend(`=`),
  chalk.yellow
)
export const formatAuthor = curry((grab, author) =>
  pipe(
    grab(AUTHOR_LENGTH),
    printAuthor(author),
    prepend(`$`)
  )(`authorLength`)
)
 
export const formatSubject = curry((grab, subject) =>
  pipe(
    grab(SUBJECT_LENGTH),
    summarize(subject),
    prepend(`-`)
  )(`subjectLength`)
)
 
export const formatChanges = pipe(
  printTypes,
  prepend(`|`)
)
 
export const configureAndPrintCommit = curry((lookup, config, o) => {
  const grab = preferredProp(config, lookup)
  return pipe(
    lens(drawTokens(lookup), `analysis`),
    lens(formatHash, `hash`),
    lens(formatAuthor(grab), `author`),
    lens(formatSubject(grab), `subject`),
    lens(formatChanges, `changes`),
    ({ analysis, hash, subject, author, changes }) =>
      [analysis, hash, subject, author, changes].join(BLANK)
  )(o)
})
 
export const colorize = curry((config, lookup, raw) =>
  (raw.type === `banner` ? configureAndPrintBanner : configureAndPrintCommit)(
    lookup,
    config,
    raw
  )
)