Skip to content

percentageToTier

Maps a numeric percentage to a tier (icon, color, label) using configurable thresholds.

Tiers are matched by their highest min that is <= value; a value below every tier’s min (e.g. a negative percentage, or custom tiers that don’t cover down to 0) falls back to the tier with the lowest min — there’s always a match as long as tiers is non-empty.

Available since v3.0.1

import { percentageToTier } from '@helpers4/ci';
// or, from the all-in-one package (same code, one install):
import { percentageToTier } from 'helpers4/ci';
percentageToTier(value: number, tiers: readonly PercentageTier[]): PercentageTier
ParameterTypeDescription
valuenumberThe percentage to classify (typically 0-100, but not clamped)
tiersreadonly PercentageTier[]Threshold tiers to match against, in any order (defaults to DEFAULT_PERCENTAGE_TIERS)

PercentageTier — The matched tier

Maps a coverage percentage to a tier using the built-in default thresholds.

percentageToTier(95.2)
// => { min: 90, icon: '🟢', color: 'green', label: 'excellent' }

percentageToTier(42)
// => { min: 0, icon: '🔴', color: 'red', label: 'poor' }

Supply your own tiers, e.g. a simple two-tier pass/fail gate.

const tiers = [
  { min: 50, icon: '🟢', color: 'green', label: 'pass' },
  { min: 0, icon: '🔴', color: 'red', label: 'fail' },
];

percentageToTier(75, tiers)  // => { min: 50, icon: '🟢', color: 'green', label: 'pass' }
percentageToTier(25, tiers)  // => { min: 0, icon: '🔴', color: 'red', label: 'fail' }

A percentage threshold and the icon/color/label to use once a value reaches it.

interface PercentageTier {
  color: string;
  icon: string;
  label: string;
  min: number;
}

View source on GitHub