parsecurrency
Advanced tools
Comparing version 0.1.0 to 0.2.0
const currencyMatcher = /^(?:([A-Z]{3}) ?)?(?:([^\d ]+?) ?)?(((?:\d{1,3}([,. ’]))*?\d{1,})(([,.])\d{1,2})?)(?: ?([^\d ]+?))??(?: ?([A-Z]{3}))?$/; | ||
const gr = /^\d{1,3}([,. ’]\d{3})*$/; // validate groups | ||
const ind = /^\d{1,2}(,\d{2})*(,\d{3})?$/; // exception for indina number format | ||
module.exports = function parseCurrency (priceStr) { | ||
@@ -11,2 +14,6 @@ if (!priceStr || !priceStr.match) return null; | ||
} | ||
var integer = match[4]; | ||
if (groupSeparator && !integer.match(gr) && !integer.match(ind)) { | ||
return null; | ||
} | ||
var value = match[3]; | ||
@@ -27,3 +34,3 @@ if (!value) return null; | ||
value: numericVal, | ||
integer: match[4] || '', | ||
integer: integer || '', | ||
decimals: match[6] || '', | ||
@@ -30,0 +37,0 @@ currency: match[1] || match[9] || '', |
{ | ||
"name": "parsecurrency", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Currency string parser utility", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# parsecurrency | ||
[![Build Status](https://travis-ci.org/mktj/parsecurrency.svg?branch=master)](https://travis-ci.org/mktj/parsecurrency) | ||
Node / browser currency parser. | ||
Extensive currency parsing utility designed to extract value, decimal separator, group separator, currency symbol and iso code from currency string. It should work with most world [currency formats][1] except: | ||
- currencies with 3 decimals | ||
- currency with 2 character group separator (Swaziland Lilangeni) | ||
Works with: | ||
- international currency formatting (SFr 12'345.67 or 10 000,00zł) | ||
- indian number system (₹1,50,000.00) | ||
- currency symbols as a prefix / suffix with or without a space | ||
- currency code before or after the value, with or without space | ||
## Install | ||
@@ -11,5 +22,5 @@ | ||
## Usage | ||
## Example | ||
```js | ||
```javascript | ||
const parseCurrency = require('parsecurrency'); | ||
@@ -31,1 +42,4 @@ | ||
``` | ||
[More examples](./test.js) | ||
[1]: http://www.thefinancials.com/Default.aspx?SubSectionID=curformat |
46
test.js
/* eslint-env mocha */ | ||
const { expect } = require('chai'); | ||
const expect = require('chai').expect; | ||
const parseCurrency = require('./index'); | ||
@@ -170,2 +170,26 @@ | ||
it('should distinguish decimal / group separator', () => { | ||
expect(parseCurrency('100,00')).to.deep.equal({ | ||
raw: '100,00', | ||
value: 100, | ||
integer: '100', | ||
decimals: ',00', | ||
currency: '', | ||
symbol: '', | ||
decimalSeparator: ',', | ||
groupSeparator: '' | ||
}); | ||
expect(parseCurrency('100,000')).to.deep.equal({ | ||
raw: '100,000', | ||
value: 100000, | ||
integer: '100,000', | ||
decimals: '', | ||
currency: '', | ||
symbol: '', | ||
decimalSeparator: '', | ||
groupSeparator: ',' | ||
}); | ||
}); | ||
it('should work with real world examples', () => { | ||
@@ -196,9 +220,9 @@ expect(parseCurrency('¥578,349,027')).to.deep.equal({ | ||
it('should parse indian numbering system', () => { | ||
expect(parseCurrency('₹1,50,000')).to.deep.equal({ | ||
expect(parseCurrency('₹1,50,000.00')).to.deep.equal({ | ||
currency: '', | ||
decimalSeparator: '', | ||
decimals: '', | ||
decimalSeparator: '.', | ||
decimals: '.00', | ||
groupSeparator: ',', | ||
integer: '1,50,000', | ||
raw: '₹1,50,000', | ||
raw: '₹1,50,000.00', | ||
symbol: '₹', | ||
@@ -209,2 +233,14 @@ value: 150000 | ||
it('should invalidate non matching number grouping', () => { | ||
expect(parseCurrency('$ 100,000,0,00.00')).to.equal(null); | ||
}); | ||
it('should fail to parse currencies with 3 decimal points (BHD, IQD, JOD, KWD, OMR, TND)', () => { | ||
expect(parseCurrency('1,234.567')).to.equal(null); | ||
}); | ||
it('should fail to parse 2 character group separator (Swaziland, Lilangeni)', () => { | ||
expect(parseCurrency('1, 000.00')).to.equal(null); | ||
}); | ||
it('shoud not match invalid currency numbers', () => { | ||
@@ -211,0 +247,0 @@ expect(parseCurrency('')).to.equal(null); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10091
7
296
43