File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 145145 "results_denied" : " Failed community vote and will not be added." ,
146146 "instapassed" : " Instapassed by %USER%" ,
147147 "invalidated" : " Invalidated by %USER%" ,
148- "upvote_percentage" : " (%d% upvoted)"
148+ "upvote_percentage" : " (%d% upvoted)" ,
149+ "no_votes" : " (Nobody voted…)"
149150 },
150151 "footer" : {
151152 "texture_edit" : " Reference | Submitted | Current" ,
Original file line number Diff line number Diff line change @@ -107,13 +107,16 @@ export function sendMessage(
107107 * @returns formatted string (or an empty string if not possible)
108108 */
109109export function getPercentage ( upvotes : number , downvotes : number ) {
110- const upvotePercentage =
111- ( ( upvotes - DEFAULT_REACTION_COUNT ) * 100 ) /
112- ( upvotes - DEFAULT_REACTION_COUNT + ( downvotes - DEFAULT_REACTION_COUNT ) ) ;
113- // handle division by zero and cache issues
114- if ( isNaN ( upvotePercentage ) || ! isFinite ( upvotePercentage ) ) return "" ;
115- return strings . submission . status . upvote_percentage . replace (
116- "%d" ,
117- String ( Number ( upvotePercentage . toFixed ( 2 ) ) ) ,
118- ) ;
110+ // normalize
111+ upvotes -= DEFAULT_REACTION_COUNT ;
112+ downvotes -= DEFAULT_REACTION_COUNT ;
113+
114+ // prevent division by zero if nobody voted
115+ if ( upvotes === 0 && downvotes === 0 ) return strings . submission . status . no_votes ;
116+
117+ const upvoteRatio = upvotes / ( upvotes + downvotes ) ;
118+
119+ // re-cast to number to remove unnecessary decimals (100.00 -> 100)
120+ const upvotePercentage = Number ( ( upvoteRatio * 100 ) . toFixed ( 2 ) ) ;
121+ return strings . submission . status . upvote_percentage . replace ( "%d" , String ( upvotePercentage ) ) ;
119122}
You can’t perform that action at this time.
0 commit comments