MOBILE-2992 rating: Helper function to merge rating infos

main
Albert Gasset 2019-05-07 11:29:15 +02:00
parent 7f643d6560
commit af28ed2732
1 changed files with 38 additions and 0 deletions

View File

@ -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.
*