Skip to content

Commit

Permalink
feat(currency-number): created helper with docs (#231)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Amaral <[email protected]>
  • Loading branch information
ammichael and Michael Amaral authored Nov 19, 2020
1 parent 17bbb5d commit 51cc974
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { isIOS } from '@platformbuilders/helpers/native'
| [`getTheme`](./docs/getTheme.md) | string | (themeProp)({ theme }) |
| [`ifStyle`](./docs/ifStyle.md) | truthy OR falsy | (prop)(truthy, falsy)(style) |
| [`parseToThousands`](./docs/parseToThousands.md) | string | (value) |
| [`currencyToNumber`](./docs/currencyToNumber.md) | string | (value) |
| [`removeWhiteSpaces`](./docs/removeWhiteSpaces.md) | string | (value) |
| [`switchStyle`](./docs/switchStyle.md) | string | (name)(stylesObj)(props) |
| [`toOnlyNumbers`](./docs/toOnlyNumbers.md) | string | (value) |
Expand Down
19 changes: 19 additions & 0 deletions docs/currencyToNumber.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# `currencyToNumber`

Parse brazilian currency string to number format.

## Arguments

- `currency: string`: currency value to be format

## Returns

- `value: number`: A number

## Usage

```jsx
import { currencyToNumber } from '@platformbuilders/helpers';

currencyToNumber('R$ 1000,00'); // return 1000
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@platformbuilders/helpers",
"version": "0.3.2",
"version": "0.3.3",
"description": "Platfom builders helpers library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/currencyToNumber.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { currencyToNumber } from '../currencyToNumber';

describe('currencyToNumber tests', () => {
it('should parse to thousands', () => {
const parsed = currencyToNumber('R$ 1000,00');
expect(parsed).toBe(1000);
});
it('should parse to thousands with decimals', () => {
const parsed = currencyToNumber('R$ 1000,10');
expect(parsed).toBe(1000.1);
});
});
2 changes: 2 additions & 0 deletions src/currencyToNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const currencyToNumber = (currency: string): number =>
parseFloat(currency.replace('R$', '').replace('.', '').replace(',', '.'));
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './ifStyle';
export * from './switchStyle';
export * from './base64';
export * from './pixelToRem';
export * from './currencyToNumber';

0 comments on commit 51cc974

Please sign in to comment.