All files / lexers line.js

100% Statements 30/30
100% Branches 10/10
100% Functions 2/2
100% Lines 27/27

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 451x     200x 200x 200x 200x   200x 200x   200x 200x   200x   200x     200x 6565x 6565x   6565x 930x 930x     6565x   6565x 930x 930x 930x 930x   930x 930x 930x       200x    
module.exports = {
  name: 'line',
  desc: 'treats lines as tokens.',
  func: ({verbose}) => (data, linesOffset) => {
    const tokens = []
    const lines  = []
    const err    = []
  
    let text     = data
    let len      = text.length
  
    let at       = -1
    let lastLine = linesOffset
    
    let obj      = false
  
    let done     = false
    let ch
    
    do {
      at++
      ch = text.charAt(at)
  
      if (ch === '\n') {
        if (verbose) lastLine++
        obj = true
      }
  
      if (at === len) done = true
  
      if (obj) {
        obj = false
        const token = text.slice(0, at)
        tokens.push(token)
        if (verbose) lines.push(lastLine)
  
        text = text.slice(at + 1, len)
        len = text.length
        at = -1
      }
    } while (!done)
  
    return {err, tokens, lines, lastLine, rest: text}
  }
}