From: Joshua Rüsweg Date: Mon, 7 Nov 2016 15:22:34 +0000 (+0100) Subject: Add shortUnit JavaScript function X-Git-Tag: 3.0.0_Beta_5~66^2~2^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=4bab3b9f96c6f5ba038e4b53b8b88fc41636b437;p=GitHub%2FWoltLab%2FWCF.git Add shortUnit JavaScript function --- diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/StringUtil.js b/wcfsetup/install/files/js/WoltLabSuite/Core/StringUtil.js index 6511a3246c..f9c008df6d 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/StringUtil.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/StringUtil.js @@ -1,7 +1,7 @@ /** * Provides helper functions for String handling. * - * @author Tim Duesterhus + * @author Tim Duesterhus, Joshua Ruesweg * @copyright 2001-2016 WoltLab GmbH * @license GNU Lesser General Public License * @module WoltLabSuite/Core/StringUtil @@ -96,8 +96,45 @@ define(['Language', './NumberUtil'], function(Language, NumberUtil) { * @param {?} string * @return {String} */ - unescapeHTML: function (string) { + unescapeHTML: function(string) { return String(string).replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>'); + }, + + /** + * Shortens numbers larger than 1000 by using unit prefixes. + * + * @param {?} number + * @return {String} + */ + shortUnit: function(number) { + var unitSuffix = ''; + + if (number >= 1000000) { + number /= 1000000; + + if (number > 10) { + number = Math.floor(number); + } + else { + number = NumberUtil.round(number, -1); + } + + unitSuffix = 'M'; + } + else if (number >= 1000) { + number /= 1000; + + if (number > 10) { + number = Math.floor(number); + } + else { + number = NumberUtil.round(number, -1); + } + + unitSuffix = 'k'; + } + + return this.formatNumeric(number) + unitSuffix; } }; });