| Total Complexity | 18 |
| Complexity/F | 3.6 |
| Lines of Code | 61 |
| Function Count | 5 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | |||
| 2 | var objectAssign = require( 'object-assign' ), |
||
| 3 | CurrencyFormatter = { |
||
| 4 | decimalDelimiter: '.', |
||
| 5 | /** |
||
| 6 | * @param {Number} value |
||
| 7 | * @return {string} |
||
| 8 | */ |
||
| 9 | format: function ( value ) { |
||
| 10 | var decimals = value % 100; |
||
| 11 | if ( decimals < 10 ) { |
||
| 12 | decimals = '0' + decimals; |
||
| 13 | } |
||
| 14 | return Math.floor( value / 100 ) + this.decimalDelimiter + decimals; |
||
| 15 | } |
||
| 16 | }, |
||
| 17 | CurrencyParser = { |
||
| 18 | decimalDelimiter: '.', |
||
| 19 | parse: function ( value ) { |
||
| 20 | var parts = value.split( this.decimalDelimiter ).map( |
||
| 21 | function ( p ) { |
||
| 22 | if ( p.match( /[^-0-9]/ ) ) { |
||
| 23 | return Number.NaN; |
||
| 24 | } |
||
| 25 | return parseInt( p, 10 ); |
||
| 26 | } |
||
| 27 | ); |
||
| 28 | if ( parts.length < 2 ) { |
||
| 29 | parts[1] = 0; |
||
| 30 | } |
||
| 31 | if ( isNaN( parts[0] ) || isNaN( parts[1] ) || parts.length > 2 || parts[1] > 100 ) { |
||
| 32 | throw new Error( 'Invalid number' ); |
||
| 33 | } |
||
| 34 | |||
| 35 | return parts[0] * 100 + parts[1]; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | ; |
||
| 39 | |||
| 40 | |||
| 41 | module.exports = { |
||
| 42 | createCurrencyFormatter: function ( locale ) { |
||
| 43 | switch ( locale ) { |
||
| 44 | case 'de': |
||
| 45 | return objectAssign( Object.create( CurrencyFormatter ), { decimalDelimiter: ',' } ); |
||
| 46 | case 'en': |
||
| 47 | return Object.create( CurrencyFormatter ); |
||
| 48 | default: |
||
| 49 | throw new Error( 'Unsupported locale: ' + locale ); |
||
| 50 | } |
||
| 51 | }, |
||
| 52 | createCurrencyParser: function ( locale ) { |
||
| 53 | switch ( locale ) { |
||
| 54 | case 'de': |
||
| 55 | return objectAssign( Object.create( CurrencyParser ), { decimalDelimiter: ',' } ); |
||
| 56 | case 'en': |
||
| 57 | return Object.create( CurrencyParser ); |
||
| 58 | default: |
||
| 59 | throw new Error( 'Unsupported locale: ' + locale ); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | }; |
||
| 63 |