UNPKG

@spaced-out/ui-design-system

Version:
39 lines (38 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLabelByScorePercentage = exports.getColorByScorePercentage = exports.SCORE_BAR_ERRORS = void 0; const getValidRange = (score, totalBars, keys) => { const scorePercentage = 100 * score / totalBars; for (const key of keys) { if (scorePercentage <= Number(key)) { return key; } } return [...keys].pop() || ''; }; const getColorByScorePercentage = (totalBars, score, currentBarNumber, colorMap) => { if (score <= 0) { return colorMap.inactive; } const validRange = getValidRange(score, totalBars, Object.keys(colorMap)); const validColor = colorMap[validRange]; return currentBarNumber < score ? validColor : colorMap.inactive; }; exports.getColorByScorePercentage = getColorByScorePercentage; const getLabelByScorePercentage = (score, totalBars, labelMap) => { if (!labelMap) { return ''; } const validRange = getValidRange(score, totalBars, Object.keys(labelMap)); const validLabel = labelMap[validRange]; return validLabel || ''; }; exports.getLabelByScorePercentage = getLabelByScorePercentage; const SCORE_BAR_ERRORS = exports.SCORE_BAR_ERRORS = Object.freeze({ INVALID_BAR_COUNT: { type: 'INVALID_BAR_COUNT', description: 'totalBars can not be less than 1' } });