|
1 | 1 | /*! @preserve
|
2 | 2 | * numeral.js
|
3 |
| - * version : 2.0.3 |
| 3 | + * version : 2.0.4 |
4 | 4 | * author : Adam Draper
|
5 | 5 | * license : MIT
|
6 | 6 | * http://adamwdraper.github.com/Numeral-js/
|
|
21 | 21 |
|
22 | 22 | var numeral,
|
23 | 23 | _,
|
24 |
| - VERSION = '2.0.3', |
| 24 | + VERSION = '2.0.4', |
25 | 25 | formats = {},
|
26 | 26 | locales = {},
|
27 | 27 | defaults = {
|
|
102 | 102 | numberToFormat: function(value, format, roundingFunction) {
|
103 | 103 | var locale = locales[numeral.options.currentLocale],
|
104 | 104 | negP = false,
|
105 |
| - signed = false, |
106 | 105 | optDec = false,
|
107 | 106 | abbr = '',
|
108 | 107 | trillion = 1000000000000,
|
109 | 108 | billion = 1000000000,
|
110 | 109 | million = 1000000,
|
111 | 110 | thousand = 1000,
|
| 111 | + decimal = '', |
| 112 | + neg = false, |
112 | 113 | abbrForce, // force abbreviation
|
113 | 114 | abs,
|
114 | 115 | min,
|
115 | 116 | max,
|
116 | 117 | power,
|
117 | 118 | int,
|
118 | 119 | precision,
|
| 120 | + signed, |
119 | 121 | thousands,
|
120 |
| - decimal = '', |
121 |
| - neg = false; |
| 122 | + output; |
122 | 123 |
|
123 | 124 | // make sure we never format a null value
|
124 | 125 | value = value || 0;
|
|
129 | 130 | // if both are present we default to parentheses
|
130 | 131 | if (numeral._.includes(format, '(')) {
|
131 | 132 | negP = true;
|
132 |
| - format = format.slice(1, -1); |
133 |
| - } else if (numeral._.includes(format, '+')) { |
134 |
| - signed = true; |
135 |
| - format = format.replace(/\+/g, ''); |
| 133 | + format = format.replace(/[\(|\)]/g, ''); |
| 134 | + } else if (numeral._.includes(format, '+') || numeral._.includes(format, '-')) { |
| 135 | + signed = numeral._.includes(format, '+') ? format.indexOf('+') : value < 0 ? format.indexOf('-') : -1; |
| 136 | + format = format.replace(/[\+|\-]/g, ''); |
136 | 137 | }
|
137 | 138 |
|
138 | 139 | // see if abbreviation is wanted
|
|
167 | 168 | }
|
168 | 169 | }
|
169 | 170 |
|
170 |
| - |
| 171 | + // check for optional decimals |
171 | 172 | if (numeral._.includes(format, '[.]')) {
|
172 | 173 | optDec = true;
|
173 | 174 | format = format.replace('[.]', '.');
|
174 | 175 | }
|
175 | 176 |
|
| 177 | + // break number and format |
176 | 178 | int = value.toString().split('.')[0];
|
177 | 179 | precision = format.split('.')[1];
|
178 | 180 | thousands = format.indexOf(',');
|
|
201 | 203 | int = numeral._.toFixed(value, null, roundingFunction);
|
202 | 204 | }
|
203 | 205 |
|
| 206 | + // check abbreviation again after rounding |
| 207 | + if (abbr && !abbrForce && Number(int) >= 1000 && abbr !== locale.abbreviations.trillion) { |
| 208 | + int = String(Number(int) / 1000); |
| 209 | + |
| 210 | + switch (abbr) { |
| 211 | + case locale.abbreviations.thousand: |
| 212 | + abbr = locale.abbreviations.million; |
| 213 | + break; |
| 214 | + case locale.abbreviations.million: |
| 215 | + abbr = locale.abbreviations.billion; |
| 216 | + break; |
| 217 | + case locale.abbreviations.billion: |
| 218 | + abbr = locale.abbreviations.trillion; |
| 219 | + break; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + |
204 | 224 | // format number
|
205 | 225 | if (numeral._.includes(int, '-')) {
|
206 | 226 | int = int.slice(1);
|
|
215 | 235 | int = '';
|
216 | 236 | }
|
217 | 237 |
|
218 |
| - return (negP && neg ? '(' : '') + (!negP && neg ? '-' : '') + (!neg && signed ? '+' : '') + int + decimal + (abbr ? abbr : '') + (negP && neg ? ')' : ''); |
| 238 | + output = int + decimal + (abbr ? abbr : ''); |
| 239 | + |
| 240 | + if (negP) { |
| 241 | + output = (negP && neg ? '(' : '') + output + (negP && neg ? ')' : ''); |
| 242 | + } else { |
| 243 | + if (signed >= 0) { |
| 244 | + output = signed === 0 ? (neg ? '-' : '+') + output : output + (neg ? '-' : '+'); |
| 245 | + } else if (neg) { |
| 246 | + output = '-' + output; |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + return output; |
219 | 251 | },
|
220 | 252 | // unformats numbers separators, decimals places, signs, abbreviations
|
221 | 253 | stringToNumber: function(string) {
|
|
269 | 301 | includes: function(string, search) {
|
270 | 302 | return string.indexOf(search) !== -1;
|
271 | 303 | },
|
| 304 | + insert: function(string, subString, start) { |
| 305 | + return string.slice(0, start) + subString + string.slice(start); |
| 306 | + }, |
272 | 307 | reduce: function(array, callback /*, initialValue*/) {
|
273 | 308 | if (this === null) {
|
274 | 309 | throw new TypeError('Array.prototype.reduce called on null or undefined');
|
|
728 | 763 | },
|
729 | 764 | format: function(value, format, roundingFunction) {
|
730 | 765 | var locale = numeral.locales[numeral.options.currentLocale],
|
731 |
| - symbolIndex = format.indexOf('$'), |
732 |
| - openParenIndex = format.indexOf('('), |
733 |
| - minusSignIndex = format.indexOf('-'), |
734 |
| - space = numeral._.includes(format, ' $') || numeral._.includes(format, '$ ') ? ' ' : '', |
735 |
| - spliceIndex, |
736 |
| - output; |
| 766 | + symbols = { |
| 767 | + before: format.match(/^([\+|\-|\(|\s|\$]*)/)[0], |
| 768 | + after: format.match(/([\+|\-|\)|\s|\$]*)$/)[0] |
| 769 | + }, |
| 770 | + output, |
| 771 | + symbol, |
| 772 | + i; |
737 | 773 |
|
738 | 774 | // strip format of spaces and $
|
739 | 775 | format = format.replace(/\s?\$\s?/, '');
|
740 | 776 |
|
741 | 777 | // format the number
|
742 | 778 | output = numeral._.numberToFormat(value, format, roundingFunction);
|
743 | 779 |
|
744 |
| - // position the symbol |
745 |
| - if (symbolIndex <= 1) { |
746 |
| - if (numeral._.includes(output, '(') || numeral._.includes(output, '-')) { |
747 |
| - output = output.split(''); |
748 |
| - |
749 |
| - spliceIndex = symbolIndex < openParenIndex || symbolIndex < minusSignIndex ? 0 : 1; |
| 780 | + // update the before and after based on value |
| 781 | + if (value >= 0) { |
| 782 | + symbols.before = symbols.before.replace(/[\-\(]/, ''); |
| 783 | + symbols.after = symbols.after.replace(/[\-\)]/, ''); |
| 784 | + } else if (value < 0 && (!numeral._.includes(symbols.before, '-') && !numeral._.includes(symbols.before, '('))) { |
| 785 | + symbols.before = '-' + symbols.before; |
| 786 | + } |
750 | 787 |
|
751 |
| - output.splice(spliceIndex, 0, locale.currency.symbol + space); |
| 788 | + // loop through each before symbol |
| 789 | + for (i = 0; i < symbols.before.length; i++) { |
| 790 | + symbol = symbols.before[i]; |
752 | 791 |
|
753 |
| - output = output.join(''); |
754 |
| - } else { |
755 |
| - output = locale.currency.symbol + space + output; |
| 792 | + switch (symbol) { |
| 793 | + case '$': |
| 794 | + output = numeral._.insert(output, locale.currency.symbol, i); |
| 795 | + break; |
| 796 | + case ' ': |
| 797 | + output = numeral._.insert(output, ' ', i); |
| 798 | + break; |
756 | 799 | }
|
757 |
| - } else { |
758 |
| - if (numeral._.includes(output, ')')) { |
759 |
| - output = output.split(''); |
| 800 | + } |
760 | 801 |
|
761 |
| - output.splice(-1, 0, space + locale.currency.symbol); |
| 802 | + // loop through each after symbol |
| 803 | + for (i = symbols.after.length - 1; i >= 0; i--) { |
| 804 | + symbol = symbols.after[i]; |
762 | 805 |
|
763 |
| - output = output.join(''); |
764 |
| - } else { |
765 |
| - output = output + space + locale.currency.symbol; |
| 806 | + switch (symbol) { |
| 807 | + case '$': |
| 808 | + output = i === symbols.after.length - 1 ? output + locale.currency.symbol : numeral._.insert(output, locale.currency.symbol, -(symbols.after.length - (1 + i))); |
| 809 | + break; |
| 810 | + case ' ': |
| 811 | + output = i === symbols.after.length - 1 ? output + ' ' : numeral._.insert(output, ' ', -(symbols.after.length - (1 + i))); |
| 812 | + break; |
766 | 813 | }
|
767 | 814 | }
|
768 | 815 |
|
| 816 | + |
769 | 817 | return output;
|
770 | 818 | }
|
771 | 819 | });
|
|
0 commit comments