All files parse.js

100% Statements 13/13
100% Branches 16/16
100% Functions 1/1
100% Lines 13/13
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    2x 13x 13x   13x 13x 13x   13x 13x   13x 2x               11x 2x             9x              
import { KEYWORD, getLang } from './constants'
 
const parse = (term) => {
  const words = term.split(' ')
  const [keyword, first, second] = words
 
  const match = keyword === KEYWORD || keyword === `${KEYWORD} `
  const firstLang = getLang(first)
  const secondLang = getLang(second)
 
  const isTwoArguments = firstLang && secondLang ? true : false
  const isOneArgument = !isTwoArguments && firstLang ? true : false
 
  if (isTwoArguments) {
    return {
      match,
      query: words.slice(3).join(' '),
      source: firstLang,
      target: secondLang,
    }
  }
 
  if (isOneArgument) {
    return {
      match,
      query: words.slice(2).join(' '),
      target: firstLang,
    }
  }
 
  return {
    match,
    query: match ? words.slice(1).join(' ') : '',
  }
}
 
export default parse