All files per-commit.js

100% Statements 27/27
100% Branches 2/2
100% Functions 12/12
100% Lines 24/24

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 104 105 106 107                                          906x             5x 138x   180x     138x                   5x 150x   900x                     5x 147x                       5x                   5x   147x   147x 1028x 1028x   1028x 147x                 5x 143x 143x 143x 143x 143x    
import {
  curry,
  merge,
  fromPairs,
  entries,
  pipe,
  keys,
  reduce,
  map
} from "f-utility"
import uniq from "array-union"
import time from "moment-timezone"
import { aliasProperty } from "./utils"
import { anyFilesMatchFromObject } from "./filters"
import { getCanon } from "./alias"
 
/**
@method grabAfterLastDot
@param {Array} strings - an array of strings
@return {Array} a list of potentially truncated strings
*/
const grabAfterLastDot = map(str => str.substr(str.lastIndexOf(`.`) + 1))
 
/**
@method filetypes
@param {Object} changes - a changes object, generated during analysis
@return {Array} an array of filetypes
*/
export const filetypes = changes =>
  pipe(
    keys,
    reduce((list, key) => list.concat(grabAfterLastDot(changes[key])), []),
    uniq,
    // eslint-disable-next-line fp/no-mutating-methods
    x => x.sort()
  )(changes)
 
/**
@method generateAnalysis
@param {Object} lookup - the legend object
@param {Object} commit - an object with changes
@param {Object} commit.changes - a changes object
@returns {Object} a commit object with an analysis property
*/
export const generateAnalysis = curry((lookup, { changes }) =>
  pipe(
    entries,
    map(([k, { matches }]) => [k, anyFilesMatchFromObject(changes, matches)]),
    fromPairs
  )(lookup)
)
 
/**
@method addAnalysisPerCommit
@param {Object} lookup - the legend object
@param {Object} raw - the commit object
@return {Object} a standardized "commit" object
*/
export const addAnalysisPerCommit = curry((lookup, raw) =>
  merge(raw, {
    type: `commit`,
    author: getCanon(raw.author),
    analysis: generateAnalysis(lookup, raw)
  })
)
 
/**
@method addAliasesPerCommit
@param {Object} commit - a commit object
@return {Object} a commit with some aliased properties
*/
export const addAliasesPerCommit = pipe(
  aliasProperty(`authorName`, `author`),
  aliasProperty(`abbrevHash`, `hash`)
)
 
/**
@method convertStatusAndFilesPerCommit
@param {Object} commit - a commit object
@return {Object} a "changes" object
*/
export const convertStatusAndFilesPerCommit = commit => {
  /* eslint-disable require-jsdoc */
  const { files } = commit
  // TODO: rewrite this so we don't have to rely on the i to count
  const arrayify = x => (file, i) => {
    const status = x.status[i]
    return [status, file]
  }
  const flattenArrays = (a, [k, v]) => merge(a, { [k]: (a[k] || []).concat(v) })
  return files.map(arrayify(commit)).reduce(flattenArrays, {})
  /* eslint-enable require-jsdoc */
}
 
/**
@method addTimestampPerCommit
@param {Object} commit - a commit object
@return {Object} a commit object with a timestamp property
*/
export const addTimestampPerCommit = curry((timezone, commit) => {
  const { authorDate } = commit
  const rel = time(authorDate, `YYYY-MM-DD HH:mm:ss Z`)
  const ms = rel.valueOf()
  const date = rel.tz(timezone).format(`DD-MM-YYYY`)
  return merge(commit, { ms, date })
})