A cool JS helper to trim/clean currency values! 💸
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! 🚀
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;// commonjsconst trimCurrency = ;
Then, you'll be able to use it:
const cleanCurrency = ; // 100000000 (as Number)
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; // we're replacing the match tokens with nothing
If you need to deal with money/currency using JS, these packages provides nice methods and you should take a look:
Licensed under the MIT.