@samuil4/dirty-number
v1.0.1
Published
Parse dirty string to number
Downloads
7
Maintainers
Readme
dirty-number
Extract correct number from dirty string like -100,000 00 . 00
.
Usage
Install the library:
npm install @samuil4/dirty-number
Then, in the file where you want to use it:
//ES6 / TypeScript
import { DirtyNumber } from '@samuil4/dirty-number';
//...
const numberParser = new DirtyNumber();
numberParser.parse('1,000,000.45') // => 1000000.45 as Number
//node.js
const DirtyNumber = require('@samuil4/dirty-number/node').default;
//...
const numberParser = new DirtyNumber();
numberParser.parse('1,000,000.45') // => 1000000.45 as Number
Default configuration works as follows
// 1. strip any non numerical characters excluding +-.,
// 2. Assuming denominator is . character
// 3. Assimung separator is , character
const numberParser = new DirtyNumber();
numberParse.parse('3.14'); // => 3.14
numberParse.parse('3,14'); // => 314
numberParse.parse('3,140.142'); // => 3140.142
numberParse.parse('3,000,140.142'); // => 3000140.142
numberParse.parse('3 , 000 , 140.142'); // => 3000140.142
// 4. Note: When denominator is matched multiple times, denominator becomes separator
numberParse.parse('3.140.142'); // => 3140142
// 5. Use on any dirty strings, like crawled prices
numberParse.parse('3.14 USD'); // => 3.14
numberParse.parse('Price: 3.14 USD'); // => 3.14
Configurable options
Configure the denominator symbol
//ES6 / TypeScript
// Denominator
import { DirtyNumber } from '@samuil4/dirty-number';
//...
const numberParser = new DirtyNumber({
denominator: '#'
});
numberParser.parse('1,000,000#45') // => 1000000.45 as Number
Configure separator symbol
//ES6 / TypeScript
// Separator
import { DirtyNumber } from '@samuil4/dirty-number';
//...
const numberParser = new DirtyNumber({
separator: '#'
});
numberParser.parse('1#000#000.45') // => 1000000.45 as Number
Weird examples
//ES6 / TypeScript
// Denominator ,
import { DirtyNumber } from '@samuil4/dirty-number';
const numberParser = new DirtyNumber({
denominator: ','
});
const num = numberParser.parse('3,14'); // => 3.14
Local Development
- Fork the project and clone it locally
npm install
to install the library dependenciesnpm install -g typescript
to install TypeScript globallynpm test
to run testsnpm run build
to build for production