parsecurrency
Advanced tools
Comparing version 1.0.1 to 1.1.0
27
index.js
@@ -1,6 +0,6 @@ | ||
var currencyMatcher = /^(?:([A-Z]{3}) ?)?(?:([^\d ]+?) ?)?(((?:\d{1,3}([,. ’'\u00A0]))*?\d{1,})(([,.])\d{1,2})?)(?: ?([^\d ]+?))??(?: ?([A-Z]{3}))?$/; | ||
var gr = /^\d{1,3}([,. ’'\u00A0]\d{3})*$/; // validate groups | ||
var currencyMatcher = /^(?:([-+]{1}) ?)?(?:([A-Z]{3}) ?)?(?:([^\d ]+?) ?)?(((?:\d{1,3}([,. ’'\u00A0\u202F]))*?\d{1,})(([,.])\d{1,2})?)(?: ?([^\d]+?))??(?: ?([A-Z]{3}))?$/; | ||
var gr = /^\d{1,3}([,. ’'\u00A0\u202F]\d{3})*$/; // validate groups | ||
var ind = /^\d{1,2}(,\d{2})*(,\d{3})?$/; // exception for Indian number format | ||
module.exports = function parseCurrency (priceStr) { | ||
module.exports = function parseCurrency(priceStr) { | ||
if (!priceStr || !priceStr.match) return null; | ||
@@ -10,12 +10,12 @@ priceStr = priceStr.trim(); | ||
if (!match) return null; | ||
var groupSeparator = match[5] || ''; | ||
var decimalSeparator = match[7] || ''; | ||
var groupSeparator = match[6] || ''; | ||
var decimalSeparator = match[8] || ''; | ||
if (groupSeparator === decimalSeparator && decimalSeparator) { | ||
return null; | ||
} | ||
var integer = match[4]; | ||
if (groupSeparator && !integer.match(gr) && !integer.match(ind)) { | ||
var integer = match[1] === '-' ? '-' + match[5] : match[5]; | ||
if (groupSeparator && !match[5].match(gr) && !match[5].match(ind)) { | ||
return null; | ||
} | ||
var value = match[3]; | ||
var value = match[4]; | ||
if (!value) return null; | ||
@@ -28,3 +28,3 @@ if (groupSeparator) { | ||
} | ||
var numericVal = +value; | ||
var numericVal = match[1] === '-' ? value * -1 : +value; | ||
if (typeof numericVal !== 'number' || isNaN(numericVal)) { | ||
@@ -37,8 +37,9 @@ return null; | ||
integer: integer || '', | ||
decimals: match[6] || '', | ||
currency: match[1] || match[9] || '', | ||
symbol: match[2] || match[8] || '', | ||
decimals: match[7] || '', | ||
currency: match[2] || match[10] || '', | ||
symbol: match[3] || match[9] || '', | ||
decimalSeparator: decimalSeparator, | ||
groupSeparator: groupSeparator | ||
groupSeparator: groupSeparator, | ||
sign: match[1] || '' | ||
}; | ||
}; |
{ | ||
"name": "parsecurrency", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Tiny currency string parser utility", | ||
@@ -28,3 +28,3 @@ "main": "index.js", | ||
"eslint": "^8.2.0", | ||
"mocha": "^9.1.3" | ||
"mocha": "^10.1.0" | ||
}, | ||
@@ -31,0 +31,0 @@ "dependencies": {}, |
# parsecurrency | ||
[![Build Status](https://travis-ci.org/mktj/parsecurrency.svg?branch=master)](https://travis-ci.org/mktj/parsecurrency) | ||
![Build Status](https://github.com/mktj/parsecurrency/actions/workflows/test.yml/badge.svg) | ||
[![npm version](http://img.shields.io/npm/v/parsecurrency.svg?style=flat)](https://npmjs.org/package/parsecurrency "View this project on npm") | ||
@@ -8,3 +8,3 @@ | ||
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: | ||
Extensive currency parsing utility designed to extract value, decimal separator, group separator, currency symbol, iso code and sign from currency string. It should work with most world [currency formats][1] except: | ||
- currencies with 3 decimals | ||
@@ -19,2 +19,3 @@ - currency with 2 character group separator (Swaziland Lilangeni) | ||
- currency code before or after the value, with or without space | ||
- positive and negative signs (before the currency) | ||
@@ -32,14 +33,28 @@ ## Install | ||
const parsedCurrency = parseCurrency('$123,456.99USD'); | ||
// parsedCurrency => | ||
const example1 = parseCurrency('$123,456.99USD'); | ||
// example1 => | ||
{ | ||
"raw": "$123,456.99USD", | ||
"value": 123456.99, | ||
"integer": "123,456", | ||
"decimals": ".99", | ||
"currency": "USD", | ||
"symbol": "$", | ||
"decimalSeparator": ".", | ||
"groupSeparator": "," | ||
raw: '$123,456.99USD', | ||
value: 123456.99, | ||
integer: '123,456', | ||
decimals: '.99', | ||
currency: 'USD', | ||
symbol: '$', | ||
decimalSeparator: '.', | ||
groupSeparator: ',', | ||
sign: '' | ||
} | ||
const example2 = parseCurrency('-¥578,349,027'); | ||
// example2 => | ||
{ | ||
raw: '-¥578,349,027', | ||
value: -578349027, | ||
integer: '-578,349,027', | ||
decimals: '', | ||
currency: '', | ||
symbol: '¥', | ||
decimalSeparator: '', | ||
groupSeparator: ',', | ||
sign: '-' | ||
} | ||
@@ -46,0 +61,0 @@ ``` |
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
4581
4
41
62