All files / src/repl/completer index.js

62.5% Statements 10/16
0% Branches 0/7
60% Functions 3/5
62.5% Lines 10/16

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 361x 1x   1x 1x   1x 1x   1x   1x       1x         1x                              
const {flatMap}    = require('./flatMap')
const {getMatches} = require('./getMatches')
 
const completer = (lexer, cmd, {only = false} = {only: false}) => {
  const lex = lexer(cmd)
 
  return line => {
    const {opts} = lex(line)
 
    const values = justValues(opts)
 
    return getMatches(line, values, cmd, {only})
  }
}
 
module.exports = {
  completer
}
 
function justValues (opts) {
  return flatMap(opts, opt => {
    if (Array.isArray(opt.values)) {
      if (isSubcommand(opt)) {
        return [{...opt, values: justValues(opt.values)}]
      } else {
        return [opt]
      }
    } else {
      return []
    }
  })
}
 
function isSubcommand ({opts} = {opts: undefined}) {
  return Array.isArray(opts)
}