Net Promoter Score (NPS®)
The net_promoter_score crate is a Rust library for working with and calculating Net Promoter Scores (NPS) from survey responses. The library is highly customizable, making it easy to work with different respondent ID types and score ranges. It includes methods for adding single and multiple responses, bulk responses, and auto-generated unique respondent IDs. Furthermore, the crate efficiently handles edge cases and has detailed error messages to ensure your application is robust and accurate.
Features
- Calculate NPS from survey responses
- Support for custom respondent ID types
- Detailed error messages
- Efficient performance
- Clear and easy-to-understand examples
- Auto-generate unique respondent IDs (for
i32identifiers)
Installation
Add this to your Cargo.toml:
[]
= "0.2.1"
Example Usage
Creating a survey and adding responses
To create a new survey and add single responses to it, you can use the following code:
use *;
use Result;
Adding multiple responses at once can be done using add_multiple_responses as shown below:
use *;
use Result;
Adding bulk responses with auto-generated respondent IDs of type i32
use *;
use Result;
Here's an example using &str as the respondent ID type for the Survey:
use *;
use Result;
Want more control over how IDs are generated?
Here's an example using a respondent ID generator function with custom String respondent IDs:
use *;
use Result;
In this example, we first define rating_quantities containing the NPS rating and respective count for each rating. We create a new Survey with String as the respondent ID type. Next, we create a respondent ID generator function respondent_id_fn which generates unique custom String respondent IDs in the format "customer_{number}".
Finally, we call add_bulk_responses with the generator function and the rating_quantities, which adds responses to the survey with unique respondent IDs. The calculated NPS is printed to the console.
For specific details and more advanced use cases, please consult the crate documentation and source code.
API Overview
The main type provided by the crate is the Survey struct. The Survey includes methods for adding responses and calculating the NPS:
new(): Creates a new empty survey.add_response(respondent_id: T, score: u8): Adds a single survey response with the given respondent ID and score.add_multiple_responses(responses: impl IntoIterator<Item = (T, u8)>): Adds multiple survey responses.add_bulk_responses(respondent_id_fn: F, quantities: &[(u8, usize)]): Adds bulk survey responses with a respondent ID generator function and a slice of tuples (rating, quantity).add_bulk_responses_auto_id(quantities: &[(u8, usize)]): Adds bulk survey responses with auto-generated unique respondent IDs of typei32, starting at 1 (specialized implementation for respondent IDs of type i32).from_responses(responses: impl IntoIterator<Item = (T, u8)>): Creates a new survey from a set of responses. If any responses have an invalid rating, an error will be returned.score(): Calculates and returns the Net Promoter Score (NPS) of the survey.
Feedback and Contributions
I appreciate any feedback and suggestions to improve this crate. Feel free to open an issue or submit a pull request if you want to contribute to the project directly.
License
This crate is free software: you can redistribute it and/or modify it under the terms of the MIT License.
Net Promoter®, NPS®, NPS Prism®, and the NPS-related emoticons are registered trademarks of Bain & Company, Inc., NICE Systems, Inc., and Fred Reichheld. Net Promoter Score and Net Promoter System are service marks of Bain & Company, Inc., NICE Systems, Inc., and Fred Reichheld.
Like this crate?
⭐ Star https://github.com/rrrodzilla/net_promoter_score
🐦 Follow https://twitter.com/rrrodzilla