@jlozovei/trim-currency

1.1.0 • Public • Published

@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.

Readme

Keywords

Package Sidebar

Install

npm i @jlozovei/trim-currency

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

6.2 kB

Total Files

4

Last publish

Collaborators

  • jlozovei