diff options
Diffstat (limited to 'resources/src/mediawiki.language/mediawiki.language.numbers.js')
-rw-r--r-- | resources/src/mediawiki.language/mediawiki.language.numbers.js | 96 |
1 files changed, 49 insertions, 47 deletions
diff --git a/resources/src/mediawiki.language/mediawiki.language.numbers.js b/resources/src/mediawiki.language/mediawiki.language.numbers.js index 3c13055b..268985f8 100644 --- a/resources/src/mediawiki.language/mediawiki.language.numbers.js +++ b/resources/src/mediawiki.language/mediawiki.language.numbers.js @@ -7,6 +7,26 @@ */ /** + * Replicate a string 'n' times. + * + * @private + * @param {string} str The string to replicate + * @param {number} num Number of times to replicate the string + * @return {string} + */ + function replicate( str, num ) { + if ( num <= 0 || !str ) { + return ''; + } + + var buf = []; + while ( num-- ) { + buf.push( str ); + } + return buf.join( '' ); + } + + /** * Pad a string to guarantee that it is at least `size` length by * filling with the character `ch` at either the start or end of the * string. Pads at the start, by default. @@ -34,26 +54,6 @@ } /** - * Replicate a string 'n' times. - * - * @private - * @param {string} str The string to replicate - * @param {number} num Number of times to replicate the string - * @return {string} - */ - function replicate( str, num ) { - if ( num <= 0 || !str ) { - return ''; - } - - var buf = []; - while ( num-- ) { - buf.push( str ); - } - return buf.join( '' ); - } - - /** * Apply numeric pattern to absolute value using options. Gives no * consideration to local customs. * @@ -74,7 +74,7 @@ decimal: '.' }; - if ( isNaN( value) ) { + if ( isNaN( value ) ) { return value; } @@ -85,62 +85,62 @@ off, remainder, patternParts = pattern.split( '.' ), - maxPlaces = ( patternParts[1] || [] ).length, + maxPlaces = ( patternParts[ 1 ] || [] ).length, valueParts = String( Math.abs( value ) ).split( '.' ), - fractional = valueParts[1] || '', + fractional = valueParts[ 1 ] || '', groupSize = 0, groupSize2 = 0, pieces = []; - if ( patternParts[1] ) { + if ( patternParts[ 1 ] ) { // Pad fractional with trailing zeros - padLength = ( patternParts[1] && patternParts[1].lastIndexOf( '0' ) + 1 ); + padLength = ( patternParts[ 1 ] && patternParts[ 1 ].lastIndexOf( '0' ) + 1 ); if ( padLength > fractional.length ) { - valueParts[1] = pad( fractional, padLength, '0', true ); + valueParts[ 1 ] = pad( fractional, padLength, '0', true ); } // Truncate fractional if ( maxPlaces < fractional.length ) { - valueParts[1] = fractional.slice( 0, maxPlaces ); + valueParts[ 1 ] = fractional.slice( 0, maxPlaces ); } } else { - if ( valueParts[1] ) { + if ( valueParts[ 1 ] ) { valueParts.pop(); } } // Pad whole with leading zeros - patternDigits = patternParts[0].replace( ',', '' ); + patternDigits = patternParts[ 0 ].replace( ',', '' ); padLength = patternDigits.indexOf( '0' ); if ( padLength !== -1 ) { padLength = patternDigits.length - padLength; - if ( padLength > valueParts[0].length ) { - valueParts[0] = pad( valueParts[0], padLength ); + if ( padLength > valueParts[ 0 ].length ) { + valueParts[ 0 ] = pad( valueParts[ 0 ], padLength ); } // Truncate whole if ( patternDigits.indexOf( '#' ) === -1 ) { - valueParts[0] = valueParts[0].slice( valueParts[0].length - padLength ); + valueParts[ 0 ] = valueParts[ 0 ].slice( valueParts[ 0 ].length - padLength ); } } // Add group separators - index = patternParts[0].lastIndexOf( ',' ); + index = patternParts[ 0 ].lastIndexOf( ',' ); if ( index !== -1 ) { - groupSize = patternParts[0].length - index - 1; - remainder = patternParts[0].slice( 0, index ); + groupSize = patternParts[ 0 ].length - index - 1; + remainder = patternParts[ 0 ].slice( 0, index ); index = remainder.lastIndexOf( ',' ); if ( index !== -1 ) { groupSize2 = remainder.length - index - 1; } } - for ( whole = valueParts[0]; whole; ) { + for ( whole = valueParts[ 0 ]; whole; ) { off = groupSize ? whole.length - groupSize : 0; pieces.push( ( off > 0 ) ? whole.slice( off ) : whole ); whole = ( off > 0 ) ? whole.slice( 0, off ) : ''; @@ -150,7 +150,7 @@ groupSize2 = null; } } - valueParts[0] = pieces.reverse().join( options.group ); + valueParts[ 0 ] = pieces.reverse().join( options.group ); return valueParts.join( options.decimal ); } @@ -198,17 +198,18 @@ convertedNumber = ''; for ( i = 0; i < numberString.length; i++ ) { - if ( transformTable[ numberString[i] ] ) { - convertedNumber += transformTable[numberString[i]]; + if ( transformTable[ numberString[ i ] ] ) { + convertedNumber += transformTable[ numberString[ i ] ]; } else { - convertedNumber += numberString[i]; + convertedNumber += numberString[ i ]; } } return integer ? parseInt( convertedNumber, 10 ) : convertedNumber; }, /** - * Get the digit transform table for current UI language. + * Get the digit transform table for current UI language. + * * @return {Object|Array} */ getDigitTransformTable: function () { @@ -217,7 +218,8 @@ }, /** - * Get the separator transform table for current UI language. + * Get the separator transform table for current UI language. + * * @return {Object|Array} */ getSeparatorTransformTable: function () { @@ -238,20 +240,20 @@ commafy: function ( value, pattern ) { var numberPattern, transformTable = mw.language.getSeparatorTransformTable(), - group = transformTable[','] || ',', + group = transformTable[ ',' ] || ',', numberPatternRE = /[#0,]*[#0](?:\.0*#*)?/, // not precise, but good enough - decimal = transformTable['.'] || '.', + decimal = transformTable[ '.' ] || '.', patternList = pattern.split( ';' ), - positivePattern = patternList[0]; + positivePattern = patternList[ 0 ]; - pattern = patternList[ ( value < 0 ) ? 1 : 0] || ( '-' + positivePattern ); + pattern = patternList[ ( value < 0 ) ? 1 : 0 ] || ( '-' + positivePattern ); numberPattern = positivePattern.match( numberPatternRE ); if ( !numberPattern ) { throw new Error( 'unable to find a number expression in pattern: ' + pattern ); } - return pattern.replace( numberPatternRE, commafyNumber( value, numberPattern[0], { + return pattern.replace( numberPatternRE, commafyNumber( value, numberPattern[ 0 ], { decimal: decimal, group: group } ) ); |