|
| 1 | +import NumberFormat from '../../src/number-format' |
| 2 | + |
| 3 | +test('when the value is invalid with custom config', () => { |
| 4 | + const numberFormat = new NumberFormat({ |
| 5 | + prefix: '$', |
| 6 | + separator: '.', |
| 7 | + decimal: ',', |
| 8 | + nullValue: '0' |
| 9 | + }) |
| 10 | + test('should return as follows', () => { |
| 11 | + expect(numberFormat.format('')).toEqual('') |
| 12 | + expect(numberFormat.format('foo')).toEqual('') |
| 13 | + expect(numberFormat.format('-foo')).toEqual('') |
| 14 | + expect(numberFormat.format('-fo,o-')).toEqual('') |
| 15 | + expect(numberFormat.format('-fo.o-')).toEqual('') |
| 16 | + expect(numberFormat.format('!@#$%^&*()')).toEqual('') |
| 17 | + }) |
| 18 | + test('should return as follows', () => { |
| 19 | + expect(numberFormat.clean(true).unformat('')).toEqual('') |
| 20 | + expect(numberFormat.clean(true).unformat('foo')).toEqual('') |
| 21 | + expect(numberFormat.clean(true).unformat('-foo')).toEqual('') |
| 22 | + expect(numberFormat.clean(true).unformat('-fo.o-')).toEqual('') |
| 23 | + expect(numberFormat.clean(true).unformat('!@#$%^&*()')).toEqual('') |
| 24 | + }) |
| 25 | +}) |
| 26 | +test('format when options are custom', () => { |
| 27 | + const numberFormat = new NumberFormat({ |
| 28 | + prefix: '$', |
| 29 | + separator: '.', |
| 30 | + decimal: ',', |
| 31 | + nullValue: '' |
| 32 | + }) |
| 33 | + test('format string value', () => { |
| 34 | + expect(numberFormat.format('')).toEqual('') |
| 35 | + expect(numberFormat.format('0')).toEqual('$0') |
| 36 | + expect(numberFormat.format('0,')).toEqual('$0') |
| 37 | + expect(numberFormat.format('-0,0')).toEqual('$0') |
| 38 | + expect(numberFormat.format('0,10')).toEqual('$0,1') |
| 39 | + expect(numberFormat.format('0,0-')).toEqual('$0') |
| 40 | + expect(numberFormat.format('0,10-')).toEqual('-$0,1') |
| 41 | + expect(numberFormat.format('12.345,54921')).toEqual('$12.345,55') |
| 42 | + expect(numberFormat.format('--12.345,12345')).toEqual('-$12.345,12') |
| 43 | + expect(numberFormat.format('12.345.54321,12945')).toEqual('$1.234.554.321,13') |
| 44 | + expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54') |
| 45 | + }) |
| 46 | + test('format numerical value', () => { |
| 47 | + expect(numberFormat.format(0)).toEqual('$0') |
| 48 | + expect(numberFormat.format(0)).toEqual('$0') |
| 49 | + expect(numberFormat.format(0.0)).toEqual('$0') |
| 50 | + expect(numberFormat.format(-0.1)).toEqual('-$0,1') |
| 51 | + expect(numberFormat.format(-0.0)).toEqual('$0') |
| 52 | + expect(numberFormat.format(0.1)).toEqual('$0,1') |
| 53 | + expect(numberFormat.format(12345.54921)).toEqual('$12.345,55') |
| 54 | + expect(numberFormat.format(12345.12345)).toEqual('$12.345,12') |
| 55 | + expect(numberFormat.format(12345.54321)).toEqual('$12.345,54') |
| 56 | + expect(numberFormat.format(12345.54321)).toEqual('$12.345,54') |
| 57 | + }) |
| 58 | +}) |
0 commit comments