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 🙏

© 2024 – Pkg Stats / Ryan Hefner

intl-currency-formatting

v1.0.0

Published

A lib for formatting monetary amounts implemented on ECMAScript Internationalization API

Downloads

268

Readme

intl-currency-formatting

The library provides a set of functions for formatting monetary amounts and supports a variety of options for customizing the output.

The library relies on the Intl.NumberFormat object to handle the differences in currency formatting between different locales. This object is built into JavaScript and it implements ECMAScript Internationalization API specification.

Usage

formatAsCurrency Function

The function is a factory function that takes a config object and returns a formatter function for formatting a monetary amount as a string representing the currency value.

import { formatAsCurrency, CurrencyFormatConfig, Formatter, Amount } from 'intl-currency-formatting';

const config: CurrencyFormatConfig = {
  showCents: true,
  currency: 'EUR',
  localization: 'de-DE'
};

const formatter: Formatter = formatAsCurrency(config);
const amount: Amount = 9999.99
const formattedAmount: string = formatter(amount);

console.log(formattedAmount);
// Output: 9.999,99 €

The formatAsCurrency function provides default handling for null, undefined, and NaN values.

When the formatter function created by formatAsCurrency is called with one of these values as the amount parameter, it returns a default value of "N/A".

However, the default value can be customized by providing a naValue option in the config object passed to formatAsCurrency. This allows you to specify a different value to be returned for null, undefined, or NaN amounts.

Here's an example that demonstrates the default handling:

import { formatAsCurrency, CurrencyFormatConfig, Formatter } from 'intl-currency-formatting';

const defaultFormatter: Formatter = formatAsCurrency({})
console.log(defaultFormatter(null));
// Output: "N/A"

const config: CurrencyFormatConfig = {
  naValue: '-'
};
const customFormatter: Formatter = formatAsCurrency(config);
console.log(formatter(null));
// Output: -

formatAsCurrencyWithStrictValidation Function

The function is similar to the formatAsCurrency function, but with an additional strict validation step. It performs strict validation on the amount parameter to ensure that it is a valid number, if it's not it throws an error.

import { formatAsCurrencyWithStrictValidation, Formatter, Amount } from 'intl-currency-formatting';

const formatter: Formatter = formatAsCurrencyWithStrictValidation({})
const amount: Amount = null;
try {
  const formattedAmount: string = formatter(amount);
  console.log(formattedAmount);
} catch (error) {
  console.error('Error:', error.message); // Output: Error: Invalid amount provided: "null"
}

formatFromCents Function

Same as formatAsCurrency, but amount is in cents

import { formatFromCents, Formatter, Amount } from 'intl-currency-formatting';

const formatter: Formatter = formatFromCents({});
const amount: Amount = 123456;

const formattedAmount: string = formatter(amount);
console.log(formattedAmount);
// Output: $1,234.56

formatFromCentsWithStrictValidation Function

Same as formatAsCurrencyWithStrictValidation but amount is in cents

Configuration

The CurrencyFormatConfig is used to configure the formatting behavior of the currency formatter functions in the library. It has the following properties:

  • showCents (boolean, optional): Specifies whether to include cents in the formatted output. If set to true, the cents will be included. If set to false, the cents will be omitted and the result will be rounded to the nearest integer. The default value is true.

  • numberOfCentDigits (number, optional): Specifies the number of decimal places to display for cents. This property allows you to customize the precision of the cents. For example, if set to 2, the cents will be displayed with two decimal places (e.g., 1.23). The default value is 2.

  • naValue (string, optional): Specifies the default value to be displayed when the amount is null or undefined. This property allows you to customize the placeholder value for missing or unavailable amounts. The default value is "N/A".

  • currency (string, optional): Specifies the currency symbol or code to be used in the formatted output. This property allows you to customize the currency symbol or code according to your requirements. It takes same values as Intl.FormatNumber currency options. The default value is "USD".

  • localization (string, optional): Specifies the locale to be used for formatting the currency. This property allows you to customize the formatting based on different locales. It takes same values as Intl.FormatNumber locale options. The default value is "en-US".

The default config object is used when one is not provided or some properties are omitted.

const config: CurrencyFormatConfig = {
  showCents: true,
  numberOfCentDigits: 2,
  naValue: "N/A",
  currency: "USD",
  localization: "en-US",
};

Available types

Amount Type

The type represents a monetary amount and can have one of the following three values.

export type Amount = number | null | undefined

Formatter Type

The type represents a function that takes an Amount as input and returns a formatted string representing the monetary amount. It is used to format monetary amounts according to a specific configuration.

Here's the definition of the Formatter type:

type Formatter = (amount: Amount) => string;

FormatterFactory Type

This is the type of all factory functions. It represents a function that creates a Formatter based on a given configuration.

type FormatterFactory = (config: CurrencyFormatConfig) => Formatter;

Errors

NotANumberError Errror

The error is thrown in Formatters with strict validation when the amount parameter passed to the formatting functions is not a number. The error message includes the value of the invalid amount.

For example

import { formatAsCurrencyWithStrictValidation, NotANumberError } from 'intl-currency-formatting';

const amount = null;

try {
  const formattedAmount = formatAsCurrencyWithStrictValidation({})(amount);
  console.log(formattedAmount);
} catch (error) {
  if (error instanceof NotANumberError) {
    console.error('Error:', error.message); // Output: Error: Invalid amount provided: "null"
  } else {
    console.error('An error occurred:', error.message);
  }
}