@tour-kit/surveysUtilities
calculateNPS
Calculate Net Promoter Score from an array of 0–10 ratings, returning score, promoter/passive/detractor counts, and percentages
Net Promoter Score measures customer loyalty. Ratings 9–10 are promoters, 7–8 are passives, and 0–6 are detractors. The score is promoter% − detractor%, ranging from −100 to 100.
Import
import { calculateNPS } from '@tour-kit/surveys';Usage
import { calculateNPS } from '@tour-kit/surveys';
const result = calculateNPS([9, 10, 6, 7, 8, 10, 4, 10, 9, 6]);
console.log(result.score); // 20 (40% promoters − 20% detractors = 20, rounded)
console.log(result.promoters); // 4
console.log(result.promoterPct); // 40
console.log(result.detractors); // 2
console.log(result.detractorPct); // 20
console.log(result.total); // 10With survey state
import { useSurvey, calculateNPS } from '@tour-kit/surveys';
function NPSResult() {
const survey = useSurvey('nps-q1');
if (!survey.state?.isCompleted) return null;
const ratings = Array.from(survey.state.responses.values()).filter(
(v): v is number => typeof v === 'number'
);
const result = calculateNPS(ratings);
return <p>NPS: {result.score}</p>;
}Parameters
Prop
Type
Returns — NPSResult
Prop
Type
Classification thresholds
| Rating | Classification |
|---|---|
| 0–6 | Detractor |
| 7–8 | Passive |
| 9–10 | Promoter |