2 * Number-related utilities for mediawiki.language.
10 * Replicate a string 'n' times.
13 * @param {string} str The string to replicate
14 * @param {number} num Number of times to replicate the string
17 function replicate( str, num ) {
20 if ( num <= 0 || !str ) {
27 return buf.join( '' );
31 * Pad a string to guarantee that it is at least `size` length by
32 * filling with the character `ch` at either the start or end of the
33 * string. Pads at the start, by default.
35 * Example: Fill the string to length 10 with '+' characters on the right.
37 * pad( 'blah', 10, '+', true ); // => 'blah++++++'
40 * @param {string} text The string to pad
41 * @param {number} size The length to pad to
42 * @param {string} [ch='0'] Character to pad with
43 * @param {boolean} [end=false] Adds padding at the end if true, otherwise pads at start
46 function pad( text, size, ch, end ) {
54 padStr = replicate( ch, Math.ceil( ( size - out.length ) / ch.length ) );
56 return end ? out + padStr : padStr + out;
60 * Apply numeric pattern to absolute value using options. Gives no
61 * consideration to local customs.
63 * Adapted from dojo/number library with thanks
64 * <http://dojotoolkit.org/reference-guide/1.8/dojo/number.html>
67 * @param {number} value the number to be formatted, ignores sign
68 * @param {string} pattern the number portion of a pattern (e.g. `#,##0.00`)
69 * @param {Object} [options] If provided, both option keys must be present:
70 * @param {string} options.decimal The decimal separator. Defaults to: `'.'`.
71 * @param {string} options.group The group separator. Defaults to: `','`.
74 function commafyNumber( value, pattern, options ) {
81 patternParts = pattern.split( '.' ),
82 maxPlaces = ( patternParts[ 1 ] || [] ).length,
83 valueParts = String( Math.abs( value ) ).split( '.' ),
84 fractional = valueParts[ 1 ] || '',
89 options = options || {
94 if ( isNaN( value ) ) {
98 if ( patternParts[ 1 ] ) {
99 // Pad fractional with trailing zeros
100 padLength = ( patternParts[ 1 ] && patternParts[ 1 ].lastIndexOf( '0' ) + 1 );
102 if ( padLength > fractional.length ) {
103 valueParts[ 1 ] = pad( fractional, padLength, '0', true );
106 // Truncate fractional
107 if ( maxPlaces < fractional.length ) {
108 valueParts[ 1 ] = fractional.slice( 0, maxPlaces );
111 if ( valueParts[ 1 ] ) {
116 // Pad whole with leading zeros
117 patternDigits = patternParts[ 0 ].replace( ',', '' );
119 padLength = patternDigits.indexOf( '0' );
121 if ( padLength !== -1 ) {
122 padLength = patternDigits.length - padLength;
124 if ( padLength > valueParts[ 0 ].length ) {
125 valueParts[ 0 ] = pad( valueParts[ 0 ], padLength );
129 if ( patternDigits.indexOf( '#' ) === -1 ) {
130 valueParts[ 0 ] = valueParts[ 0 ].slice( valueParts[ 0 ].length - padLength );
134 // Add group separators
135 index = patternParts[ 0 ].lastIndexOf( ',' );
137 if ( index !== -1 ) {
138 groupSize = patternParts[ 0 ].length - index - 1;
139 remainder = patternParts[ 0 ].slice( 0, index );
140 index = remainder.lastIndexOf( ',' );
141 if ( index !== -1 ) {
142 groupSize2 = remainder.length - index - 1;
146 for ( whole = valueParts[ 0 ]; whole; ) {
147 off = groupSize ? whole.length - groupSize : 0;
148 pieces.push( ( off > 0 ) ? whole.slice( off ) : whole );
149 whole = ( off > 0 ) ? whole.slice( 0, off ) : '';
152 groupSize = groupSize2;
156 valueParts[ 0 ] = pieces.reverse().join( options.group );
158 return valueParts.join( options.decimal );
162 * Helper function to flip transformation tables.
164 * @param {...Object} Transformation tables
167 function flipTransform() {
168 var i, key, table, flipped = {};
170 // Ensure we strip thousand separators. This might be overwritten.
173 for ( i = 0; i < arguments.length; i++ ) {
174 table = arguments[ i ];
175 for ( key in table ) {
176 if ( table.hasOwnProperty( key ) ) {
177 // The thousand separator should be deleted
178 flipped[ table[ key ] ] = key === ',' ? '' : key;
186 $.extend( mw.language, {
189 * Converts a number using #getDigitTransformTable.
191 * @param {number} num Value to be converted
192 * @param {boolean} [integer=false] Whether to convert the return value to an integer
193 * @return {number|string} Formatted number
195 convertNumber: function ( num, integer ) {
196 var transformTable, digitTransformTable, separatorTransformTable,
197 i, numberString, convertedNumber, pattern;
199 // Quick shortcut for plain numbers
200 if ( integer && parseInt( num, 10 ) === num ) {
204 // Load the transformation tables (can be empty)
205 digitTransformTable = mw.language.getDigitTransformTable();
206 separatorTransformTable = mw.language.getSeparatorTransformTable();
209 // Reverse the digit transformation tables if we are doing unformatting
210 transformTable = flipTransform( separatorTransformTable, digitTransformTable );
211 numberString = String( num );
213 // This check being here means that digits can still be unformatted
214 // even if we do not produce them. This seems sane behavior.
215 if ( mw.config.get( 'wgTranslateNumerals' ) ) {
216 transformTable = digitTransformTable;
219 // Commaying is more complex, so we handle it here separately.
220 // When unformatting, we just use separatorTransformTable.
221 pattern = mw.language.getData( mw.config.get( 'wgUserLanguage' ),
222 'digitGroupingPattern' ) || '#,##0.###';
223 numberString = mw.language.commafy( num, pattern );
226 if ( transformTable ) {
227 convertedNumber = '';
228 for ( i = 0; i < numberString.length; i++ ) {
229 if ( transformTable.hasOwnProperty( numberString[ i ] ) ) {
230 convertedNumber += transformTable[ numberString[ i ] ];
232 convertedNumber += numberString[ i ];
236 convertedNumber = numberString;
240 // Parse string to integer. This loses decimals!
241 convertedNumber = parseInt( convertedNumber, 10 );
244 return convertedNumber;
248 * Get the digit transform table for current UI language.
250 * @return {Object|Array}
252 getDigitTransformTable: function () {
253 return mw.language.getData( mw.config.get( 'wgUserLanguage' ),
254 'digitTransformTable' ) || [];
258 * Get the separator transform table for current UI language.
260 * @return {Object|Array}
262 getSeparatorTransformTable: function () {
263 return mw.language.getData( mw.config.get( 'wgUserLanguage' ),
264 'separatorTransformTable' ) || [];
268 * Apply pattern to format value as a string.
270 * Using patterns from [Unicode TR35](http://www.unicode.org/reports/tr35/#Number_Format_Patterns).
272 * @param {number} value
273 * @param {string} pattern Pattern string as described by Unicode TR35
274 * @throws {Error} If unable to find a number expression in `pattern`.
277 commafy: function ( value, pattern ) {
279 transformTable = mw.language.getSeparatorTransformTable(),
280 group = transformTable[ ',' ] || ',',
281 numberPatternRE = /[#0,]*[#0](?:\.0*#*)?/, // not precise, but good enough
282 decimal = transformTable[ '.' ] || '.',
283 patternList = pattern.split( ';' ),
284 positivePattern = patternList[ 0 ];
286 pattern = patternList[ ( value < 0 ) ? 1 : 0 ] || ( '-' + positivePattern );
287 numberPattern = positivePattern.match( numberPatternRE );
289 if ( !numberPattern ) {
290 throw new Error( 'unable to find a number expression in pattern: ' + pattern );
293 return pattern.replace( numberPatternRE, commafyNumber( value, numberPattern[ 0 ], {
301 }( mediaWiki, jQuery ) );