diff --git a/src/core/rating/providers/rating.ts b/src/core/rating/providers/rating.ts index e14e4593f..1b9104459 100644 --- a/src/core/rating/providers/rating.ts +++ b/src/core/rating/providers/rating.ts @@ -326,6 +326,44 @@ export class CoreRatingProvider { }); } + /** + * Convenience function to merge two or more rating infos of the same instance. + * + * @param {CoreRatingInfo[]} ratingInfos Array of rating infos. + * @return {CoreRatingInfo} Merged rating info or null. + */ + mergeRatingInfos(ratingInfos: CoreRatingInfo[]): CoreRatingInfo { + let result: CoreRatingInfo = null; + const scales = {}; + const ratings = {}; + + ratingInfos.forEach((ratingInfo) => { + if (!ratingInfo) { + // Skip null rating infos. + return; + } + + if (!result) { + result = Object.assign({}, ratingInfo); + } + + (ratingInfo.scales || []).forEach((scale) => { + scales[scale.id] = scale; + }); + + (ratingInfo.ratings || []).forEach((rating) => { + ratings[rating.itemid] = rating; + }); + }); + + if (result) { + result.scales = this.utils.objectToArray(scales); + result.ratings = this.utils.objectToArray(ratings); + } + + return result; + } + /** * Prefetch individual ratings. *