• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3exports.severityLabel = severityLabel
4exports.color = color
5exports.totalVulnCount = totalVulnCount
6exports.severities = severities
7
8const ccs = require('console-control-strings')
9
10const severityColors = {
11  critical: {
12    color: 'brightMagenta',
13    label: 'Critical'
14  },
15  high: {
16    color: 'brightRed',
17    label: 'High'
18  },
19  moderate: {
20    color: 'brightYellow',
21    label: 'Moderate'
22  },
23  low: {
24    color: 'bold',
25    label: 'Low'
26  },
27  info: {
28    color: '',
29    label: 'Info'
30  }
31}
32
33function color (value, colorName, withColor) {
34  return (colorName && withColor) ? ccs.color(colorName) + value + ccs.color('reset') : value
35}
36
37function severityLabel (sev, withColor, bold) {
38  if (!(sev in severityColors)) return sev.charAt(0).toUpperCase() + sev.substr(1).toLowerCase()
39  let colorName = severityColors[sev].color
40  if (bold) colorName = [colorName, 'bold']
41  return color(severityColors[sev].label, colorName, withColor)
42}
43
44function totalVulnCount (vulns) {
45  return Object.keys(vulns).reduce((accumulator, key) => {
46    const vulnCount = vulns[key]
47    accumulator += vulnCount
48
49    return accumulator
50  }, 0)
51}
52
53function severities (vulns) {
54  return Object.keys(vulns).reduce((accumulator, severity) => {
55    const vulnCount = vulns[severity]
56    if (vulnCount > 0) accumulator.push([severity, vulnCount])
57
58    return accumulator
59  }, [])
60}
61