bin/cli.js
function completer (text) {
  const math = getMath()
  let matches = []
  let keyword
  const m = /[a-zA-Z_0-9]+$/.exec(text)
  if (m) {
    keyword = m[0]

    // scope variables
    for (const def in scope) {
      if (scope.hasOwnProperty(def)) {
        if (def.indexOf(keyword) === 0) {
          matches.push(def)
        }
      }
    }

    // commandline keywords
    ['exit', 'quit', 'clear'].forEach(function (cmd) {
      if (cmd.indexOf(keyword) === 0) {
        matches.push(cmd)
      }
    })

    // math functions and constants
    const ignore = ['expr', 'type']
    for (const func in math.expression.mathWithTransform) {
      if (math.expression.mathWithTransform.hasOwnProperty(func)) {
        if (func.indexOf(keyword) === 0 && ignore.indexOf(func) === -1) {
          matches.push(func)
        }
      }
    }

    // units
    const Unit = math.Unit
    for (let name in Unit.UNITS) {
      if (Unit.UNITS.hasOwnProperty(name)) {
        if (name.indexOf(keyword) === 0) {
          matches.push(name)
        }
      }
    }
    for (let name in Unit.PREFIXES) {
      if (Unit.PREFIXES.hasOwnProperty(name)) {
        const prefixes = Unit.PREFIXES[name]
        for (const prefix in prefixes) {
          if (prefixes.hasOwnProperty(prefix)) {
            if (prefix.indexOf(keyword) === 0) {
              matches.push(prefix)
            } else if (keyword.indexOf(prefix) === 0) {
              const unitKeyword = keyword.substring(prefix.length)
              for (const n in Unit.UNITS) {
                if (Unit.UNITS.hasOwnProperty(n)) {
                  if (n.indexOf(unitKeyword) === 0 &&
                      Unit.isValuelessUnit(prefix + n)) {
                    matches.push(prefix + n)
                  }
                }
              }
            }
          }
        }
      }
    }

    // remove duplicates
    matches = matches.filter(function (elem, pos, arr) {
      return arr.indexOf(elem) === pos
    })
  }

  return [matches, keyword]
}
function completer (text) {
  const math = getMath()
  let matches = []
  let keyword
  const m = /[a-zA-Z_0-9]+$/.exec(text)
  if (m) {
    keyword = m[0]

    // scope variables
    for (const def in scope) {
      if (scope.hasOwnProperty(def)) {
        if (def.indexOf(keyword) === 0) {
          matches.push(def)
        }
      }
    }

    // commandline keywords
    ['exit', 'quit', 'clear'].forEach(function (cmd) {
      if (cmd.indexOf(keyword) === 0) {
        matches.push(cmd)
      }
    })

    // math functions and constants
    const ignore = ['expr', 'type']
    for (const func in math.expression.mathWithTransform) {
      if (math.expression.mathWithTransform.hasOwnProperty(func)) {
        if (func.indexOf(keyword) === 0 && ignore.indexOf(func) === -1) {
          matches.push(func)
        }
      }
    }

    // units
    const Unit = math.Unit
    for (let name in Unit.UNITS) {
      if (Unit.UNITS.hasOwnProperty(name)) {
        if (name.indexOf(keyword) === 0) {
          matches.push(name)
        }
      }
    }
    for (let name in Unit.PREFIXES) {
      if (Unit.PREFIXES.hasOwnProperty(name)) {
        const prefixes = Unit.PREFIXES[name]
        for (const prefix in prefixes) {
          if (prefixes.hasOwnProperty(prefix)) {
            if (prefix.indexOf(keyword) === 0) {
              matches.push(prefix)
            } else if (keyword.indexOf(prefix) === 0) {
              const unitKeyword = keyword.substring(prefix.length)
              for (const n in Unit.UNITS) {
                if (Unit.UNITS.hasOwnProperty(n)) {
                  if (n.indexOf(unitKeyword) === 0 &&
                      Unit.isValuelessUnit(prefix + n)) {
                    matches.push(prefix + n)
                  }
                }
              }
            }
          }
        }
      }
    }

    // remove duplicates
    matches = matches.filter(function (elem, pos, arr) {
      return arr.indexOf(elem) === pos
    })
  }

  return [matches, keyword]
}
function completer ( text ) { const math = getMath ( ) let matches = [ ] let keyword const m = / [a-zA-Z_0-9]+$ / . exec ( text ) if ( m ) { keyword = m [ 0 ] // scope variables for ( const def in scope ) { if ( scope . hasOwnProperty ( def ) ) { if ( def . indexOf ( keyword ) === 0 ) { matches . push ( def ) } } } // commandline keywords [ 'exit' , 'quit' , 'clear' ] . forEach ( function ( cmd ) { if ( cmd . indexOf ( keyword ) === 0 ) { matches . push ( cmd ) } } ) // math functions and constants const ignore = [ 'expr' , 'type' ] for ( const func in math . expression . mathWithTransform ) { if ( math . expression . mathWithTransform . hasOwnProperty ( func ) ) { if ( func . indexOf ( keyword ) === 0 && ignore . indexOf ( func ) === - 1 ) { matches . push ( func ) } } } // units const Unit = math . Unit for ( let name in Unit . UNITS ) { if ( Unit . UNITS . hasOwnProperty ( name ) ) { if ( name . indexOf ( keyword ) === 0 ) { matches . push ( name ) } } } for ( let name in Unit . PREFIXES ) { if ( Unit . PREFIXES . hasOwnProperty ( name ) ) { const prefixes = Unit . PREFIXES [ name ] for ( const prefix in prefixes ) { if ( prefixes . hasOwnProperty ( prefix ) ) { if ( prefix . indexOf ( keyword ) === 0 ) { matches . push ( prefix ) } else if ( keyword . indexOf ( prefix ) === 0 ) { const unitKeyword = keyword . substring ( prefix . length ) for ( const n in Unit . UNITS ) { if ( Unit . UNITS . hasOwnProperty ( n ) ) { if ( n . indexOf ( unitKeyword ) === 0 && Unit . isValuelessUnit ( prefix + n ) ) { matches . push ( prefix + n ) } } } } } } } } // remove duplicates matches = matches . filter ( function ( elem , pos , arr ) { return arr . indexOf ( elem ) === pos } ) } return [ matches , keyword ] }
auto complete a text
@param {String} text
@return {[Array, String]} completions
auto complete a text