|
1
|
|
|
import _ from 'underscore'; |
|
|
|
|
|
|
2
|
|
|
import { |
|
3
|
|
|
CellFormatter |
|
4
|
|
|
} from './cell.js'; |
|
5
|
|
|
/** |
|
6
|
|
|
A floating point number formatter. Doesn't understand scientific notation at |
|
7
|
|
|
the moment. |
|
8
|
|
|
|
|
9
|
|
|
@class Backgrid.NumberFormatter |
|
10
|
|
|
@extends Backgrid.CellFormatter |
|
11
|
|
|
@constructor |
|
12
|
|
|
@throws {RangeError} If decimals < 0 or > 20. |
|
13
|
|
|
*/ |
|
14
|
|
|
var NumberFormatter = function (options) { |
|
15
|
|
|
_.extend(this, this.defaults, options || {}); |
|
16
|
|
|
|
|
17
|
|
|
if (this.decimals < 0 || this.decimals > 20) { |
|
18
|
|
|
throw new RangeError("decimals must be between 0 and 20"); |
|
19
|
|
|
} |
|
20
|
|
|
}; |
|
21
|
|
|
NumberFormatter.prototype = new CellFormatter(); |
|
22
|
|
|
_.extend(NumberFormatter.prototype, { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
@member Backgrid.NumberFormatter |
|
26
|
|
|
@cfg {Object} options |
|
27
|
|
|
|
|
28
|
|
|
@cfg {number} [options.decimals=2] Number of decimals to display. Must be an integer. |
|
29
|
|
|
|
|
30
|
|
|
@cfg {string} [options.decimalSeparator='.'] The separator to use when |
|
31
|
|
|
displaying decimals. |
|
32
|
|
|
|
|
33
|
|
|
@cfg {string} [options.orderSeparator=','] The separator to use to |
|
34
|
|
|
separator thousands. May be an empty string. |
|
35
|
|
|
*/ |
|
36
|
|
|
defaults: { |
|
37
|
|
|
decimals: 2, |
|
38
|
|
|
decimalSeparator: '.', |
|
39
|
|
|
orderSeparator: ',' |
|
40
|
|
|
}, |
|
41
|
|
|
|
|
42
|
|
|
HUMANIZED_NUM_RE: /(\d)(?=(?:\d{3})+$)/g, |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
Takes a floating point number and convert it to a formatted string where |
|
46
|
|
|
every thousand is separated by `orderSeparator`, with a `decimal` number of |
|
47
|
|
|
decimals separated by `decimalSeparator`. The number returned is rounded |
|
48
|
|
|
the usual way. |
|
49
|
|
|
|
|
50
|
|
|
@member Backgrid.NumberFormatter |
|
51
|
|
|
@param {number} number |
|
52
|
|
|
@param {Backbone.Model} model Used for more complicated formatting |
|
53
|
|
|
@return {string} |
|
54
|
|
|
*/ |
|
55
|
|
|
fromRaw: function (number, model) { |
|
|
|
|
|
|
56
|
|
|
if (_.isNull(number) || _.isUndefined(number)) return ''; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
number = parseFloat(number).toFixed(~~this.decimals); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
var parts = number.split('.'); |
|
61
|
|
|
var integerPart = parts[0]; |
|
62
|
|
|
var decimalPart = parts[1] ? (this.decimalSeparator || '.') + parts[1] : ''; |
|
63
|
|
|
|
|
64
|
|
|
return integerPart.replace(this.HUMANIZED_NUM_RE, '$1' + this.orderSeparator) + decimalPart; |
|
65
|
|
|
}, |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
Takes a string, possibly formatted with `orderSeparator` and/or |
|
69
|
|
|
`decimalSeparator`, and convert it back to a number. |
|
70
|
|
|
|
|
71
|
|
|
@member Backgrid.NumberFormatter |
|
72
|
|
|
@param {string} formattedData |
|
73
|
|
|
@param {Backbone.Model} model Used for more complicated formatting |
|
74
|
|
|
@return {number|undefined} Undefined if the string cannot be converted to |
|
75
|
|
|
a number. |
|
76
|
|
|
*/ |
|
77
|
|
|
toRaw: function (formattedData, model) { |
|
|
|
|
|
|
78
|
|
|
formattedData = formattedData.trim(); |
|
79
|
|
|
|
|
80
|
|
|
if (formattedData === '') return null; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
var rawData = ''; |
|
83
|
|
|
|
|
84
|
|
|
var thousands = formattedData.split(this.orderSeparator); |
|
85
|
|
|
for (var i = 0; i < thousands.length; i++) { |
|
86
|
|
|
rawData += thousands[i]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
var decimalParts = rawData.split(this.decimalSeparator); |
|
90
|
|
|
rawData = ''; |
|
91
|
|
|
for (var i = 0; i < decimalParts.length; i++) { |
|
|
|
|
|
|
92
|
|
|
rawData = rawData + decimalParts[i] + '.'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (rawData[rawData.length - 1] === '.') { |
|
96
|
|
|
rawData = rawData.slice(0, rawData.length - 1); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
var result = (rawData * 1).toFixed(~~this.decimals) * 1; |
|
|
|
|
|
|
100
|
|
|
if (_.isNumber(result) && !_.isNaN(result)) return result; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
}); |
|
104
|
|
|
export { |
|
105
|
|
|
NumberFormatter |
|
106
|
|
|
}; |
|
107
|
|
|
|