|
| 1 | +import { NumberFormat } from '../../src/utils' |
| 2 | + |
| 3 | +describe('should not throw error on empty config', () => { |
| 4 | + expect(() => new NumberFormat({ |
| 5 | + prefix: '$', |
| 6 | + separator: '.', |
| 7 | + decimal: ',', |
| 8 | + null_value: '', |
| 9 | + })).not.toThrow() |
| 10 | +}) |
| 11 | +describe('when the value is invalid with custom config', () => { |
| 12 | + const numberFormat = new NumberFormat({ |
| 13 | + prefix: '$', |
| 14 | + separator: '.', |
| 15 | + decimal: ',', |
| 16 | + null_value: '', |
| 17 | + }) |
| 18 | + it('should return as follows', () => { |
| 19 | + expect(numberFormat.format('')).toEqual('') |
| 20 | + expect(numberFormat.format('foo')).toEqual('$0') |
| 21 | + expect(numberFormat.format('-foo')).toEqual('$0') |
| 22 | + expect(numberFormat.format('-fo,o-')).toEqual('$0,') |
| 23 | + expect(numberFormat.format('-fo.o-')).toEqual('$0') |
| 24 | + expect(numberFormat.format('!@#$%^&*()')).toEqual('$0') |
| 25 | + }) |
| 26 | + it('should return as follows', () => { |
| 27 | + expect(numberFormat.clean().format('')).toEqual('') |
| 28 | + expect(numberFormat.clean().format('foo')).toEqual('$0') |
| 29 | + expect(numberFormat.clean().format('-foo')).toEqual('$0') |
| 30 | + expect(numberFormat.clean().format('-fo.o-')).toEqual('$0') |
| 31 | + expect(numberFormat.clean().format('-fo,o-')).toEqual('$0') |
| 32 | + expect(numberFormat.clean().format('!@#$%^&*()')).toEqual('$0') |
| 33 | + }) |
| 34 | + it('should return as follows', () => { |
| 35 | + expect(numberFormat.unformat('')).toEqual('') |
| 36 | + expect(numberFormat.unformat('foo')).toEqual('0') |
| 37 | + expect(numberFormat.unformat('-foo')).toEqual('0') |
| 38 | + expect(numberFormat.unformat('-fo,o-')).toEqual('0') |
| 39 | + expect(numberFormat.unformat('!@#$%^&*()')).toEqual('0') |
| 40 | + }) |
| 41 | +}) |
| 42 | +describe('format when options are custom', () => { |
| 43 | + const numberFormat = new NumberFormat({ |
| 44 | + prefix: '$', |
| 45 | + separator: '.', |
| 46 | + decimal: ',', |
| 47 | + null_value: '', |
| 48 | + }) |
| 49 | + it('format string value', () => { |
| 50 | + expect(numberFormat.format('0')).toEqual('$0') |
| 51 | + expect(numberFormat.format('0,')).toEqual('$0,') |
| 52 | + expect(numberFormat.format('-0,0')).toEqual('$0,0') |
| 53 | + expect(numberFormat.format('0,10')).toEqual('$0,10') |
| 54 | + expect(numberFormat.format('0,0-')).toEqual('$0,0') |
| 55 | + expect(numberFormat.format('0,10-')).toEqual('-$0,10') |
| 56 | + expect(numberFormat.format('12.345,54921')).toEqual('$12.345,54') |
| 57 | + expect(numberFormat.format('--12.345,12345')).toEqual('-$12.345,12') |
| 58 | + expect(numberFormat.format('12.345.54321,12945')).toEqual('$1.234.554.321,12') |
| 59 | + expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54') |
| 60 | + }) |
| 61 | + it('format numerical value', () => { |
| 62 | + expect(numberFormat.format(0)).toEqual('$0,00') |
| 63 | + expect(numberFormat.format(0.)).toEqual('$0,00') |
| 64 | + expect(numberFormat.format(0.0)).toEqual('$0,00') |
| 65 | + expect(numberFormat.format(-0.10)).toEqual('-$0,10') |
| 66 | + expect(numberFormat.format(-0.0)).toEqual('$0,00') |
| 67 | + expect(numberFormat.format(0.10)).toEqual('$0,10') |
| 68 | + expect(numberFormat.format(12345.54921)).toEqual('$12.345,55') |
| 69 | + expect(numberFormat.format(12345.12345)).toEqual('$12.345,12') |
| 70 | + expect(numberFormat.format(12345.54321)).toEqual('$12.345,54') |
| 71 | + expect(numberFormat.format(12345.54321)).toEqual('$12.345,54') |
| 72 | + }) |
| 73 | + it('format and clean numerical value', () => { |
| 74 | + expect(numberFormat.clean().format(0)).toEqual('$0') |
| 75 | + expect(numberFormat.clean().format(0.)).toEqual('$0') |
| 76 | + expect(numberFormat.clean().format(0.0)).toEqual('$0') |
| 77 | + expect(numberFormat.clean().format(0.10)).toEqual('$0,1') |
| 78 | + expect(numberFormat.clean().format(-0.0)).toEqual('$0') |
| 79 | + expect(numberFormat.clean().format(-0.10)).toEqual('-$0,1') |
| 80 | + expect(numberFormat.clean().format(12345.54921)).toEqual('$12.345,55') |
| 81 | + expect(numberFormat.clean().format(12345.12345)).toEqual('$12.345,12') |
| 82 | + expect(numberFormat.clean().format(12345.54321)).toEqual('$12.345,54') |
| 83 | + expect(numberFormat.clean().format(12345.54321)).toEqual('$12.345,54') |
| 84 | + }) |
| 85 | +}) |
| 86 | +describe('unformat when options are default', () => { |
| 87 | + const numberFormat = new NumberFormat({ |
| 88 | + prefix: '$', |
| 89 | + separator: '.', |
| 90 | + decimal: ',', |
| 91 | + null_value: '', |
| 92 | + }) |
| 93 | + it('unformat string value', () => { |
| 94 | + expect(numberFormat.unformat('0')).toEqual('0') |
| 95 | + expect(numberFormat.unformat('0,')).toEqual('0') |
| 96 | + expect(numberFormat.unformat('-0,0')).toEqual('0') |
| 97 | + expect(numberFormat.unformat('0,10')).toEqual('0.1') |
| 98 | + expect(numberFormat.unformat('0,0-')).toEqual('0') |
| 99 | + expect(numberFormat.unformat('0,10-')).toEqual('-0.1') |
| 100 | + expect(numberFormat.unformat('12.345,54921')).toEqual('12345.54') |
| 101 | + expect(numberFormat.unformat('--12.345,12345')).toEqual('-12345.12') |
| 102 | + expect(numberFormat.unformat('12.345.54321,12345')).toEqual('1234554321.12') |
| 103 | + expect(numberFormat.unformat('-12.345,,54321-')).toEqual('-12345.54') |
| 104 | + }) |
| 105 | + it('unformat numerical value', () => { |
| 106 | + expect(numberFormat.unformat(0)).toEqual('0') |
| 107 | + expect(numberFormat.unformat(0.)).toEqual('0') |
| 108 | + expect(numberFormat.unformat(0.0)).toEqual('0') |
| 109 | + expect(numberFormat.unformat(-0.10)).toEqual('-0.1') |
| 110 | + expect(numberFormat.unformat(-0.0)).toEqual('0') |
| 111 | + expect(numberFormat.unformat(0.10)).toEqual('0.1') |
| 112 | + expect(numberFormat.unformat(12345.54921)).toEqual('12345.55') |
| 113 | + expect(numberFormat.unformat(12345.12345)).toEqual('12345.12') |
| 114 | + expect(numberFormat.unformat(12345.54321)).toEqual('12345.54') |
| 115 | + expect(numberFormat.unformat(12345.54321)).toEqual('12345.54') |
| 116 | + }) |
| 117 | + it('unformat and clean numerical value', () => { |
| 118 | + expect(numberFormat.clean().unformat(0)).toEqual('0') |
| 119 | + expect(numberFormat.clean().unformat(0.)).toEqual('0') |
| 120 | + expect(numberFormat.clean().unformat(0.0)).toEqual('0') |
| 121 | + expect(numberFormat.clean().unformat(0.10)).toEqual('0.1') |
| 122 | + expect(numberFormat.clean().unformat(-0.0)).toEqual('0') |
| 123 | + expect(numberFormat.clean().unformat(-0.10)).toEqual('-0.1') |
| 124 | + expect(numberFormat.clean().unformat(12345.54921)).toEqual('12345.55') |
| 125 | + expect(numberFormat.clean().unformat(12345.12345)).toEqual('12345.12') |
| 126 | + expect(numberFormat.clean().unformat(12345.54321)).toEqual('12345.54') |
| 127 | + expect(numberFormat.clean().unformat(12345.54321)).toEqual('12345.54') |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | + |
0 commit comments