diff options
Diffstat (limited to 'js/sprintf.js')
| -rw-r--r-- | js/sprintf.js | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/js/sprintf.js b/js/sprintf.js index 4d61fa2..244fe2a 100644 --- a/js/sprintf.js +++ b/js/sprintf.js | |||
| @@ -19,60 +19,55 @@ | |||
| 19 | "use strict"; | 19 | "use strict"; |
| 20 | 20 | ||
| 21 | function sprintf() | 21 | function sprintf() |
| 22 | { | 22 | { |
| 23 | var argc = 0; | 23 | var argc = 0; |
| 24 | var args = arguments; | 24 | var args = arguments; |
| 25 | var fmt = args[argc++]; | 25 | var fmt = args[argc++]; |
| 26 | 26 | ||
| 27 | function lpad (str, padString, length) | 27 | function lpad (str, padString, length) |
| 28 | { | 28 | { |
| 29 | while (str.length < length) | 29 | while (str.length < length) |
| 30 | str = padString + str; | 30 | str = padString + str; |
| 31 | return str; | 31 | return str; |
| 32 | }; | 32 | } |
| 33 | 33 | ||
| 34 | function format (match, width, dot, precision, length, conversion) | 34 | function format (match, width, dot, precision, length, conversion) |
| 35 | { | 35 | { |
| 36 | if (match === '%%') return '%'; | 36 | if (match === '%%') return '%'; |
| 37 | 37 | ||
| 38 | var value = args[argc++]; | 38 | var value = args[argc++]; |
| 39 | var prefix; | 39 | var prefix; |
| 40 | 40 | ||
| 41 | if (width === undefined) | 41 | if (width === undefined) |
| 42 | width = 0; | 42 | width = 0; |
| 43 | else | 43 | else |
| 44 | width = +width; | 44 | width = +width; |
| 45 | 45 | ||
| 46 | if (precision === undefined) | 46 | if (precision === undefined) |
| 47 | precision = conversion == 'd' ? 0 : 6; | 47 | precision = conversion == 'd' ? 0 : 6; |
| 48 | else | 48 | else |
| 49 | precision = +precision; | 49 | precision = +precision; |
| 50 | 50 | ||
| 51 | switch (conversion) { | 51 | switch (conversion) { |
| 52 | case 's': | 52 | case 's': |
| 53 | case 'c': | 53 | case 'c': |
| 54 | return value; | 54 | return value; |
| 55 | break; | ||
| 56 | case 'd': | 55 | case 'd': |
| 57 | return parseInt(value, 10); | 56 | return parseInt(value, 10); |
| 58 | break; | ||
| 59 | case 'e': | 57 | case 'e': |
| 60 | prefix = value < 0 ? '-' : ''; | 58 | prefix = value < 0 ? '-' : ''; |
| 61 | return lpad(prefix+Math.abs(value).toExponential(precision),' ',width); | 59 | return lpad(prefix+Math.abs(value).toExponential(precision),' ',width); |
| 62 | break; | ||
| 63 | case 'F': | 60 | case 'F': |
| 64 | case 'f': | 61 | case 'f': |
| 65 | prefix = value < 0 ? '-' : ''; | 62 | prefix = value < 0 ? '-' : ''; |
| 66 | return lpad(prefix+Math.abs(value).toFixed(precision),' ',width); | 63 | return lpad(prefix+Math.abs(value).toFixed(precision),' ',width); |
| 67 | break; | ||
| 68 | case 'g': | 64 | case 'g': |
| 69 | prefix = value < 0 ? '-' : ''; | 65 | prefix = value < 0 ? '-' : ''; |
| 70 | return lpad(prefix+Math.abs(value).toPrecision(precision),' ',width); | 66 | return lpad(prefix+Math.abs(value).toPrecision(precision),' ',width); |
| 71 | break; | ||
| 72 | default: | 67 | default: |
| 73 | return match; | 68 | return match; |
| 74 | } | 69 | } |
| 75 | 70 | ||
| 76 | }; | 71 | } |
| 77 | return fmt.replace(/%(\d+)?(\.(\d+))?(l?)([%scdfFeg])/g,format); | 72 | return fmt.replace(/%(\d+)?(\.(\d+))?(l?)([%scdfFeg])/g,format); |
| 78 | }; | 73 | } |
