|
20 | 20 |
|
21 | 21 | 'use strict'; |
22 | 22 |
|
23 | | -var |
24 | | -/** @const */ FormatOptions = [ |
| 23 | +var FormatOptions = [ |
25 | 24 | 'decimals', |
26 | 25 | 'thousand', |
27 | 26 | 'mark', |
28 | 27 | 'prefix', |
29 | | - 'postfix', |
| 28 | + 'suffix', |
30 | 29 | 'encoder', |
31 | 30 | 'decoder', |
32 | 31 | 'negativeBefore', |
|
47 | 46 | return input.substring(0, match.length) === match; |
48 | 47 | } |
49 | 48 |
|
50 | | - // Check is a string ends in a specified postfix. |
| 49 | + // Check is a string ends in a specified suffix. |
51 | 50 | function strEndsWith ( input, match ) { |
52 | 51 | return input.slice(-1 * match.length) === match; |
53 | 52 | } |
|
65 | 64 | } |
66 | 65 |
|
67 | 66 | // Provide rounding-accurate toFixed method. |
68 | | - function toFixed ( value, decimals ) { |
69 | | - var scale = Math.pow(10, decimals); |
70 | | - return ( Math.round(value * scale) / scale).toFixed( decimals ); |
| 67 | + // Borrowed: http://stackoverflow.com/a/21323330/775265 |
| 68 | + function toFixed ( value, exp ) { |
| 69 | + value = value.toString().split('e'); |
| 70 | + value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp))); |
| 71 | + value = value.toString().split('e'); |
| 72 | + return (+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))).toFixed(exp); |
71 | 73 | } |
72 | 74 |
|
73 | 75 |
|
74 | 76 | // Formatting |
75 | 77 |
|
76 | 78 | // Accept a number as input, output formatted string. |
77 | | - function formatTo ( decimals, thousand, mark, prefix, postfix, encoder, decoder, negativeBefore, negative, edit, undo, input ) { |
| 79 | + function formatTo ( decimals, thousand, mark, prefix, suffix, encoder, decoder, negativeBefore, negative, edit, undo, input ) { |
78 | 80 |
|
79 | 81 | var originalInput = input, inputIsNegative, inputPieces, inputBase, inputDecimals = '', output = ''; |
80 | 82 |
|
|
151 | 153 | output += inputBase; |
152 | 154 | output += inputDecimals; |
153 | 155 |
|
154 | | - // Apply the postfix. |
155 | | - if ( postfix ) { |
156 | | - output += postfix; |
| 156 | + // Apply the suffix. |
| 157 | + if ( suffix ) { |
| 158 | + output += suffix; |
157 | 159 | } |
158 | 160 |
|
159 | 161 | // Run the output through a user-specified post-formatter. |
|
166 | 168 | } |
167 | 169 |
|
168 | 170 | // Accept a sting as input, output decoded number. |
169 | | - function formatFrom ( decimals, thousand, mark, prefix, postfix, encoder, decoder, negativeBefore, negative, edit, undo, input ) { |
| 171 | + function formatFrom ( decimals, thousand, mark, prefix, suffix, encoder, decoder, negativeBefore, negative, edit, undo, input ) { |
170 | 172 |
|
171 | 173 | var originalInput = input, inputIsNegative, output = ''; |
172 | 174 |
|
@@ -198,10 +200,10 @@ var |
198 | 200 | inputIsNegative = true; |
199 | 201 | } |
200 | 202 |
|
201 | | - // Remove the postfix. |
| 203 | + // Remove the suffix. |
202 | 204 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice |
203 | | - if ( postfix && strEndsWith(input, postfix) ) { |
204 | | - input = input.slice(0, -1 * postfix.length); |
| 205 | + if ( suffix && strEndsWith(input, suffix) ) { |
| 206 | + input = input.slice(0, -1 * suffix.length); |
205 | 207 | } |
206 | 208 |
|
207 | 209 | // Remove the thousand grouping. |
|
255 | 257 | var i, optionName, optionValue, |
256 | 258 | filteredOptions = {}; |
257 | 259 |
|
| 260 | + if ( inputOptions['suffix'] === undefined ) { |
| 261 | + inputOptions['suffix'] = inputOptions['postfix']; |
| 262 | + } |
| 263 | + |
258 | 264 | for ( i = 0; i < FormatOptions.length; i+=1 ) { |
259 | 265 |
|
260 | 266 | optionName = FormatOptions[i]; |
|
323 | 329 | return method.apply('', args); |
324 | 330 | } |
325 | 331 |
|
326 | | - /** @constructor */ |
327 | 332 | function wNumb ( options ) { |
328 | 333 |
|
329 | 334 | if ( !(this instanceof wNumb) ) { |
|
347 | 352 | }; |
348 | 353 | } |
349 | 354 |
|
350 | | - /** @export */ |
351 | 355 | return wNumb; |
352 | 356 |
|
353 | 357 | })); |
0 commit comments