Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / StringUtil.js
1 /**
2 * Provides helper functions for String handling.
3 *
4 * @author Tim Duesterhus, Joshua Ruesweg
5 * @copyright 2001-2018 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/StringUtil
8 */
9 define(['Language', './NumberUtil'], function(Language, NumberUtil) {
10 "use strict";
11
12 /**
13 * @exports WoltLabSuite/Core/StringUtil
14 */
15 return {
16 /**
17 * Adds thousands separators to a given number.
18 *
19 * @see http://stackoverflow.com/a/6502556/782822
20 * @param {?} number
21 * @return {String}
22 */
23 addThousandsSeparator: function(number) {
24 // Fetch Language, as it cannot be provided because of a circular dependency
25 if (Language === undefined) Language = require('Language');
26
27 return String(number).replace(/(^-?\d{1,3}|\d{3})(?=(?:\d{3})+(?:$|\.))/g, '$1' + Language.get('wcf.global.thousandsSeparator'));
28 },
29
30 /**
31 * Escapes special HTML-characters within a string
32 *
33 * @param {?} string
34 * @return {String}
35 */
36 escapeHTML: function (string) {
37 return String(string).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
38 },
39
40 /**
41 * Escapes a String to work with RegExp.
42 *
43 * @see https://github.com/sstephenson/prototype/blob/master/src/prototype/lang/regexp.js#L25
44 * @param {?} string
45 * @return {String}
46 */
47 escapeRegExp: function(string) {
48 return String(string).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
49 },
50
51 /**
52 * Rounds number to given count of floating point digits, localizes decimal-point and inserts thousands separators.
53 *
54 * @param {?} number
55 * @param {int} decimalPlaces The number of decimal places to leave after rounding.
56 * @return {String}
57 */
58 formatNumeric: function(number, decimalPlaces) {
59 // Fetch Language, as it cannot be provided because of a circular dependency
60 if (Language === undefined) Language = require('Language');
61
62 number = String(NumberUtil.round(number, decimalPlaces || -2));
63 var numberParts = number.split('.');
64
65 number = this.addThousandsSeparator(numberParts[0]);
66 if (numberParts.length > 1) number += Language.get('wcf.global.decimalPoint') + numberParts[1];
67
68 number = number.replace('-', '\u2212');
69
70 return number;
71 },
72
73 /**
74 * Makes a string's first character lowercase.
75 *
76 * @param {?} string
77 * @return {String}
78 */
79 lcfirst: function(string) {
80 return String(string).substring(0, 1).toLowerCase() + string.substring(1);
81 },
82
83 /**
84 * Makes a string's first character uppercase.
85 *
86 * @param {?} string
87 * @return {String}
88 */
89 ucfirst: function(string) {
90 return String(string).substring(0, 1).toUpperCase() + string.substring(1);
91 },
92
93 /**
94 * Unescapes special HTML-characters within a string.
95 *
96 * @param {?} string
97 * @return {String}
98 */
99 unescapeHTML: function(string) {
100 return String(string).replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
101 },
102
103 /**
104 * Shortens numbers larger than 1000 by using unit prefixes.
105 *
106 * @param {?} number
107 * @return {String}
108 */
109 shortUnit: function(number) {
110 var unitSuffix = '';
111
112 if (number >= 1000000) {
113 number /= 1000000;
114
115 if (number > 10) {
116 number = Math.floor(number);
117 }
118 else {
119 number = NumberUtil.round(number, -1);
120 }
121
122 unitSuffix = 'M';
123 }
124 else if (number >= 1000) {
125 number /= 1000;
126
127 if (number > 10) {
128 number = Math.floor(number);
129 }
130 else {
131 number = NumberUtil.round(number, -1);
132 }
133
134 unitSuffix = 'k';
135 }
136
137 return this.formatNumeric(number) + unitSuffix;
138 }
139 };
140 });