1 | /**
|
---|
2 | * @fileoverview Helpers for severity values (e.g. normalizing different types).
|
---|
3 | * @author Bryan Mishkin
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Convert severity value of different types to a string.
|
---|
10 | * @param {string|number} severity severity value
|
---|
11 | * @throws error if severity is invalid
|
---|
12 | * @returns {string} severity string
|
---|
13 | */
|
---|
14 | function normalizeSeverityToString(severity) {
|
---|
15 | if ([2, "2", "error"].includes(severity)) {
|
---|
16 | return "error";
|
---|
17 | }
|
---|
18 | if ([1, "1", "warn"].includes(severity)) {
|
---|
19 | return "warn";
|
---|
20 | }
|
---|
21 | if ([0, "0", "off"].includes(severity)) {
|
---|
22 | return "off";
|
---|
23 | }
|
---|
24 | throw new Error(`Invalid severity value: ${severity}`);
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Convert severity value of different types to a number.
|
---|
29 | * @param {string|number} severity severity value
|
---|
30 | * @throws error if severity is invalid
|
---|
31 | * @returns {number} severity number
|
---|
32 | */
|
---|
33 | function normalizeSeverityToNumber(severity) {
|
---|
34 | if ([2, "2", "error"].includes(severity)) {
|
---|
35 | return 2;
|
---|
36 | }
|
---|
37 | if ([1, "1", "warn"].includes(severity)) {
|
---|
38 | return 1;
|
---|
39 | }
|
---|
40 | if ([0, "0", "off"].includes(severity)) {
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 | throw new Error(`Invalid severity value: ${severity}`);
|
---|
44 | }
|
---|
45 |
|
---|
46 | module.exports = {
|
---|
47 | normalizeSeverityToString,
|
---|
48 | normalizeSeverityToNumber
|
---|
49 | };
|
---|