diff --git a/README.md b/README.md
index f52eed29..535c6a8a 100644
--- a/README.md
+++ b/README.md
@@ -5,34 +5,36 @@ This is a simple masked text (normal text and input text) component for React-Na
Thanks to [vanilla-masker](https://github.com/BankFacil/vanilla-masker) =).
Thanks to [moment](http://momentjs.com/) =).
-
## Supported Versions
+
React-native: 0.32.0 or higher
## Install
+
`npm install react-native-masked-text --save`
## Usage (TextInputMask)
+
```jsx
-import React, {Component} from 'react';
+import React, { Component } from 'react'
// import the component
-import {TextInputMask} from 'react-native-masked-text';
+import { TextInputMask } from 'react-native-masked-text'
export default class MyComponent extends Component {
constructor(props) {
- super(props);
+ super(props)
}
isValid() {
// isValid method returns if the inputed value is valid.
// Ex: if you input 40/02/1990 30:20:20, it will return false
// because in this case, the day and the hour is invalid.
- let valid = this.refs['myDateText'].isValid();
+ let valid = this.refs['myDateText'].isValid()
// get converted value. Using type=datetime, it returns the moment object.
// If it's using type=money, it returns a Number object.
- let rawValue = this.refs['myDateText'].getRawValue();
+ let rawValue = this.refs['myDateText'].getRawValue()
}
render() {
@@ -43,31 +45,31 @@ export default class MyComponent extends Component {
type={'datetime'}
options={{
format: 'DD-MM-YYYY HH:mm:ss'
- }} />
- );
+ }}
+ />
+ )
}
}
-
```
### Props
#### type
-*credit-card*: use the mask 9999 9999 9999 9999. It accepts options (see later in this doc).
-*cpf*: use the mask `999.999.999-99` and `numeric` keyboard.
-*cnpj*: use the mask `99.999.999/9999-99` and `numeric` keyboard.
-*zip-code*: use the mask `99999-999` and `numeric` keyboard.
-*only-numbers*: accept only numbers on field with `numeric` keyboard.
-*money*: use the mask `R$ 0,00` on the field with `numeric` keyboard. It accepts options (see later in this doc).
-*cel-phone*: use the mask `(99) 9999-9999` or `(99) 99999-9999` (changing automaticaly by length). It accepts options (see later in this doc).
-*datetime*: use datetime mask with moment format (default DD/MM/YYYY HH:mm:ss). It accepts options (see later in this doc).
-*custom*: use your custom mask (see the options props later in this doc).
-
+_credit-card_: use the mask 9999 9999 9999 9999. It accepts options (see later in this doc).
+_cpf_: use the mask `999.999.999-99` and `numeric` keyboard.
+_cnpj_: use the mask `99.999.999/9999-99` and `numeric` keyboard.
+_zip-code_: use the mask `99999-999` and `numeric` keyboard.
+_only-numbers_: accept only numbers on field with `numeric` keyboard.
+_money_: use the mask `R$ 0,00` on the field with `numeric` keyboard. It accepts options (see later in this doc).
+_cel-phone_: use the mask `(99) 9999-9999` or `(99) 99999-9999` (changing automaticaly by length). It accepts options (see later in this doc).
+_datetime_: use datetime mask with moment format (default DD/MM/YYYY HH:mm:ss). It accepts options (see later in this doc).
+_custom_: use your custom mask (see the options props later in this doc).
#### onChangeText
Invoked after new value applied to mask.
+
```jsx
/**
* @param {String} text the text AFTER mask is applied.
@@ -81,7 +83,6 @@ onChangeText(text) {
onChangeText={this.onChangeText.bind(this)} />
```
-
#### checkText
Allow you to check and prevent value to be inputed.
@@ -107,17 +108,17 @@ You can use this prop if you want custom text input instead native TextInput com
```jsx
const Textfield = MKTextField.textfield()
- .withPlaceholder('Text...')
- .withStyle(styles.textfield)
- .build();
+ .withPlaceholder('Text...')
+ .withStyle(styles.textfield)
+ .build()
-
-
+ placeholder="Enter text to see events"
+/>
```
#### customTextInputProps
@@ -125,54 +126,53 @@ const Textfield = MKTextField.textfield()
Some custom inputs like [react-native-textinput-effects](https://github.com/halilb/react-native-textinput-effects) have to set properties in mount time. For these types of components we use this property.
```jsx
-import React from 'react';
-import { StyleSheet, View } from 'react-native';
+import React from 'react'
+import { StyleSheet, View } from 'react-native'
-import { TextInputMask } from 'react-native-masked-text';
-import { Kaede } from 'react-native-textinput-effects';
+import { TextInputMask } from 'react-native-masked-text'
+import { Kaede } from 'react-native-textinput-effects'
export default class App extends React.Component {
- constructor(props) {
- super(props)
-
- this.state = {
- birthday: ''
- }
- }
-
- render() {
- return (
-
- this.setState({ birthday })}
- value={this.state.birthday} />
-
- );
- }
+ constructor(props) {
+ super(props)
+
+ this.state = {
+ birthday: ''
+ }
+ }
+
+ render() {
+ return (
+
+ this.setState({ birthday })}
+ value={this.state.birthday}
+ />
+
+ )
+ }
}
const styles = StyleSheet.create({
- container: {
- flex: 1,
- backgroundColor: '#fff',
- alignItems: 'center',
- justifyContent: 'center',
- }
-});
+ container: {
+ flex: 1,
+ backgroundColor: '#fff',
+ alignItems: 'center',
+ justifyContent: 'center'
+ }
+})
```
#### TextInput Props
@@ -191,25 +191,29 @@ If you want to use the methods of the native TextInput, use the `getElement()` m
export default class App extends React.Component {
onGoFocus() {
// when you call getElement method, the instance of native TextInput will returned.
- this.refs['myText'].getElement().focus();
+ this.refs['myText'].getElement().focus()
}
- render() {
- return (
-
-
-
-
-
-
-
-
- );
- }
+ render() {
+ return (
+
+
+
+
+
+
+
+
+ )
+ }
}
```
@@ -217,35 +221,37 @@ export default class App extends React.Component {
Some types accept options, use it like this: ``
-
**For `type={'money'}`**
-* *options={...}*
- * `precision` (Number, default 2): the decimal places.
- * `separator` (String, default ','): the decimal separator.
- * `delimiter` (String, default '.'): the thousand separator.
- * `unit`: (String, default 'R$'): the prefix text.
- * `suffixUnit` (String, default ''): the suffix text.
- * `zeroCents` (Boolean, default false): if must show cents.
+
+* _options={...}_
+ _ `precision` (Number, default 2): the decimal places.
+ _ `separator` (String, default ','): the decimal separator.
+ _ `delimiter` (String, default '.'): the thousand separator.
+ _ `unit`: (String, default 'R$'): the prefix text.
+ _ `suffixUnit` (String, default ''): the suffix text.
+ _ `zeroCents` (Boolean, default false): if must show cents.
**For `type={'cel-phone'}`**
-* *options={...}*
- * `withDDD` (Boolean, default true): if the ddd will be include in the mask.
- * `dddMask` (String, default '(99) '): the default mask applied if `withDDD` is true.
+
+* _options={...}_
+ _ `withDDD` (Boolean, default true): if the ddd will be include in the mask.
+ _ `dddMask` (String, default '(99) '): the default mask applied if `withDDD` is true.
**For `type={'datetime'}`**
-* *options={...}*
- * `format` (String, default DD/MM/YYYY HH:mm:ss): moment date format. It accepts the following:
- * DD/MM/YYYY HH:mm:ss
- * DD/MM/YYYY
- * MM/DD/YYYY
- * YYYY/MM/DD
- * HH:mm:ss
- * HH:mm
- * HH
- * *You can use all of dates with `-` instead of `/` if you want*
+
+* _options={...}_
+ _ `format` (String, default DD/MM/YYYY HH:mm:ss): moment date format. It accepts the following:
+ _ DD/MM/YYYY HH:mm:ss
+ _ DD/MM/YYYY
+ _ MM/DD/YYYY
+ _ YYYY/MM/DD
+ _ HH:mm:ss
+ _ HH:mm
+ _ HH \* _You can use all of dates with `-` instead of `/` if you want_
**For `type={'custom'}`**
-* *options={...}*
+
+* _options={...}_
```jsx
{
@@ -298,53 +304,32 @@ Some types accept options, use it like this: `
-* *options={...}*
- * `obfuscated` (Boolean, default false): if the mask must be `9999 **** **** 9999`
+* _options={...}_ \* `obfuscated` (Boolean, default false): if the mask must be `9999 **** **** 9999`
### Methods
-* `getElement()`: return the instance of *TextInput* component.
-* `isValid()`: if the value inputed is valid for the mask.
- * *credit-card*: return true if the mask is complete.
- * *cpf*: return true if the mask is complete and cpf is valid.
- * *cnpj*: return true if the mask is complete and cnpj is valid.
- * *zip-code*: return true if the mask is complete.
- * *only-numbers*: always returns true.
- * *money*: always returns true.
- * *cel-phone*: return true if the mask is complete.
- * *datetime*: return true if the date value is valid for format.
- * *custom*: use custom validation, if it not exist, always returns true.
-* `getRawValue()`: get the converted value of mask.
- * *credit-card*: return the array with the value parts. Ex: `1234 1234 1234 1234` returns `[1234, 1234, 1234, 1234]`.
- * *cpf*: return the value without mask.
- * *cnpj*: return the value without mask.
- * *zip-code*: return the value without mask.
- * *only-numbers*: return the value without mask.
- * *money*: return the Number value. Ex: `R$ 1.234,56` returns `1234.56`.
- * *cel-phone*: return the value without mask.
- * *datetime*: return the `moment` object for the date and format.
- * *custom*: use custom method (passed in options). If it not exists, returns the current value.
-
+* `getElement()`: return the instance of _TextInput_ component.
+* `isValid()`: if the value inputed is valid for the mask. \* _credit-card_: return true if the mask is complete. \* _cpf_: return true if the mask is complete and cpf is valid. \* _cnpj_: return true if the mask is complete and cnpj is valid. \* _zip-code_: return true if the mask is complete. \* _only-numbers_: always returns true. \* _money_: always returns true. \* _cel-phone_: return true if the mask is complete. \* _datetime_: return true if the date value is valid for format. \* _custom_: use custom validation, if it not exist, always returns true.
+* `getRawValue()`: get the converted value of mask. \* _credit-card_: return the array with the value parts. Ex: `1234 1234 1234 1234` returns `[1234, 1234, 1234, 1234]`. \* _cpf_: return the value without mask. \* _cnpj_: return the value without mask. \* _zip-code_: return the value without mask. \* _only-numbers_: return the value without mask. \* _money_: return the Number value. Ex: `R$ 1.234,56` returns `1234.56`. \* _cel-phone_: return the value without mask. \* _datetime_: return the `moment` object for the date and format. \* _custom_: use custom method (passed in options). If it not exists, returns the current value.
## Usage (TextMask)
```jsx
-import React, {Component} from 'react';
+import React, { Component } from 'react'
// import the component
-import {TextMask} from 'react-native-masked-text';
+import { TextMask } from 'react-native-masked-text'
export default class MyComponent extends Component {
constructor(props) {
- super(props);
+ super(props)
this.state = {
text: '4567123409871234'
- };
+ }
}
render() {
@@ -356,120 +341,146 @@ export default class MyComponent extends Component {
type={'credit-card'}
options={{
obfuscated: true
- }} />
- );
+ }}
+ />
+ )
}
}
-
```
### Props
-The same of *TextInputMask*, but for React-Native *Text* component instead *TextInput*.
+
+The same of _TextInputMask_, but for React-Native _Text_ component instead _TextInput_.
-*Warning*: if the value not match the mask, it will not appear.
+_Warning_: if the value not match the mask, it will not appear.
### Methods
-`getElement()`: return the instance of *Text* component.
+`getElement()`: return the instance of _Text_ component.
## Extra (MaskService)
+
If you want, we expose the `MaskService`. You can use it:
**Methods**
+
* static toMask(type, value, settings): mask a value.
- * `type` (String, required): the type of the mask (`cpf`, `datetime`, etc...)
- * `value` (String, required): the value to be masked
- * `settings` (Object, optional): if the mask type accepts options, pass it in the settings parameter
+ _ `type` (String, required): the type of the mask (`cpf`, `datetime`, etc...)
+ _ `value` (String, required): the value to be masked \* `settings` (Object, optional): if the mask type accepts options, pass it in the settings parameter
* static isValid(type, value, settings): validate if the mask and the value match.
- * `type` (String, required): the type of the mask (`cpf`, `datetime`, etc...)
- * `value` (String, required): the value to be masked
- * `settings` (Object, optional): if the mask type accepts options, pass it in the settings parameter
+ _ `type` (String, required): the type of the mask (`cpf`, `datetime`, etc...)
+ _ `value` (String, required): the value to be masked \* `settings` (Object, optional): if the mask type accepts options, pass it in the settings parameter
Ex:
-``` jsx
-import {MaskService} from 'react-native-masked-text'
+```jsx
+import { MaskService } from 'react-native-masked-text'
var money = MaskService.toMask('money', '123', {
unit: 'US$',
separator: '.',
delimiter: ','
-});
+})
// money -> US$ 1.23
```
## Throubleshooting
-* If the `es2015` error throw by babel, try run `react-native start --reset-cache`
+* If the `es2015` error throw by babel, try run `react-native start --reset-cache`
# Changelog
+
+## 1.6.5
+
+* Fixing validation for CPF and CNPJ when empty string is inputed.
+
## 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))
## 1.6.2
+
* Fixing custom mask when removing value in middle of the text. (thanks to [Aleksandr Kompaniets](https://github.com/Oxyaction))
- * KNOW ISSUE: the custom mask is not allowing user to input white spaces for `*` translation temporarily.
+ _ KNOW ISSUE: the custom mask is not allowing user to input white spaces for `_` translation temporarily.
## 1.6.1
+
* Fixing duplicated custom text input component. (thanks to [Pablo](https://github.com/rochapablo))
## 1.6.0
+
* Add compatibility to [react-native-textinput-effects](https://github.com/halilb/react-native-textinput-effects) by using `customTextInputProps` (thanks to [Pablo](https://github.com/rochapablo))
## 1.5.3
+
* Fix suffix backspace (thanks to [Thomas Kekeisen](https://github.com/blaues0cke))
* Fix last character on custom mask (thanks to [Daniel Maly](https://github.com/DanielMaly))
## 1.5.2
+
* Adding `keyboardType` to custom mask. (thanks to [Nurbek Abulgazin](https://github.com/nurbek-ab))
## 1.5.1
+
* Adding `tinymask` to fix some custom mask issues.
## 1.5.0
+
* Adding new and powerfull `custom` engine mask \m/.
## 1.4.0
+
* Adding `customTextInput` to allow other inputs instead native TextInput. (thanks to [Hellon Canella](https://github.com/helloncanella))
## 1.3.4
-* Remove default value from mask to allow placeholder on text-input (thanks to [Cuong Hoang](https://github.com/cuonghv91)).
- * Please, caution. This can cause some runtime breaking if you update to this version.
+
+* Remove default value from mask to allow placeholder on text-input (thanks to [Cuong Hoang](https://github.com/cuonghv91)). \* Please, caution. This can cause some runtime breaking if you update to this version.
## 1.3.3
+
* Update dependencies (thanks to [Vlad-Zhukov](https://github.com/Vlad-Zhukov))
## 1.3.2
+
* Fix: ignoring Jet Brains ide files (thanks to [Vlad-Zhukov](https://github.com/Vlad-Zhukov))
## 1.3.1
+
* Performance: adding check if the next value to be applied to the mask is equal as actual and prevent state change.
## 1.3.0
+
* Feat: now you can check and prevent input text on `TextInputMask` using `checkText` prop.
## 1.2.2
+
* Fix: fixing es2015 preset (thanks to [vagnercsouza](https://github.com/vagnercsouza), [barakcoh](https://github.com/barakcoh), Marvin Santos)
## 1.2.1
+
* Fix: fixing white space after unit on money mask.
## 1.2.0
+
* Adding `getRawValue`.
## 1.1.1
+
* Fixing toolbox-service reference (thanks to [ziftinpeki](https://github.com/ziftinpeki)).
## 1.1.0
+
* Adding credit-card mask.
* Refactoring base mask to contain helpfull functions.
## 1.0.0
+
* Adding datetime and cnpj masks.
* [Breaking Change] Refactoring MaskService.
* Separate mask handlers for better extensibility.
@@ -477,4 +488,5 @@ var money = MaskService.toMask('money', '123', {
* Refactoring Components for use new mask handlers.
## 0.3.6
+
* Fix vanilla-mask path on windows.
diff --git a/__tests__/cnpj.mask.test.js b/__tests__/cnpj.mask.test.js
index 9f9ccf2c..aea11dab 100644
--- a/__tests__/cnpj.mask.test.js
+++ b/__tests__/cnpj.mask.test.js
@@ -1,66 +1,73 @@
-import { CnpjMask } from '../lib/masks';
+import { CnpjMask } from '../lib/masks'
test('getType results cnpj', () => {
- var expected = 'cnpj';
- var received = CnpjMask.getType();
+ var expected = 'cnpj'
+ var received = CnpjMask.getType()
- expect(received).toBe(expected);
-});
+ expect(received).toBe(expected)
+})
test('79885262000130 results 79.885.262/0001-30', () => {
- var mask = new CnpjMask();
- var expected = '79.885.262/0001-30';
- var received = mask.getValue('79885262000130');
+ var mask = new CnpjMask()
+ var expected = '79.885.262/0001-30'
+ var received = mask.getValue('79885262000130')
- expect(received).toBe(expected);
-});
+ expect(received).toBe(expected)
+})
test('798852 results 79.885.2', () => {
- var mask = new CnpjMask();
- var expected = '79.885.2';
- var received = mask.getValue('798852');
+ var mask = new CnpjMask()
+ var expected = '79.885.2'
+ var received = mask.getValue('798852')
- expect(received).toBe(expected);
-});
+ expect(received).toBe(expected)
+})
test('79885262000130 results 79.885.262/0001-30 and is valid', () => {
- var mask = new CnpjMask();
- var expected = '79.885.262/0001-30';
- var received = mask.getValue('79885262000130');
- var isValid = mask.validate(received);
+ var mask = new CnpjMask()
+ var expected = '79.885.262/0001-30'
+ var received = mask.getValue('79885262000130')
+ var isValid = mask.validate(received)
- expect(received).toBe(expected);
- expect(isValid).toBe(true);
-});
+ expect(received).toBe(expected)
+ expect(isValid).toBe(true)
+})
test('79885262000140 results 79.885.262/0001-40 and is not valid', () => {
- var mask = new CnpjMask();
- var expected = '79.885.262/0001-40';
- var received = mask.getValue('79885262000140');
- var isValid = mask.validate(received);
+ var mask = new CnpjMask()
+ var expected = '79.885.262/0001-40'
+ var received = mask.getValue('79885262000140')
+ var isValid = mask.validate(received)
- expect(received).toBe(expected);
- expect(isValid).toBe(false);
-});
+ expect(received).toBe(expected)
+ expect(isValid).toBe(false)
+})
test('7988526200013 results 79.885.262/0001-3 and is not valid', () => {
- var mask = new CnpjMask();
- var expected = '79.885.262/0001-3';
- var received = mask.getValue('7988526200013');
- var isValid = mask.validate(received);
+ var mask = new CnpjMask()
+ var expected = '79.885.262/0001-3'
+ var received = mask.getValue('7988526200013')
+ var isValid = mask.validate(received)
- expect(received).toBe(expected);
- expect(isValid).toBe(false);
-});
+ expect(received).toBe(expected)
+ expect(isValid).toBe(false)
+})
test('79885262000130 results 79.885.262/0001-30 and raw value 79885262000130', () => {
- var mask = new CnpjMask();
- var expected = '79.885.262/0001-30';
- var received = mask.getValue('79885262000130');
+ var mask = new CnpjMask()
+ var expected = '79.885.262/0001-30'
+ var received = mask.getValue('79885262000130')
- var expectedRawValue = '79885262000130';
- var receivedRawValue = mask.getRawValue(received);
+ var expectedRawValue = '79885262000130'
+ var receivedRawValue = mask.getRawValue(received)
- expect(received).toBe(expected);
- expect(receivedRawValue).toBe(expectedRawValue);
-});
\ No newline at end of file
+ expect(received).toBe(expected)
+ expect(receivedRawValue).toBe(expectedRawValue)
+})
+
+test('empty cnpj is invalid', () => {
+ var mask = new CnpjMask()
+ var received = mask.validate('', {})
+
+ expect(received).toBeFalsy()
+})
diff --git a/__tests__/cpf.mask.test.js b/__tests__/cpf.mask.test.js
index 261a9666..417f4024 100644
--- a/__tests__/cpf.mask.test.js
+++ b/__tests__/cpf.mask.test.js
@@ -1,66 +1,73 @@
-import { CpfMask } from '../lib/masks';
+import { CpfMask } from '../lib/masks'
test('getType results cpf', () => {
- var expected = 'cpf';
- var received = CpfMask.getType();
+ var expected = 'cpf'
+ var received = CpfMask.getType()
- expect(received).toBe(expected);
-});
+ expect(received).toBe(expected)
+})
test('12312312356 results 123.123.123-56', () => {
- var mask = new CpfMask();
- var expected = '123.123.123-56';
- var received = mask.getValue('12312312356');
+ var mask = new CpfMask()
+ var expected = '123.123.123-56'
+ var received = mask.getValue('12312312356')
- expect(received).toBe(expected);
-});
+ expect(received).toBe(expected)
+})
test('123123 results 123.123', () => {
- var mask = new CpfMask();
- var expected = '123.123';
- var received = mask.getValue('123123');
+ var mask = new CpfMask()
+ var expected = '123.123'
+ var received = mask.getValue('123123')
- expect(received).toBe(expected);
-});
+ expect(received).toBe(expected)
+})
test('07833823678 results 078.338.236-78 and is valid', () => {
- var mask = new CpfMask();
- var expected = '078.338.236-78';
- var received = mask.getValue('07833823678');
- var isValid = mask.validate(received);
+ var mask = new CpfMask()
+ var expected = '078.338.236-78'
+ var received = mask.getValue('07833823678')
+ var isValid = mask.validate(received)
- expect(received).toBe(expected);
- expect(isValid).toBe(true);
-});
+ expect(received).toBe(expected)
+ expect(isValid).toBe(true)
+})
test('11111111111 results 111.111.111-11 and is not valid', () => {
- var mask = new CpfMask();
- var expected = '111.111.111-11';
- var received = mask.getValue('11111111111');
- var isValid = mask.validate(received);
+ var mask = new CpfMask()
+ var expected = '111.111.111-11'
+ var received = mask.getValue('11111111111')
+ var isValid = mask.validate(received)
- expect(received).toBe(expected);
- expect(isValid).toBe(false);
-});
+ expect(received).toBe(expected)
+ expect(isValid).toBe(false)
+})
test('1234567890 results 123.456.789-0 and is not valid', () => {
- var mask = new CpfMask();
- var expected = '123.456.789-0';
- var received = mask.getValue('1234567890');
- var isValid = mask.validate(received);
+ var mask = new CpfMask()
+ var expected = '123.456.789-0'
+ var received = mask.getValue('1234567890')
+ var isValid = mask.validate(received)
- expect(received).toBe(expected);
- expect(isValid).toBe(false);
-});
+ expect(received).toBe(expected)
+ expect(isValid).toBe(false)
+})
test('12312312356 results 123.123.123-56 and raw value 12312312356', () => {
- var mask = new CpfMask();
- var expected = '123.123.123-56';
- var received = mask.getValue('12312312356');
+ var mask = new CpfMask()
+ var expected = '123.123.123-56'
+ var received = mask.getValue('12312312356')
- var expectedRawValue = '12312312356';
- var receivedRawValue = mask.getRawValue(received);
+ var expectedRawValue = '12312312356'
+ var receivedRawValue = mask.getRawValue(received)
- expect(received).toBe(expected);
- expect(receivedRawValue).toBe(expectedRawValue);
-});
\ No newline at end of file
+ expect(received).toBe(expected)
+ expect(receivedRawValue).toBe(expectedRawValue)
+})
+
+test('empty cpf is invalid', () => {
+ var mask = new CpfMask()
+ var received = mask.validate('', {})
+
+ expect(received).toBeFalsy()
+})
diff --git a/dist/lib/masks/money.mask.js b/dist/lib/masks/money.mask.js
index da015071..d7ca719a 100644
--- a/dist/lib/masks/money.mask.js
+++ b/dist/lib/masks/money.mask.js
@@ -17,17 +17,16 @@ MoneyMask=function(_BaseMask){_inherits(MoneyMask,_BaseMask);function MoneyMask(
value,settings,oldValue){
var mergedSettings=_get(MoneyMask.prototype.__proto__||Object.getPrototypeOf(MoneyMask.prototype),'mergeSettings',this).call(this,MONEY_MASK_SETTINGS,settings);
-if(mergedSettings.suffixUnit&&oldValue&&value){
+var sanitized=this._sanitize(value,mergedSettings);
-
-
-if(value.length==oldValue.length-1){
-var cleared=this.removeNotNumbers(value);
-value=cleared.substr(0,cleared.length-1);
+if(mergedSettings.suffixUnit&&oldValue&&sanitized){
+if(sanitized.length==oldValue.length-1){
+var cleared=this.removeNotNumbers(sanitized);
+sanitized=cleared.substr(0,cleared.length-1);
}
}
-var masked=this.getVMasker().toMoney(value,mergedSettings);
+var masked=this.getVMasker().toMoney(sanitized,mergedSettings);
return masked;
}},{key:'getRawValue',value:function getRawValue(
@@ -44,13 +43,24 @@ return Number(normalized);
value,settings){
return true;
+}},{key:'_sanitize',value:function _sanitize(
+
+value,settings){
+if(typeof value==='number'){
+return value.toFixed(settings.precision);
+}
+
+return value;
}},{key:'_insert',value:function _insert(
text,index,string){
if(index>0){
-return text.substring(0,index)+string+text.substring(index,text.length);
-}else
-{
+return(
+text.substring(0,index)+
+string+
+text.substring(index,text.length));
+
+}else{
return string+text;
}
}}],[{key:'getType',value:function getType(){return'money';}}]);return MoneyMask;}(_base2.default);exports.default=MoneyMask;
\ No newline at end of file
diff --git a/lib/masks/cnpj.mask.js b/lib/masks/cnpj.mask.js
index e7f494f3..183f9d0c 100644
--- a/lib/masks/cnpj.mask.js
+++ b/lib/masks/cnpj.mask.js
@@ -1,41 +1,42 @@
-import BaseMask from './_base.mask';
+import BaseMask from './_base.mask'
-const CNPJ_MASK = '99.999.999/9999-99';
+const CNPJ_MASK = '99.999.999/9999-99'
const validateCnpj = cnpj => {
- var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
- var dig1= new Number;
- var dig2= new Number;
- var i = 0;
-
- var exp = /\.|\-|\//g;
- cnpj = cnpj.toString().replace( exp, "" );
- var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));
-
- for(i = 0; i0? (cnpj.charAt(i-1)*valida[i]):0);
- dig2 += cnpj.charAt(i)*valida[i];
- }
- dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
- dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));
-
- return (((dig1*10)+dig2) == digito);
+ var valida = new Array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2)
+ var dig1 = new Number()
+ var dig2 = new Number()
+ var i = 0
+
+ var exp = /\.|\-|\//g
+ cnpj = cnpj.toString().replace(exp, '')
+ var digito = new Number(eval(cnpj.charAt(12) + cnpj.charAt(13)))
+
+ for (i = 0; i < valida.length; i++) {
+ dig1 += i > 0 ? cnpj.charAt(i - 1) * valida[i] : 0
+ dig2 += cnpj.charAt(i) * valida[i]
+ }
+ dig1 = dig1 % 11 < 2 ? 0 : 11 - dig1 % 11
+ dig2 = dig2 % 11 < 2 ? 0 : 11 - dig2 % 11
+
+ return dig1 * 10 + dig2 == digito
}
export default class CnpjMask extends BaseMask {
- static getType() {
- return 'cnpj';
- }
+ static getType() {
+ return 'cnpj'
+ }
- getValue(value, settings) {
- return this.getVMasker().toPattern(value, CNPJ_MASK);
- }
+ getValue(value, settings) {
+ return this.getVMasker().toPattern(value, CNPJ_MASK)
+ }
- getRawValue(maskedValue, settings) {
- return super.removeNotNumbers(maskedValue);
+ getRawValue(maskedValue, settings) {
+ return super.removeNotNumbers(maskedValue)
}
- validate(value, settings) {
- return validateCnpj(value);
- }
-}
\ No newline at end of file
+ validate(value, settings) {
+ var isEmpty = (value || '').trim().length === 0
+ return !isEmpty && validateCnpj(value)
+ }
+}
diff --git a/lib/masks/cpf.mask.js b/lib/masks/cpf.mask.js
index 7824819e..76f1a137 100644
--- a/lib/masks/cpf.mask.js
+++ b/lib/masks/cpf.mask.js
@@ -1,70 +1,83 @@
-import BaseMask from './_base.mask';
-
-const CPF_MASK = '999.999.999-99';
-
-const validateCPF = (cpf) => {
- if (cpf == "") {
- return true;
- }
-
- cpf = cpf.replace(/\./gi, "").replace(/-/gi, "");
- var isValid = true;
- var sum;
- var rest;
- var i;
- i = 0;
- sum = 0;
-
- if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") {
- isValid = false;
- }
-
- for (i = 1; i <= 9; i++) {
- sum = sum + parseInt(cpf.substring(i - 1, i)) * (11 - i);
- }
-
- rest = (sum * 10) % 11;
-
- if ((rest == 10) || (rest == 11)) {
- rest = 0;
- }
-
- if (rest != parseInt(cpf.substring(9, 10))) {
- isValid = false;
- }
-
- sum = 0;
-
- for (i = 1; i <= 10; i++) {
- sum = sum + parseInt(cpf.substring(i - 1, i)) * (12 - i);
- }
-
- rest = (sum * 10) % 11;
-
- if ((rest == 10) || (rest == 11)) {
- rest = 0;
- }
- if (rest != parseInt(cpf.substring(10, 11))) {
- isValid = false;
- }
-
- return isValid;
+import BaseMask from './_base.mask'
+
+const CPF_MASK = '999.999.999-99'
+
+const validateCPF = cpf => {
+ if (cpf == '') {
+ return true
+ }
+
+ cpf = cpf.replace(/\./gi, '').replace(/-/gi, '')
+ var isValid = true
+ var sum
+ var rest
+ var i
+ i = 0
+ sum = 0
+
+ if (
+ cpf.length != 11 ||
+ cpf == '00000000000' ||
+ cpf == '11111111111' ||
+ cpf == '22222222222' ||
+ cpf == '33333333333' ||
+ cpf == '44444444444' ||
+ cpf == '55555555555' ||
+ cpf == '66666666666' ||
+ cpf == '77777777777' ||
+ cpf == '88888888888' ||
+ cpf == '99999999999'
+ ) {
+ isValid = false
+ }
+
+ for (i = 1; i <= 9; i++) {
+ sum = sum + parseInt(cpf.substring(i - 1, i)) * (11 - i)
+ }
+
+ rest = (sum * 10) % 11
+
+ if (rest == 10 || rest == 11) {
+ rest = 0
+ }
+
+ if (rest != parseInt(cpf.substring(9, 10))) {
+ isValid = false
+ }
+
+ sum = 0
+
+ for (i = 1; i <= 10; i++) {
+ sum = sum + parseInt(cpf.substring(i - 1, i)) * (12 - i)
+ }
+
+ rest = (sum * 10) % 11
+
+ if (rest == 10 || rest == 11) {
+ rest = 0
+ }
+ if (rest != parseInt(cpf.substring(10, 11))) {
+ isValid = false
+ }
+
+ return isValid
}
export default class CpfMask extends BaseMask {
- static getType() {
- return 'cpf';
- }
-
- getValue(value, settings) {
- return this.getVMasker().toPattern(value, CPF_MASK);
- }
-
- getRawValue(maskedValue, settings) {
- return super.removeNotNumbers(maskedValue);
- }
-
- validate(value, settings) {
- return validateCPF(value);
- }
-}
\ No newline at end of file
+ static getType() {
+ return 'cpf'
+ }
+
+ getValue(value, settings) {
+ return this.getVMasker().toPattern(value, CPF_MASK)
+ }
+
+ getRawValue(maskedValue, settings) {
+ return super.removeNotNumbers(maskedValue)
+ }
+
+ validate(value, settings) {
+ var isEmpty = (value || '').trim().length === 0
+ return !isEmpty && validateCPF(value)
+ }
+}
diff --git a/package-lock.json b/package-lock.json
index 2b89ee77..e28cb996 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "react-native-masked-text",
- "version": "1.6.3",
+ "version": "1.6.4",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
diff --git a/package.json b/package.json
index 071ac240..f39b1237 100644
--- a/package.json
+++ b/package.json
@@ -1,39 +1,39 @@
{
- "name": "react-native-masked-text",
- "version": "1.6.4",
- "description": "Text and TextInput with mask for React Native applications",
- "licenses": [
- {
- "type": "MIT",
- "url": "https://www.opensource.org/licenses/mit-license.php"
- }
- ],
- "main": "./dist/index.js",
- "scripts": {
- "clean": "rimraf dist",
- "test": "jest",
- "build": "babel ./index.js ./lib/*.js ./lib/**/*.js -d ./dist",
- "prepublish": "npm run clean && npm run build"
- },
- "repository": {
- "type": "git",
- "url": "git+https://github.com/benhurott/react-native-masked-text.git"
- },
- "keywords": ["mask", "text", "textinput", "react-native"],
- "author": "Ben-hur Santos Ott",
- "license": "ISC",
- "bugs": {
- "url": "https://github.com/benhurott/react-native-masked-text/issues"
- },
- "homepage": "https://github.com/benhurott/react-native-masked-text#readme",
- "dependencies": {
- "moment": "^2.19.1",
- "tinymask": "^1.0.2"
- },
- "devDependencies": {
- "babel-cli": "^6.26.0",
- "babel-preset-react-native": "^1.9.1",
- "jest": "^19.0.2",
- "rimraf": "^2.6.2"
- }
+ "name": "react-native-masked-text",
+ "version": "1.6.5",
+ "description": "Text and TextInput with mask for React Native applications",
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://www.opensource.org/licenses/mit-license.php"
+ }
+ ],
+ "main": "./dist/index.js",
+ "scripts": {
+ "clean": "rimraf dist",
+ "test": "jest",
+ "build": "babel ./index.js ./lib/*.js ./lib/**/*.js -d ./dist",
+ "prepublish": "npm run clean && npm run build"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/benhurott/react-native-masked-text.git"
+ },
+ "keywords": ["mask", "text", "textinput", "react-native"],
+ "author": "Ben-hur Santos Ott",
+ "license": "ISC",
+ "bugs": {
+ "url": "https://github.com/benhurott/react-native-masked-text/issues"
+ },
+ "homepage": "https://github.com/benhurott/react-native-masked-text#readme",
+ "dependencies": {
+ "moment": "^2.19.1",
+ "tinymask": "^1.0.2"
+ },
+ "devDependencies": {
+ "babel-cli": "^6.26.0",
+ "babel-preset-react-native": "^1.9.1",
+ "jest": "^19.0.2",
+ "rimraf": "^2.6.2"
+ }
}