forked from componitable/format-number
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
267 lines (241 loc) · 8.58 KB
/
index.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
module.exports = formatter;
module.exports.default = formatter;
function formatter(options) {
options = options || {};
// *********************************************************************************************
// Set defaults for negatives
// options.negative, options.negativeOut, options.separator retained for backward compatibility
// *********************************************************************************************
// type of negative; default left
options.negativeType = options.negativeType || (options.negative === 'R' ? 'right' : 'left')
// negative symbols '-' or '()'
if (typeof options.negativeLeftSymbol !== 'string') {
switch (options.negativeType) {
case 'left':
options.negativeLeftSymbol = '-';
break;
case 'brackets':
options.negativeLeftSymbol = '(';
break;
default:
options.negativeLeftSymbol = '';
}
}
if (typeof options.negativeRightSymbol !== 'string') {
switch (options.negativeType) {
case 'right':
options.negativeRightSymbol = '-';
break;
case 'brackets':
options.negativeRightSymbol = ')';
break;
default:
options.negativeRightSymbol = '';
}
}
// whether negative symbol should be inside/outside prefix and suffix
if (typeof options.negativeLeftOut !== "boolean") {
options.negativeLeftOut = (options.negativeOut === false ? false : true);
}
if (typeof options.negativeRightOut !== "boolean") {
options.negativeRightOut = (options.negativeOut === false ? false : true);
}
//prefix and suffix
options.prefix = options.prefix || '';
options.suffix = options.suffix || '';
//separators
if (typeof options.integerSeparator !== 'string') {
options.integerSeparator = (typeof options.separator === 'string' ? options.separator : ',');
}
options.decimalsSeparator = typeof options.decimalsSeparator === 'string' ? options.decimalsSeparator : '';
options.decimal = options.decimal || '.';
//padders
options.padLeft = options.padLeft || -1 //default no padding
options.padRight = options.padRight || -1 //default no padding
function format(number, overrideOptions) {
overrideOptions = overrideOptions || {};
if (number || number === 0) {
if (typeof number === 'number') {
//get rid of the possible "e-"
var parts = number.toString().split('e-');
if (parts.length === 2) {
var digits = (parts[0].replace('.', '').replace('-', '')).length - 1 + Number(parts[1]);
//maximum number of digits is 20
//see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
digits = digits > 20 ? 20 : digits;
number = number.toFixed(digits);
}
}
number = '' + number;//convert number to string if it isn't already
} else {
return '';
}
//identify a negative number and make it absolute
var output = [];
var negative = number.charAt(0) === '-';
number = number.replace(/^\-/g, '');
//Prepare output with left hand negative and/or prefix
if (!options.negativeLeftOut && !overrideOptions.noUnits) {
output.push(options.prefix);
}
if (negative) {
output.push(options.negativeLeftSymbol);
}
if (options.negativeLeftOut && !overrideOptions.noUnits) {
output.push(options.prefix);
}
//Format core number
number = number.split('.');
if (options.round != null) round(number, options.round);
if (options.truncate != null) number[1] = truncate(number[1], options.truncate);
if (options.padLeft > 0) number[0] = padLeft(number[0], options.padLeft);
if (options.padRight > 0) number[1] = padRight(number[1], options.padRight);
if (!overrideOptions.noSeparator && number[1]) number[1] = addDecimalSeparators(number[1], options.decimalsSeparator);
if (!overrideOptions.noSeparator && number[0]) number[0] = addIntegerSeparators(number[0], options.integerSeparator);
output.push(number[0]);
if (number[1]) {
output.push(options.decimal);
output.push(number[1]);
}
//Prepare output with right hand negative and/or prefix
if (options.negativeRightOut && !overrideOptions.noUnits) {
output.push(options.suffix);
}
if (negative) {
output.push(options.negativeRightSymbol);
}
if (!options.negativeRightOut && !overrideOptions.noUnits) {
output.push(options.suffix);
}
//join output and return
return output.join('');
}
format.negative = options.negative;
format.negativeOut = options.negativeOut;
format.negativeType = options.negativeType;
format.negativeLeftOut = options.negativeLeftOut;
format.negativeLeftSymbol = options.negativeLeftSymbol;
format.negativeRightOut = options.negativeRightOut;
format.negativeRightSymbol = options.negativeRightSymbol;
format.prefix = options.prefix;
format.suffix = options.suffix;
format.separate = options.separate;
format.integerSeparator = options.integerSeparator;
format.decimalsSeparator = options.decimalsSeparator;
format.decimal = options.decimal;
format.padLeft = options.padLeft;
format.padRight = options.padRight;
format.truncate = options.truncate;
format.round = options.round;
function unformat(number, allowedSeparators) {
allowedSeparators = allowedSeparators || [];
if (options.allowedSeparators) {
options.allowedSeparators.forEach(function (s) { allowedSeparators.push (s); });
}
allowedSeparators.push(options.integerSeparator);
allowedSeparators.push(options.decimalsSeparator);
number = number.replace(options.prefix, '');
number = number.replace(options.suffix, '');
var newNumber = number;
do {
number = newNumber;
for (var i = 0; i < allowedSeparators.length; i++) {
newNumber = newNumber.replace(allowedSeparators[i], '');
}
} while (newNumber != number);
return number;
}
format.unformat = unformat;
function validate(number, allowedSeparators) {
number = unformat(number, allowedSeparators);
number = number.split(options.decimal);
if (number.length > 2) {
return false;
} else if (options.truncate != null && number[1] && number[1].length > options.truncate) {
return false;
} else if (options.round != null && number[1] && number[1].length > options.round) {
return false;
} else {
return /^-?\d+\.?\d*$/.test(number);
}
}
return format;
}
//where x is already the integer part of the number
function addIntegerSeparators(x, separator) {
x += '';
if (!separator) return x;
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x)) {
x = x.replace(rgx, '$1' + separator + '$2');
}
return x;
}
//where x is already the decimal part of the number
function addDecimalSeparators(x, separator) {
x += '';
if (!separator) return x;
var rgx = /(\d{3})(\d+)/;
while (rgx.test(x)) {
x = x.replace(rgx, '$1' + separator + '$2');
}
return x;
}
//where x is the integer part of the number
function padLeft(x, padding) {
x = x + '';
var buf = [];
while (buf.length + x.length < padding) {
buf.push('0');
}
return buf.join('') + x;
}
//where x is the decimals part of the number
function padRight(x, padding) {
if (x) {
x += '';
} else {
x = '';
}
var buf = [];
while (buf.length + x.length < padding) {
buf.push('0');
}
return x + buf.join('');
}
function truncate(x, length) {
if (x) {
x += '';
}
if (x && x.length > length) {
return x.substr(0, length);
} else {
return x;
}
}
//where number is an array with 0th item as integer string and 1st item as decimal string (no negatives)
function round(number, places) {
if (number[1] && places >= 0 && number[1].length > places) {
//truncate to correct number of decimal places
var decim = number[1].slice(0, places);
//if next digit was >= 5 we need to round up
if (+(number[1].substr(places, 1)) >= 5) {
//But first count leading zeros as converting to a number will loose them
var leadingzeros = "";
while (decim.charAt(0)==="0") {
leadingzeros = leadingzeros + "0";
decim = decim.substr(1);
}
//Then we can change decim to a number and add 1 before replacing leading zeros
decim = (+decim + 1) + '';
decim = leadingzeros + decim;
if (decim.length > places) {
//adding one has made it longer
number[0] = (+number[0]+ +decim.charAt(0)) + ''; //add value of firstchar to the integer part
decim = decim.substring(1); //ignore the 1st char at the beginning which is the carry to the integer part
}
}
number[1] = decim;
}
return number;
}