1.1.0 • Published 4 years ago

@jlozovei/trim-currency v1.1.0

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

@jlozovei/trim-currency

A cool JS helper to trim/clean currency values! :money_with_wings:

codecov Release

:scroll: 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! :rocket:

:closed_book: 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)

:computer: 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!

:thinking: 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

:books: Related

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

:closed_lock_with_key: License

Licensed under the MIT.