@mhfrough/star-rating
v1.0.1
Published
Tiny, dependency-free function to generate 5-star ratings with optional half-star (★⯪☆) support.
Maintainers
Readme
⭐ @mhfrough/star-rating
Tiny, dependency-free function to generate 5-star ratings with optional half-star (
★⯪☆) support.
✨ Features
- Supports decimal ratings (e.g.,
3.5→★★★⯪☆) - Unicode-based, lightweight, and zero dependencies
- Works in both Node.js and frontend environments
- Includes TypeScript definitions
📦 Installation
npm install @mhfrough/star-rating🔧 Usage
const rating = require('@mhfrough/star-rating');
console.log(rating(0)); // ☆☆☆☆☆
console.log(rating(2)); // ★★☆☆☆
console.log(rating(3.5)); // ★★★⯪☆
console.log(rating(5)); // ★★★★★📘 API
rating(n: number): string
| Parameter | Type | Description |
|-----------|--------|----------------------------------------|
| n | number | A value from 0 to 5 (can be decimal)|
Returns a string like ★★★⯪☆ representing the rating.
Throws an error if n is not a valid number between 0 and 5.
🧪 Example Output
| Input | Output | |-------|-----------| | 0 | ☆☆☆☆☆ | | 1.5 | ★⯪☆☆☆ | | 3 | ★★★☆☆ | | 4.5 | ★★★★⯪ | | 5 | ★★★★★ |
✅ TypeScript Support
import rating from '@mhfrough/star-rating';
const stars: string = rating(4.5); // "★★★★⯪"Type definitions are included automatically.
📁 File Structure
index.js // Main function
index.d.ts // Type definitions
README.md // This file
LICENSE // MIT License🪪 License
MIT © mhfrough
