From b486262239ae364e7914aec83d0b2ad18cd6aba4 Mon Sep 17 00:00:00 2001 From: Ben-hur Santos Ott Date: Fri, 10 Nov 2017 22:36:00 -0200 Subject: [PATCH] fix(money-mask): fixing format for number with one decimal place --- README.md | 3 + __tests__/money.mask.test.js | 256 ++++++++++++++++++++--------------- lib/masks/money.mask.js | 58 ++++---- package-lock.json | 2 +- package.json | 2 +- 5 files changed, 183 insertions(+), 138 deletions(-) diff --git a/README.md b/README.md index 3dab89fa..f52eed29 100644 --- a/README.md +++ b/README.md @@ -404,6 +404,9 @@ var money = MaskService.toMask('money', '123', { # Changelog +## 1.6.4 +* Fixing wrong format when use money mask and values with only one decimal place (`1.9`) (thanks to [Pablo](https://github.com/rochapablo)). + ## 1.6.3 * Fixing moment version (thanks to [Edward Coleridge Smith](https://github.com/edcs)) * Adding pre-builded lib to improve build phase (thanks to [Giorgi Bagdavadze](https://github.com/notgiorgi)) diff --git a/__tests__/money.mask.test.js b/__tests__/money.mask.test.js index 74642511..50a135f7 100644 --- a/__tests__/money.mask.test.js +++ b/__tests__/money.mask.test.js @@ -1,198 +1,230 @@ -import { MoneyMask } from '../lib/masks'; +import { MoneyMask } from '../lib/masks' test('getType results money', () => { - var expected = 'money'; - var received = MoneyMask.getType(); + var expected = 'money' + var received = MoneyMask.getType() - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('1 results R$0,01', () => { - var mask = new MoneyMask(); - var expected = 'R$0,01'; - var received = mask.getValue('1'); + var mask = new MoneyMask() + var expected = 'R$0,01' + var received = mask.getValue('1') - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('111 results R$1,11', () => { - var mask = new MoneyMask(); - var expected = 'R$1,11'; - var received = mask.getValue('111'); + var mask = new MoneyMask() + var expected = 'R$1,11' + var received = mask.getValue('111') - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('1111 results R$11,11', () => { - var mask = new MoneyMask(); - var expected = 'R$11,11'; - var received = mask.getValue('1111'); + var mask = new MoneyMask() + var expected = 'R$11,11' + var received = mask.getValue('1111') - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('11111 results R$111,11', () => { - var mask = new MoneyMask(); - var expected = 'R$111,11'; - var received = mask.getValue('11111'); + var mask = new MoneyMask() + var expected = 'R$111,11' + var received = mask.getValue('11111') - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('111111 results R$1.111,11', () => { - var mask = new MoneyMask(); - var expected = 'R$1.111,11'; - var received = mask.getValue('111111'); + var mask = new MoneyMask() + var expected = 'R$1.111,11' + var received = mask.getValue('111111') - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('111111111 results R$1.111.111,11', () => { - var mask = new MoneyMask(); - var expected = 'R$1.111.111,11'; - var received = mask.getValue('111111111'); + var mask = new MoneyMask() + var expected = 'R$1.111.111,11' + var received = mask.getValue('111111111') - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test(' results R$0,00', () => { - var mask = new MoneyMask(); - var expected = 'R$0,00'; - var received = mask.getValue(''); + var mask = new MoneyMask() + var expected = 'R$0,00' + var received = mask.getValue('') - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('11111 precision 3 results R$11,111', () => { - var mask = new MoneyMask(); - var expected = 'R$11,111'; + var mask = new MoneyMask() + var expected = 'R$11,111' var received = mask.getValue('11111', { precision: 3 - }); + }) - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('111 separator . results R$1.11', () => { - var mask = new MoneyMask(); - var expected = 'R$1.11'; + var mask = new MoneyMask() + var expected = 'R$1.11' var received = mask.getValue('111', { separator: '.' - }); + }) - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('111111 delimiter , results R$1,111,11', () => { - var mask = new MoneyMask(); - var expected = 'R$1,111,11'; + var mask = new MoneyMask() + var expected = 'R$1,111,11' var received = mask.getValue('111111', { delimiter: ',' - }); + }) - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('1 unit US$ results US$0,01', () => { - var mask = new MoneyMask(); - var expected = 'US$0,01'; + var mask = new MoneyMask() + var expected = 'US$0,01' var received = mask.getValue('1', { unit: 'US$' - }); + }) - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('1 suffixUnit $$$ results R$0,01', () => { - var mask = new MoneyMask(); - var expected = 'R$0,01 $$$'; + var mask = new MoneyMask() + var expected = 'R$0,01 $$$' var received = mask.getValue('1', { suffixUnit: '$$$' - }); + }) - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('1 zeroCents results R$1,00', () => { - var mask = new MoneyMask(); - var expected = 'R$1,00'; + var mask = new MoneyMask() + var expected = 'R$1,00' var received = mask.getValue('1', { zeroCents: true - }); + }) - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('US$ config with value 1234567 results US$12,345.67', () => { - var mask = new MoneyMask(); - var expected = 'US$12,345.67'; + var mask = new MoneyMask() + var expected = 'US$12,345.67' var received = mask.getValue('1234567', { unit: 'US$', delimiter: ',', separator: '.' - }); + }) - expect(received).toBe(expected); -}); + expect(received).toBe(expected) +}) test('1 results R$0,01 and raw value 0.01', () => { - var mask = new MoneyMask(); - var expected = 'R$0,01'; - var received = mask.getValue('1'); + var mask = new MoneyMask() + var expected = 'R$0,01' + var received = mask.getValue('1') - var expectedRawValue = 0.01; - var receivedRawValue = mask.getRawValue(received); + var expectedRawValue = 0.01 + var receivedRawValue = mask.getRawValue(received) - expect(received).toBe(expected); - expect(receivedRawValue).toBe(expectedRawValue); -}); + expect(received).toBe(expected) + expect(receivedRawValue).toBe(expectedRawValue) +}) test('111111 results R$1.111,11 and raw value 1111.11', () => { - var mask = new MoneyMask(); - var expected = 'R$1.111,11'; - var received = mask.getValue('111111'); + var mask = new MoneyMask() + var expected = 'R$1.111,11' + var received = mask.getValue('111111') - var expectedRawValue = 1111.11; - var receivedRawValue = mask.getRawValue(received); + var expectedRawValue = 1111.11 + var receivedRawValue = mask.getRawValue(received) - expect(received).toBe(expected); - expect(receivedRawValue).toBe(expectedRawValue); -}); + expect(received).toBe(expected) + expect(receivedRawValue).toBe(expectedRawValue) +}) test('1 zeroCents results R$1,00 and raw value 1', () => { - var mask = new MoneyMask(); - var expected = 'R$1,00'; + var mask = new MoneyMask() + var expected = 'R$1,00' var received = mask.getValue('1', { zeroCents: true - }); + }) - var expectedRawValue = 1; - var receivedRawValue = mask.getRawValue(received); + var expectedRawValue = 1 + var receivedRawValue = mask.getRawValue(received) - expect(received).toBe(expected); - expect(receivedRawValue).toBe(expectedRawValue); -}); + expect(received).toBe(expected) + expect(receivedRawValue).toBe(expectedRawValue) +}) test('111111 delimiter , results R$1,111,11 and raw value 1111.11', () => { - var mask = new MoneyMask(); - var expected = 'R$1,111,11'; + var mask = new MoneyMask() + var expected = 'R$1,111,11' var received = mask.getValue('111111', { delimiter: ',' - }); + }) - var expectedRawValue = 1111.11; - var receivedRawValue = mask.getRawValue(received); + var expectedRawValue = 1111.11 + var receivedRawValue = mask.getRawValue(received) - expect(received).toBe(expected); - expect(receivedRawValue).toBe(expectedRawValue); -}); + expect(received).toBe(expected) + expect(receivedRawValue).toBe(expectedRawValue) +}) test('1 unit US$ results US$ 0,01', () => { - var mask = new MoneyMask(); - var expected = 'US$ 0,01'; + var mask = new MoneyMask() + var expected = 'US$ 0,01' var received = mask.getValue('1', { unit: 'US$ ' - }); + }) - expect(received).toBe(expected); -}); \ No newline at end of file + expect(received).toBe(expected) +}) + +test('number 1.9 must return R$1,90', () => { + var mask = new MoneyMask() + const expected = 'R$1,90' + const received = mask.getValue(1.9) + + expect(received).toBe(expected) +}) + +test('number 0.05 must return R$0,05', () => { + var mask = new MoneyMask() + const expected = 'R$0,05' + const received = mask.getValue(0.05) + + expect(received).toBe(expected) +}) + +test('number 0.85 must return R$0,85', () => { + var mask = new MoneyMask() + const expected = 'R$0,85' + const received = mask.getValue(0.85) + + expect(received).toBe(expected) +}) + +test('number 1111.2 must return R$1.111,20', () => { + var mask = new MoneyMask() + const expected = 'R$1.111,20' + const received = mask.getValue(1111.2) + + expect(received).toBe(expected) +}) diff --git a/lib/masks/money.mask.js b/lib/masks/money.mask.js index 324192be..ff7ec28b 100644 --- a/lib/masks/money.mask.js +++ b/lib/masks/money.mask.js @@ -1,4 +1,4 @@ -import BaseMask from './_base.mask'; +import BaseMask from './_base.mask' const MONEY_MASK_SETTINGS = { precision: 2, @@ -7,51 +7,61 @@ const MONEY_MASK_SETTINGS = { unit: 'R$', suffixUnit: '', zeroCents: false -}; +} export default class MoneyMask extends BaseMask { static getType() { - return 'money'; + return 'money' } getValue(value, settings, oldValue) { - let mergedSettings = super.mergeSettings(MONEY_MASK_SETTINGS, settings); + let mergedSettings = super.mergeSettings(MONEY_MASK_SETTINGS, settings) - if (mergedSettings.suffixUnit && oldValue && value) { - // value: 123 R - // oldValue: 123 R$ + let sanitized = this._sanitize(value, mergedSettings) - if (value.length == oldValue.length - 1) { - let cleared = this.removeNotNumbers(value); - value = cleared.substr(0, cleared.length - 1); + if (mergedSettings.suffixUnit && oldValue && sanitized) { + if (sanitized.length == oldValue.length - 1) { + let cleared = this.removeNotNumbers(sanitized) + sanitized = cleared.substr(0, cleared.length - 1) } } - let masked = this.getVMasker().toMoney(value, mergedSettings); + let masked = this.getVMasker().toMoney(sanitized, mergedSettings) - return masked; + return masked } getRawValue(maskedValue, settings) { - let mergedSettings = super.mergeSettings(MONEY_MASK_SETTINGS, settings); - let normalized = super.removeNotNumbers(maskedValue); + let mergedSettings = super.mergeSettings(MONEY_MASK_SETTINGS, settings) + let normalized = super.removeNotNumbers(maskedValue) - let dotPosition = normalized.length - mergedSettings.precision; - normalized = this._insert(normalized, dotPosition, '.'); + let dotPosition = normalized.length - mergedSettings.precision + normalized = this._insert(normalized, dotPosition, '.') - return Number(normalized); + return Number(normalized) } validate(value, settings) { - return true; + return true + } + + _sanitize(value, settings) { + if (typeof value === 'number') { + return value.toFixed(settings.precision) + } + + return value } _insert(text, index, string) { if (index > 0) { - return text.substring(0, index) + string + text.substring(index, text.length); + return ( + text.substring(0, index) + + string + + text.substring(index, text.length) + ) + } else { + return string + text } - else { - return string + text; - } - }; -} \ No newline at end of file + } +} diff --git a/package-lock.json b/package-lock.json index 12fa9f67..2b89ee77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "react-native-masked-text", - "version": "1.6.2", + "version": "1.6.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 539c873b..071ac240 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-masked-text", - "version": "1.6.3", + "version": "1.6.4", "description": "Text and TextInput with mask for React Native applications", "licenses": [ {