Skip to content

A cool JS helper to trim/clean currency values ๐Ÿ’ธ

License

Notifications You must be signed in to change notification settings

jlozovei/trim-currency

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

86 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

@jlozovei/trim-currency

A cool JS helper to trim/clean currency values! ๐Ÿ’ธ

codecov Release

๐Ÿ“œ About

If you already have to deal with currency numbers with JS, you know the struggle is real.

After some suffering with it, I decided to develop this helper function to trim/clean currency values, returning them in clean integer values.

No dependencies, no framework restrictions - just plain JS! ๐Ÿš€

๐Ÿ“• Usage

First things first: you need to install the package:

npm i @jlozovei/trim-currency

# or yarn add @jlozovei/trim-currency

After that, you'll need to import the helper wherever you want to use it:

// es-modules
import trimCurrency from '@jlozovei/trim-currency';

// commonjs
const trimCurrency = require('@jlozovei/trim-currency');

Then, you'll be able to use it:

const cleanCurrency = trimCurrency('$1,000,000.00'); // 100000000 (as Number)

๐Ÿ’ป Developing

First, fork the project. After it, install the dependencies (preferably using npm - since the project is using it) and do the work.

Also, take a look at the contributing guide!

๐Ÿค” I want to use it, but I don't want to install it

Cool! So, the magic under the hood is basically removing all the non-digits characters from the string, using a regular expression (a.k.a. RegExp):

const regex = /\D+/g; // \D is the token for non-digits
/*
 * you can also use the RegExp constructor:
 * const regex = new RegExp('\\D+', 'g');
 */

const currency = 'R$ 1.000.000,00';
const clean = currency.replace(regex, ''); // we're replacing the match tokens with nothing

๐Ÿ“š Related

If you need to deal with money/currency using JS, these packages provides nice methods and you should take a look:

๐Ÿ” License

Licensed under the MIT.