From 4bab3b9f96c6f5ba038e4b53b8b88fc41636b437 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Mon, 7 Nov 2016 16:22:34 +0100 Subject: [PATCH] Add shortUnit JavaScript function --- .../files/js/WoltLabSuite/Core/StringUtil.js | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) 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; } }; }); -- 2.20.1