mirror of
https://github.com/zschaffer/rating-room.git
synced 2024-06-18 15:07:05 +00:00
9 lines
308 B
TypeScript
9 lines
308 B
TypeScript
import type { Rating } from '$types';
|
|
|
|
/** Average of all ratings of a product */
|
|
export const avgRating = (ratings: Rating[]): number => {
|
|
if (!ratings) return 0;
|
|
if (ratings.length === 1) return ratings[0].rating;
|
|
else return ratings.reduce((total, curr) => total + curr.rating, 0) / ratings.length;
|
|
};
|