|
1
|
|
|
|
|
2
|
|
|
function getFirstTwoDigitsOfNumberString( num ) { |
|
3
|
|
|
switch ( num.length ) { |
|
4
|
|
|
case 1: |
|
5
|
|
|
return num + '0'; |
|
6
|
|
|
case 2: |
|
7
|
|
|
return num; |
|
8
|
|
|
default: |
|
9
|
|
|
return num.substr( 0, 2 ); |
|
10
|
|
|
} |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
function stringToNumber ( s ) { |
|
14
|
|
|
if ( s.match( /[^-0-9]/ ) ) { |
|
15
|
|
|
return Number.NaN; |
|
16
|
|
|
} |
|
17
|
|
|
return parseInt( s, 10 ); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
var objectAssign = require( 'object-assign' ), |
|
21
|
|
|
CurrencyFormatter = { |
|
22
|
|
|
decimalDelimiter: '.', |
|
23
|
|
|
/** |
|
24
|
|
|
* @param {Number} value |
|
25
|
|
|
* @return {string} |
|
26
|
|
|
*/ |
|
27
|
|
|
format: function ( value ) { |
|
28
|
|
|
var decimals = value % 100; |
|
29
|
|
|
if ( decimals < 10 ) { |
|
30
|
|
|
decimals = '0' + decimals; |
|
31
|
|
|
} |
|
32
|
|
|
return Math.floor( value / 100 ) + this.decimalDelimiter + decimals; |
|
33
|
|
|
} |
|
34
|
|
|
}, |
|
35
|
|
|
CurrencyParser = { |
|
36
|
|
|
decimalDelimiter: '.', |
|
37
|
|
|
allowDecimals: true, |
|
38
|
|
|
parse: function ( value ) { |
|
39
|
|
|
var strParts = value.split( this.decimalDelimiter ), parts; |
|
40
|
|
|
|
|
41
|
|
|
if ( strParts.length < 2 || this.allowDecimals === false ) { |
|
42
|
|
|
strParts[1] = '00'; |
|
43
|
|
|
} else { |
|
44
|
|
|
strParts[1] = getFirstTwoDigitsOfNumberString( strParts[1] ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
parts = strParts.map( stringToNumber ); |
|
48
|
|
|
|
|
49
|
|
|
if ( isNaN( parts[0] ) || isNaN( parts[1] ) || parts.length > 2 ) { |
|
50
|
|
|
throw new Error( 'Invalid number' ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return parts[0] * 100 + parts[1]; |
|
54
|
|
|
}, |
|
55
|
|
|
getDecimalDelimiter: function () { |
|
56
|
|
|
return this.decimalDelimiter; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
; |
|
60
|
|
|
|
|
61
|
|
|
module.exports = { |
|
62
|
|
|
createCurrencyFormatter: function ( locale ) { |
|
63
|
|
|
switch ( locale ) { |
|
64
|
|
|
case 'de': |
|
65
|
|
|
return objectAssign( Object.create( CurrencyFormatter ), { |
|
66
|
|
|
decimalDelimiter: ',' |
|
67
|
|
|
} ); |
|
68
|
|
|
case 'en': |
|
69
|
|
|
return Object.create( CurrencyFormatter ); |
|
70
|
|
|
default: |
|
71
|
|
|
throw new Error( 'Unsupported locale: ' + locale ); |
|
72
|
|
|
} |
|
73
|
|
|
}, |
|
74
|
|
|
createCurrencyParser: function ( locale, allowDecimals ) { |
|
75
|
|
|
switch ( locale ) { |
|
76
|
|
|
case 'de': |
|
77
|
|
|
return objectAssign( Object.create( CurrencyParser ), { |
|
78
|
|
|
decimalDelimiter: ',', |
|
79
|
|
|
allowDecimals: typeof allowDecimals === 'boolean' ? allowDecimals : true |
|
80
|
|
|
} ); |
|
81
|
|
|
case 'en': |
|
82
|
|
|
return objectAssign( Object.create( CurrencyParser ), { |
|
83
|
|
|
allowDecimals: typeof allowDecimals === 'boolean' ? allowDecimals : true |
|
84
|
|
|
} ); |
|
85
|
|
|
default: |
|
86
|
|
|
throw new Error( 'Unsupported locale: ' + locale ); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
}; |
|
90
|
|
|
|