All files filters.js

100% Statements 26/26
100% Branches 23/23
100% Functions 7/7
100% Lines 26/26

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                                  6x               6x 1057x                 6x 902x   1214x     6x           6x 143x   6x 245x 2x   245x 245x 197x   197x 31x     166x 31x     135x 65x   70x                 6x             7x 237x         7x    
import {
  isArray,
  curry,
  pipe,
  entries,
  reduce,
  pathOr,
  split,
  map,
  filter,
  every,
  path
} from "f-utility"
import mm from "micromatch"
import { neue, indexAny } from "./utils"
import { getCanon } from "./alias"
import { CHARACTER_LITERALS } from "./constants"
const { ASTERISK, TILDE } = CHARACTER_LITERALS
 
/**
@method matchesWildcards
@param {Array} wildcards - a list of wildcards to match against
@param {Array} list - a list of files to compare against
@return {boolean} whether any of the files match any of the wildcards
*/
export const matchesWildcards = curry((wildcards, list) =>
  mm.some(list, neue(wildcards))
)
 
/**
@method anyFilesMatchFromObject
@param {Object} changes - an object whose keys are lists of files
@param {Array} wildcards - a list of wildcards
@return {boolean} whether any keys on the changes object match any of the wildcards
*/
export const anyFilesMatchFromObject = curry((changes, wildcards) =>
  pipe(
    entries,
    reduce((agg, [, v]) => agg || matchesWildcards(wildcards, v), false)
  )(changes)
)
const MERGE_WORD = `Merge `
/**
@method isAMergeCommit
@param {Object} x - an object with an optional subject
@returns {boolean} whether the given object's subject starts with 'Merge '
*/
export const isAMergeCommit = x =>
  pathOr(``, [`subject`], x).substr(0, 6) === MERGE_WORD
 
export const stringMatcher = curry((commit, [k, v]) => {
  if (v === `true` || v === `false`) {
    v = !!v // eslint-disable-line fp/no-mutation
  }
  const dotted = indexAny(`.`, k)
  if (dotted || (commit && commit[k])) {
    const value = dotted ? path(k.split(`.`), commit) : commit[k]
    // authors are magical
    if (k === `author` || k === `authorName`) {
      return getCanon(value) === getCanon(v)
    }
    // asterisks turn on minimatch mode for arrays
    if (isArray(value) && indexAny(ASTERISK, v)) {
      return mm.some(value, v)
    }
    // a ~ suffix will do looser matching
    if (TILDE.test(v)) {
      return indexAny(v.replace(TILDE, ``), value.toLowerCase())
    }
    return value === v
  }
})
/**
 @method filterByStringPattern
 @param {string} f - colon and hash character delimited string (e.g. 'a:1#b:2')
 @param {Array} commits - an array of commits
 @return {Array} a potentially filtered array of commits
 */
export const filterByStringPattern = curry((filterPattern, commits) => {
  /**
   @method matcher
   @private
   @param {Object} commit - a commit object
   @return {boolean} whether there's a match
   */
  const matcher = commit =>
    pipe(
      split(`#`),
      map(split(`:`)),
      every(stringMatcher(commit))
    )(filterPattern)
  return filter(matcher, commits)
})