All files utils.js

100% Statements 66/66
100% Branches 35/35
100% Functions 35/35
100% Lines 55/55

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                                              7x 4x     7x 7x           7x           3529x             7x 139x                           7x   288x                   7x                 7x 152x 152x         7x 677x 677x                         7x   166x   656x         7x   1328x 7x   7x 340x         7x   170x 7x   7x 1x   7x 385x 385x 385x     51x   7x 1x   9x   1x               341x               7x   4x 4x       4x                   7x           7x   7x 22x     7x       5x 12x 5x 5x       15x       5x          
import fs from "fs"
import groupArrayBy from "group-array-by"
import chalk from "chalk"
 
import {
  isFunction,
  I,
  merge,
  curry,
  padEnd,
  toPairs,
  prop,
  ap,
  join,
  map,
  filter,
  reduce,
  pipe,
  entries
} from "f-utility"
// import { trace } from "xtrace"
import Future from "fluture"
 
export const unaryCallbackToFuture = curry(
  (fn, x) => new Future((rej, res) => fn(x, (e, d) => (e ? rej(e) : res(d))))
)
 
export const log = console.log // eslint-disable-line no-console
export const warn = console.warn // eslint-disable-line no-console
/**
@method box
@param {*} x - anything
@return {Array} something in an array
*/
export const box = x => (Array.isArray(x) ? x : [x])
/**
@method neue
@param {Object|Array} x - something that might be an array or an object
@return {Object|Array} a cloned copy of said array or object
*/
export const neue = x => (x && Array.isArray(x) ? [].concat(x) : merge({}, x))
 
/**
@method summarize
@param {number} limit - the max length of the string, + 3
@return {string} a summarized string, within the goal limits + 3
*/
export const summarize = curry((str, limit) =>
  padEnd(
    limit + 3,
    ` `,
    str.substr(0, limit) + `${str.length > limit ? `...` : ``}`
  )
)
 
/**
@method aliasProperty
@param {string} property - a string property key
@param {string} propAlias - a string property key alias
@param {Object} x - some object
@return {Object} object with aliased property, or no change
*/
export const aliasProperty = curry(
  (property, propAlias, x) =>
    typeof x[property] !== `undefined`
      ? merge(x, { [propAlias]: x[property] })
      : neue(x)
)
 
/**
@method j2
@param {*} x - anything
@return {string} json stringified stuff with a 2 space indent
*/
export const j2 = x => JSON.stringify(x, null, 2)
 
/**
@method macroLens
@param {Function} fn - a macroLens function (copy, property) => {}
@param {string} prop - a property to macroLens
@param {Object} target - an object which has a property to macroLens
@return {Object} a cloned object with a modified property
*/
export const macroLens = curry((fn, property, target) => {
  const copy = neue(target)
  return copy && property
    ? merge(copy, { [property]: fn(copy, copy[property]) })
    : copy
})
 
export const lens = curry((fn, property, target) => {
  const copy = neue(target)
  return copy && property
    ? neue(merge(copy, { [property]: fn(copy[property]) }))
    : copy
})
 
/**
@method sortByKeyWithWrapper
@param {boolean} ascendingSort - sort ascending?
@param {Function} wrap - a function to wrap the comparitors with
@param {string} key - a key which is shared across both comparitors
@param {Array} arr - an array to be sorted
@return {Array} a sorted array
*/
export const sortByKeyWithWrapper = curry((ascendingSort, wrap, key, arr) =>
  // eslint-disable-next-line fp/no-mutating-methods
  neue(arr).sort(
    ({ [key]: a }, { [key]: b }) =>
      ascendingSort ? wrap(b) - wrap(a) : wrap(a) - wrap(b)
  )
)
 
// eslint-disable-next-line fp/no-mutating-methods
export const sortByDate = sortByKeyWithWrapper(true, I, `ms`)
// eslint-disable-next-line require-jsdoc
const tomorrow = x => new Date(x)
export const sortByDateKey = sortByKeyWithWrapper(true, tomorrow)
// eslint-disable-next-line require-jsdoc
const prettyPrintDate = x =>
  tomorrow(
    x.split(`-`).reverse() // eslint-disable-line fp/no-mutating-methods
  )
 
// eslint-disable-next-line require-jsdoc
export const sortByDateObject = k =>
  // eslint-disable-next-line fp/no-mutating-methods
  k.sort((a, b) => prettyPrintDate(b) - prettyPrintDate(a))
export const sortByAuthorDate = sortByDateKey(`authorDate`)
 
export const binaryCallback = curry((orig, cb, output, data) => {
  orig(output, data, cb)
})
export const preferredProp = curry((a, b, def, key) => {
  const _a = prop(key, neue(a))
  const _b = prop(key, neue(b))
  return _a || _b || def
})
// eslint-disable-next-line require-jsdoc
export const stripDoubleBackslash = w => w.replace(/^\\/g, ``)
 
export const processKeysByLookup = curry((fnLookup, o) =>
  pipe(
    entries,
    map(([k, v]) => ([k1, v1]) => (k1 === k ? [k, v(v1)] : null)),
    table =>
      pipe(
        entries,
        ap(table),
        filter(I)
      )(o)
  )(fnLookup)
)
 
export const indexAny = curry((look, str) => str.indexOf(look) > -1)
 
/**
@method fileAccess
@param {boolean} returnFalse - return false?
@param {string} file - a file reference
@returns {Future} some future
*/
export const fileAccess = curry(
  (returnFalse, file) =>
    new Future((rej, res) =>
      fs.access(
        file,
        /* ------------------ */
        fs.constants.F_OK,
        e => (e ? rej(returnFalse ? false : e) : res(true))
      )
    )
)
 
/**
@method fileOrError
@param {string} file - a file reference
@returns {Future} true or an error
*/
export const fileOrError = fileAccess(false)
/**
@method fileExists
@param {string} file - a file reference
@returns {Future} eventual boolean value
*/
export const fileExists = fileAccess(true)
 
export const groupBy = curry((fn, iter) =>
  groupArrayBy(isFunction(fn) ? fn : prop(fn), iter)
)
 
export const flagify = pipe(
  toPairs,
  map(
    pipe(
      ([x, y]) => box(x).concat(y),
      x => x.sort((a, b) => a.length - b.length), // eslint-disable-line fp/no-mutating-methods
      x => [x[0], x],
      ([key, flags]) => [
        key,
        map(
          x =>
            x.length === 1 ? `-${chalk.yellow(x)}` : `--${chalk.yellow(x)}`,
          flags
        )
      ],
      ([k, v]) => ({ [k]: join(` / `, v) })
    )
  ),
  reduce(merge, {})
)