All files extract-console-logs.ts

97.78% Statements 44/45
94.44% Branches 17/18
100% Functions 7/7
100% Lines 39/39

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 801x 1x 1x               5x 3x   3x   8x   8x           3x     5x     1x 5x   5x 5x   5x 23x     106x     23x   18x 18x 18x 18x   18x 88x 70x       70x 9x 9x   9x   9x 9x     18x   9x 9x 8x   9x 9x     5x   5x    
import {anyFalse, map, remove, take, trim, forEach} from 'rambdax'
const LIMIT = 115
const MARK = 'NIKETA_MARKER'
 
interface Decoration {
  line: string,
  decoration: string,
}
 
function mergeLogs(hash: Record<string, any>) {
  const iterable = (x: Record<string, any>) => {
    const lineDecoration: Decoration[] = []
 
    forEach((xInstance, lineNumber) => {
      const decoration =
        xInstance.length === 1 ? xInstance[0] : xInstance.join(' ')
 
      lineDecoration.push({
        decoration,
        line: lineNumber,
      })
    }, x)
 
    return lineDecoration
  }
 
  return map<any, any>(iterable, hash)
}
 
export function extractConsoleLogs(input: string) {
  const withMarker = input.replace(/console\.log/g, `${MARK} console.log`)
 
  const parts = withMarker.split('console.log')
  const hash: Record<string, any> = {}
 
  const extractor = (part: string) => {
    const partialLines = part
      .split('\n')
      .filter(Boolean)
      .filter(x => !x.includes(MARK))
      .map(trim)
 
    if (partialLines.length === 0) return
 
    const logLines: string[] = []
    let found = false
    let fileName = ''
    let lineNumber = 0
 
    partialLines.forEach(partialLine => {
      if (found) return
      const matched = partialLine.match(
        /\([a-zA-Z_\-\.\/]+:[0-9]+:[0-9]+\)$/
      )
 
      if (matched === null && !found) return logLines.push(partialLine)
      Iif (matched === null) return
      found = true
 
      const [fileNameRaw, lineNumberRaw] = matched[0].split(':')
 
      lineNumber = Number(lineNumberRaw)
      fileName = remove(['(', ')'])(fileNameRaw)
    })
 
    if (anyFalse(found, lineNumber, fileName)) return
 
    if (hash[fileName] === undefined) hash[fileName] = {}
    if (hash[fileName][lineNumber] === undefined)
      hash[fileName][lineNumber] = []
 
    const decoration = take(LIMIT)(logLines.join(' '))
    hash[fileName][lineNumber].push(decoration)
  }
 
  parts.forEach(extractor)
 
  return mergeLogs(hash)
}