From af28ed2732e529cccf3d674c3618d4ac153645ca Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Tue, 7 May 2019 11:29:15 +0200 Subject: [PATCH] MOBILE-2992 rating: Helper function to merge rating infos --- src/core/rating/providers/rating.ts | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) 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. *