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

abbreviate-currency

v1.0.2

Published

Abbreviate currency values in a configurable, locale-friendly way.

Downloads

263

Readme

AbbreviateCurrency

npm version CircleCI Maintainability Test Coverage


Abbreviate currency values in a configurable, locale-friendly way. No dependencies!

Example Usage:

import { AbbreviateCurrency } from 'abbreviate-currency'

const value = 75300.55
const americanEnglishUsd = new AbbreviateCurrency({ language: 'en-US' })
const frenchCanadianEuros = new AbbreviateCurrency({ language: 'fr-CA', currency: 'EUR' })

americanEnglishUsd.transform(value) // -> '$75.3K'
frenchCanadianEuros.transform(value) // -> '75,3K €'

Quick Start

Install

$ npm install abbreviate-currency

Or download a release.

Class Properties

defaultConfig

The default config object used to construct new instances. Changes made to defaultConfig will only affect instances constructed after the change, and are not inherited by previously-constructed instances.

Config Object

| Property | Default | Description | | :--- | :--- | :--- | | language | navigator.language | BCP 47 language tag; e.g., 'en-US' | | currency | 'USD' | ISO 4217 currency code; e.g., EUR. | | useLowerCaseSymbols | false | Determines if digit symbols should be capitalized or not. | | digitGroups | See below. | Array of DigitGroups. |

DigitGroup

| Property | Description | | :--- | :--- | | symbol | String. Symbol to append to the value when abbreviated; e.g., 'K' is used to abbreviate 1,000 in "$1K". | | digits | Number. How many digit places for which this abbreviation should apply; e.g., 1,000 is 4 digits. |

The default digitGroups array is as follows:

[
    { symbol: 'K', digits: 4 }, // 1,000+ (4 digits or more.)
    { symbol: 'M', digits: 7 }, // 1,000,000+ (7 digits or more.)
    { symbol: 'B', digits: 10 }, // 1,000,000,000+ (10 digits or more.)
    { symbol: 'T', digits: 13 } // 1,000,000,000,000+ (13 digits or more.)
  ]

Instantiation

Instances are immutable. Values shown below are the defaults. Defaults can be overridden by modifying AbbreviateCurrency.defaultConfig. Changes to the default config will not be inherited by any previous class instances, as they are immutable.

Instance Methods

transform(value)

Takes a number or number-like string and abbreviates it.

en-US/USD Abbrevations:

                    0 => '$0'
                0.001 => '$0'
                0.005 => '$0.01'
                    1 => '$1'
                 9.01 => '$9.01'
                   10 => '$10'
                 10.5 => '$11'
                1,000 => '$1K'
                1,999 => '$1.9K'
               19,999 => '$19.9K'
              199,999 => '$199K'
  199,999,999,999,999 => '$199T'
1,999,999,999,999,999 => '$1,999T'

config()

Returns the configuration object used when constructing the AbbreviateCurrency instance.

A Quick Note

I have not done much testing with non-Western locales, nor with any non-Hindu–Arabic numeral systems. If you'd like to help out, please make an issue or a PR.