1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Number related utilities for mediawiki.language
*/
( function ( mw, $ ) {
/**
* 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.
* example:
* Fill the string to length 10 with '+' characters on the right. Yields 'blah++++++'.
* pad('blah', 10, '+', true);
*
* @param {string} text The string to pad
* @param {Number} size To provide padding
* @param {string} ch Character to pad, defaults to '0'
* @param {Boolean} end Adds padding at the end if true, otherwise pads at start
* @return {string}
*/
function pad ( text, size, ch, end ) {
if ( !ch ) {
ch = '0';
}
var out = String( text ),
padStr = replicate( ch, Math.ceil( ( size - out.length ) / ch.length ) );
return end ? out + padStr : padStr + out;
}
/**
* Efficiently replicate a string n times.
*
* @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 );
str += str;
}
return buf.join( '' );
}
/**
* Apply numeric pattern to absolute value using options. Gives no
* consideration to local customs.
*
* Adapted from dojo/number library with thanks
* http://dojotoolkit.org/reference-guide/1.8/dojo/number.html
*
* @param {Number} value the number to be formatted, ignores sign
* @param {string} pattern the number portion of a pattern (e.g. `#,##0.00`)
* @param {string} options.decimalThe decimal separator
* @param {string} options.group The group separator
*
* @return {string}
*/
function commafyNumber( value, pattern, options ) {
options = options || {
group: ',',
decimal: '.'
};
if ( isNaN( value) ) {
return value;
}
var padLength,
patternDigits,
index,
whole,
off,
remainder,
patternParts = pattern.split( '.' ),
maxPlaces = ( patternParts[1] || [] ).length,
valueParts = String( Math.abs( value ) ).split( '.' ),
fractional = valueParts[1] || '',
groupSize = 0,
groupSize2 = 0,
pieces = [];
if ( patternParts[1] ) {
// Pad fractional with trailing zeros
padLength = ( patternParts[1] && patternParts[1].lastIndexOf( '0' ) + 1 );
if ( padLength > fractional.length ) {
valueParts[1] = pad( fractional, padLength, '0', true );
}
// Truncate fractional
if ( maxPlaces < fractional.length ) {
valueParts[1] = fractional.substr( 0, maxPlaces );
}
} else {
if ( valueParts[1] ) {
valueParts.pop();
}
}
// Pad whole with leading zeros
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 );
}
// Truncate whole
if ( patternDigits.indexOf( '#' ) === -1 ) {
valueParts[0] = valueParts[0].substr( valueParts[0].length - padLength );
}
}
// Add group separators
index = patternParts[0].lastIndexOf( ',' );
if ( index !== -1 ) {
groupSize = patternParts[0].length - index - 1;
remainder = patternParts[0].substr( 0, index );
index = remainder.lastIndexOf( ',' );
if ( index !== -1 ) {
groupSize2 = remainder.length - index - 1;
}
}
for ( whole = valueParts[0]; whole; ) {
off = whole.length - groupSize;
pieces.push( ( off > 0 ) ? whole.substr( off ) : whole );
whole = ( off > 0 ) ? whole.slice( 0, off ) : '';
if ( groupSize2 ) {
groupSize = groupSize2;
}
}
valueParts[0] = pieces.reverse().join( options.group );
return valueParts.join( options.decimal );
}
$.extend( mw.language, {
/**
* Converts a number using digitTransformTable.
*
* @param {Number} num Value to be converted
* @param {boolean} integer Convert the return value to an integer
* @return {Number|string} Formatted number
*/
convertNumber: function ( num, integer ) {
var i, tmp, transformTable, numberString, convertedNumber, pattern;
pattern = mw.language.getData( mw.config.get( 'wgUserLanguage' ),
'digitGroupingPattern' ) || '#,##0.###';
// Set the target transform table:
transformTable = mw.language.getDigitTransformTable();
if ( !transformTable ) {
return num;
}
// Check if the 'restore' to Latin number flag is set:
if ( integer ) {
if ( parseInt( num, 10 ) === num ) {
return num;
}
tmp = [];
for ( i in transformTable ) {
tmp[ transformTable[ i ] ] = i;
}
transformTable = tmp;
numberString = num + '';
} else {
numberString = mw.language.commafy( num, pattern );
}
convertedNumber = '';
for ( i = 0; i < numberString.length; i++ ) {
if ( transformTable[ numberString[i] ] ) {
convertedNumber += transformTable[numberString[i]];
} else {
convertedNumber += numberString[i];
}
}
return integer ? parseInt( convertedNumber, 10 ) : convertedNumber;
},
getDigitTransformTable: function () {
return mw.language.getData( mw.config.get( 'wgUserLanguage' ),
'digitTransformTable' ) || [];
},
getSeparatorTransformTable: function () {
return mw.language.getData( mw.config.get( 'wgUserLanguage' ),
'separatorTransformTable' ) || [];
},
/**
* Apply pattern to format value as a string using as per
* unicode.org TR35 - http://www.unicode.org/reports/tr35/#Number_Format_Patterns.
*
* @param {Number} value
* @param {string} pattern Pattern string as described by Unicode TR35
* @throws Error
* @returns {String}
*/
commafy: function ( value, pattern ) {
var numberPattern,
transformTable = mw.language.getSeparatorTransformTable(),
group = transformTable[','] || ',',
numberPatternRE = /[#0,]*[#0](?:\.0*#*)?/, // not precise, but good enough
decimal = transformTable['.'] || '.',
patternList = pattern.split( ';' ),
positivePattern = patternList[0];
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], {
decimal: decimal,
group: group
} ) );
}
} );
}( mediaWiki, jQuery ) );
|