Skip to content

Commit

Permalink
feat: add score humanizer (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lutie authored Feb 24, 2021
1 parent 0d30fe0 commit 8309643
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/humanizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
31 changes: 30 additions & 1 deletion tests/humanizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
BaseMetric,
BaseMetricValue,
humanizeBaseMetric,
humanizeBaseMetricValue
humanizeBaseMetricValue,
humanizeScore
} from '../src';
import { expect } from 'chai';

Expand Down Expand Up @@ -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');
});
});

0 comments on commit 8309643

Please sign in to comment.