Currency String Parser
The `parsecurrency` package provides a minimalist utility for parsing currency strings, extracting components such as the numerical value, currency symbol, decimal and group separators, and ISO code. Currently at version 1.1.1, the package was last updated over six years ago and appears to be abandoned, with no active development or maintenance. While it supports many common international currency formats, including Indian numbering, it has explicit limitations, notably an inability to process currencies with three decimal places or two-character group separators. Due to its age, it primarily supports CommonJS imports, and users should be aware of potential compatibility issues in modern ESM-only environments and the lack of ongoing security patches or feature enhancements.
Common errors
-
TypeError: parseCurrency is not a function
cause Attempting to use a named import syntax (e.g., `import { parseCurrency } from 'parsecurrency';`) with this CommonJS package, which exports a single function or default value.fixUse the CommonJS `require` syntax for Node.js projects: `const parseCurrency = require('parsecurrency');`. For ESM, try `import parseCurrency from 'parsecurrency';`. -
Unexpected parsing result for currency string
cause Input string contains a currency format (e.g., three decimal places, two-character group separator, or an uncommon locale) that the library does not explicitly support or handle correctly.fixReview the library's limitations and compare them with your input format. For unsupported formats, consider preprocessing the input, using a more robust internationalization library, or implementing custom parsing logic.
Warnings
- gotcha The 'parsecurrency' package has not been updated in over six years and appears to be abandoned. Users should be cautious about using it in new projects, as it may not receive security updates, bug fixes, or compatibility improvements for newer Node.js versions or JavaScript features.
- gotcha This package primarily supports CommonJS modules (`require()`). While it might be possible to import it into an ESM project using compatibility layers, direct native ESM support is not provided, which can lead to import issues or bundle size inefficiencies.
- gotcha The utility cannot correctly parse currency strings that include three decimal places, which is a common format in some regions (e.g., Kuwaiti Dinar).
- gotcha Currencies using a two-character group separator, such as the Swaziland Lilangeni, are not supported and will likely result in incorrect parsing or unexpected output.
Install
-
npm install parsecurrency -
yarn add parsecurrency -
pnpm add parsecurrency
Imports
- parseCurrency
import { parseCurrency } from 'parsecurrency';const parseCurrency = require('parsecurrency');
Quickstart
const parseCurrency = require('parsecurrency');
// Example 1: Standard international format with symbol and ISO code
const result1 = parseCurrency('$123,456.99USD');
console.log('Example 1:', result1);
/*
Output:
{
raw: '$123,456.99USD',
value: 123456.99,
integer: '123,456',
decimals: '.99',
currency: 'USD',
symbol: '$',
decimalSeparator: '.',
groupSeparator: ',',
sign: ''
}
*/
// Example 2: Negative value with symbol prefix
const result2 = parseCurrency('-¥578,349,027');
console.log('Example 2:', result2);
/*
Output:
{
raw: '-¥578,349,027',
value: -578349027,
integer: '-578,349,027',
decimals: '',
currency: '',
symbol: '¥',
decimalSeparator: '',
groupSeparator: ',',
sign: '-'
}
*/
// Example 3: European format with space as group separator and comma as decimal
const result3 = parseCurrency('10 000,00zł');
console.log('Example 3:', result3);
/*
Output:
{
raw: '10 000,00zł',
value: 10000,
integer: '10 000',
decimals: ',00',
currency: 'zł',
symbol: '',
decimalSeparator: ',',
groupSeparator: ' ',
sign: ''
}
*/