Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scaleRaw option to get integer value from money mask (e.g. cents) #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Some types accept options, use it like this: `<TextInputMask type={'money'} opti
* `unit`: (String, default 'R$'): the prefix text.
* `suffixUnit` (String, default ''): the suffix text.
* `zeroCents` (Boolean, default false): if must show cents.
* `scaleRaw` (Boolean, default true): if getRawValue returns scaled (1.23) or un-scaled (123) Number.

**For `type={'cel-phone'}`** <br />
* *options={...}*
Expand Down Expand Up @@ -404,6 +405,10 @@ var money = MaskService.toMask('money', '123', {


# Changelog

## 1.6.2
* Add `scaleRaw` option to money mask. (thanks to [Cogwheel](https://github.com/cogwheel))

## 1.6.1
* Fixing duplicated custom text input component. (thanks to [Pablo](https://github.com/rochapablo))

Expand Down
16 changes: 15 additions & 1 deletion __tests__/money.mask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ test('1 zeroCents results R$1,00 and raw value 1', () => {
expect(receivedRawValue).toBe(expectedRawValue);
});

test('1 scaleRaw false results R$0,01 and raw value 1', () => {
var mask = new MoneyMask();
var expected = 'R$0,01';
var received = mask.getValue('1');

var expectedRawValue = 1;
var receivedRawValue = mask.getRawValue(received, {
scaleRaw: false
});

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';
Expand All @@ -195,4 +209,4 @@ test('1 unit US$ results US$ 0,01', () => {
});

expect(received).toBe(expected);
});
});
12 changes: 8 additions & 4 deletions lib/masks/money.mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const MONEY_MASK_SETTINGS = {
delimiter: '.',
unit: 'R$',
suffixUnit: '',
zeroCents: false
zeroCents: false,
scaleRaw: true
};

export default class MoneyMask extends BaseMask {
Expand Down Expand Up @@ -36,8 +37,11 @@ export default class MoneyMask extends BaseMask {
let mergedSettings = super.mergeSettings(MONEY_MASK_SETTINGS, settings);
let normalized = super.removeNotNumbers(maskedValue);

let dotPosition = normalized.length - mergedSettings.precision;
normalized = this._insert(normalized, dotPosition, '.');
if (mergedSettings.scaleRaw && mergedSettings.precision > 0) {
// turn 123 into 1.23 (given precision of 2)
let dotPosition = normalized.length - mergedSettings.precision;
normalized = this._insert(normalized, dotPosition, '.');
}

return Number(normalized);
}
Expand All @@ -54,4 +58,4 @@ export default class MoneyMask extends BaseMask {
return string + text;
}
};
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-masked-text",
"version": "1.6.1",
"version": "1.6.2",
"description": "Text and TextInput with mask for React Native applications",
"main": "index.js",
"scripts": {
Expand Down