diff --git a/src/humanizer.ts b/src/humanizer.ts index b36510c..c9989d8 100644 --- a/src/humanizer.ts +++ b/src/humanizer.ts @@ -49,3 +49,18 @@ export const humanizeBaseMetricValue = ( return 'Unknown'; } }; + +/** + * Stringify a score into a qualitative severity rating string + * @param score + */ +export const humanizeScore = (score: number): string => + score <= 0 + ? 'None' + : score <= 3.9 + ? 'Low' + : score <= 6.9 + ? 'Medium' + : score <= 8.9 + ? 'High' + : 'Critical'; diff --git a/tests/humanizer.spec.ts b/tests/humanizer.spec.ts index 907a977..344fed7 100644 --- a/tests/humanizer.spec.ts +++ b/tests/humanizer.spec.ts @@ -2,7 +2,8 @@ import { BaseMetric, BaseMetricValue, humanizeBaseMetric, - humanizeBaseMetricValue + humanizeBaseMetricValue, + humanizeScore } from '../src'; import { expect } from 'chai'; @@ -52,4 +53,32 @@ describe('humanizer', () => { ); expect(result).to.equal('Unknown'); }); + + it('should humanize score as "None" for zero value', () => { + expect(humanizeScore(0)).to.equal('None'); + }); + + it('should humanize score as "Low" for values from interval [0.1, 3.9]', () => { + expect(humanizeScore(0.1)).to.equal('Low'); + expect(humanizeScore(1.2)).to.equal('Low'); + expect(humanizeScore(3.9)).to.equal('Low'); + }); + + it('should humanize score as "Medium" for values from interval [4.0, 6.9]', () => { + expect(humanizeScore(4.0)).to.equal('Medium'); + expect(humanizeScore(4.2)).to.equal('Medium'); + expect(humanizeScore(6.9)).to.equal('Medium'); + }); + + it('should humanize score as "High" for values from interval [7.0, 8.9]', () => { + expect(humanizeScore(7.0)).to.equal('High'); + expect(humanizeScore(7.5)).to.equal('High'); + expect(humanizeScore(8.9)).to.equal('High'); + }); + + it('should humanize score as "Critical" for values from interval [9.0, 10.0]', () => { + expect(humanizeScore(9.0)).to.equal('Critical'); + expect(humanizeScore(9.5)).to.equal('Critical'); + expect(humanizeScore(10.0)).to.equal('Critical'); + }); });