npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nejs/currencyinfo

v1.1.0

Published

Zero dependency Intl Currency helper class

Downloads

20

Readme

CurrencyInfo

A powerful and flexible JavaScript library for handling currency formatting, detection, and localization.

Features

  • Currency formatting based on locale
  • Currency detection from formatted strings
  • Support for multiple currencies and locales
  • Caching mechanism for improved performance
  • Built on native Intl APIs for broad compatibility

Installation

Node JS

In a node js project, simply perform the following npm install command.

npm install @nejs/currencyinfo

Browser

In a browser, a script tag pointed here can get you a CDN served version of the latest build

<script type="module">
  const { CurrencyInfo } = await import('https://cdn.jsdelivr.net/gh/nyteshade/currencyinfo@main/dist/currencyinfo.mjs')

  // ... use CurrencyInfo here
</script>

Or if you want it to automatically be injected into the window, you can use the iffy variant.

<script src="https://cdn.jsdelivr.net/gh/nyteshade/currencyinfo@main/dist/currencyinfo.browser.js"></script>

Usage

Basic Usage

import { CurrencyInfo } from '@nejs/currencyinfo';

// or alternatively
const { CurrencyInfo } = require('@nejs/currencyinfo');

// or alternatively
const { CurrencyInfo } = await import('@nejs/currencyinfo');

// Format currency
const usd = CurrencyInfo.USD;
console.log(usd.format(1234.56)); // '$1,234.56'

// Strip formatting
console.log(usd.strip('$1,234.56')); // 1234.56

// Detect currency and locale
const detected = CurrencyInfo.detect('1 234,56 €');
console.log(detected.locale); // 'fr-FR'
console.log(detected.currencyInfo.currency); // 'EUR'

Creating Custom Instances

const gbp = new CurrencyInfo('GBP', 'en-GB');
console.log(gbp.format(1234.56)); // '£1,234.56'

Currency Detection

Basic detection looks for USD and CAD currencies in US and CA, for en, es, and fr languages. When detecting with these defaults, you need simply pass in a string to attempt detection

const result = CurrencyInfo.detect('$1.00')
console.log(result.locale) // en-US
console.log(result.currency) // USD

However the language, country and currency combinations are configurable

const result = CurrencyInfo.detect('$1,234.56', {
  currencies: ['USD', 'CAD', 'AUD'],
  languages: ['en'],
  countries: ['US', 'CA', 'AU']
});
console.log(result.locale); // 'en-US'
console.log(result.currencyInfo.currency); // 'USD'

While .detect() does accept number primitives, there isn't enough to go on in such cases. If you don't know if your value is a string or a number, you can provide an assume configuration. It can take either an instance of CurrencyInfo or an object with the keys for locale and currency, such as { locale: "en-US", currency: "USD" }

console.log(CurrencyInfo.detect(1)) // null

// here we use the static en-US USD CurrencyInfo instance
let result = CurrencyInfo.detect(1, { assume: CurrencyInfo.USD })
console.log(result?.locale) // 'en-US'
console.log(result?.currency) // 'USD'

// the result would be the same with
let result = CurrencyInfo.detect(1, { assume: {
  locale: 'en-US',
  currency: 'USD' 
}})

API

CurrencyInfo

Constructor

  • new CurrencyInfo(currency: string, locale: string, doNotThrow?: boolean)

Static Methods

  • checkForInputErrors(currency: string, locale?: string): error[]
  • detect(formatted: string, options?: DetectOptions): DetectResult
  • get(currency: string, locale: string, doNotThrow?: boolean): CurrencyInfo
  • isCurrencyInfo(value: any): boolean
  • validateCurrency(currency: string): boolean | TypeError
  • validateLocale(locale: string): boolean | RangeError
  • validateRuntime(): boolean | Error

Instance Methods

  • format(value: number): string
  • strip(formatted: string): number

Properties

  • currency: string
  • locale: string
  • `separators: { group: string, decimal: string, symbol: string }``
  • `symbol: { position: string, locator: Function }``

Pre-configured Instances

  • CurrencyInfo.USD: US Dollars (en-US)
  • CurrencyInfo.CAD: Canadian Dollars (fr-CA)
  • CurrencyInfo.enCAD: Canadian Dollars (en-CA)
  • CurrencyInfo.frCAD: Canadian Dollars (fr-CA)

Browser Support

This library use the built-in Intl API, which is supported in all modern browsers. For older browsers, you may need to use a polyfill. If you do not, an error will be raised when attemting to use the class.

License

MIT

Contributing

Contributors are welcome! Please submit a Pull Request.