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

@cuppy/core

v1.1.0

Published

A lightweight currency display component with automatic inflation adjustment.

Downloads

14

Readme

Cuppy

Cuppy is a zero-dependency TypeScript library for declaratively formatting monetary values across your webpage. It supports automatic adjustment of historical currency amounts for inflation based on the Consumer Price Index. (“Cuppy” is how “CPI” would be pronounced if economics was any fun.)

Usage

Basic usage

Formatting

To mark an element as a Cuppy, add the data-cuppy attribute to it and provide the monetary value as the element’s text content or using the data-value attribute:

<!-- Via innerText -->
<span data-cuppy>25400000000</span>

<!-- Via data-value -->
<span data-cuppy data-value="25400000000"></span>

<!-- Using underscores to separate thousands -->
<span data-cuppy>25_400_000_000</span>
<span data-cuppy data-value="25_400_000_000"></span>

Then in your JavaScript, call the cuppy function to format all Cuppies on the page:

import cuppy from "@cuppy/core";

cuppy();

This will format the Cuppy using the default options for the user’s preferred locale.

Inflation adjustment

To adjust the value for inflation, you must install a dataset for the Consumer Price Index. For example, the @cuppy/cpi-u package provides data for the United States Consumer Price Index for All Urban Consumers (CPI-U) all the way back to 1913. You can install it like this:

npm i @cuppy/cpi-u

and then load it in your JavaScript:

import cuppy from "@cuppy/core";
import cpiU from "@cuppy/cpi-u";

cuppy.dataset(cpiU);

cuppy();

Then in your HTML, set the data-from and optionally data-to attributes to the years you want to adjust between and voilà!

<span data-cuppy data-from="1969" data-to="2020">25400000000</span>
<!-- $179B -->

Advanced usage

You can customize almost every aspect of the formatting by setting attributes on the element:

<span
  data-cuppy
  data-from="1969"
  data-to="2020"
  data-hint="to"
  data-hint-display="inline"
  data-year-display="always"
  data-hint-year-display="ifFallback"
  data-number-style="compactShort"
  data-currency-style="name"
  data-sign-display="always"
  data-use-grouping
  data-precision-mode="decimalPlaces"
  data-min-digits="4"
  data-max-digits="5"
  data-inflation-dataset="cpiU"
  data-locale="en-NZ"
>
  25_400_000_000
</span>

Cuppy uses Intl.NumberFormat under the hood, so you can use any of the options supported by that API. See the MDN documentation for more information.

You can also configure the default values for these options globally:

import cuppy from "@cuppy/core";
import cpiU from "@cuppy/cpi-u";

// Set global defaults
cuppy.options.hint = "to";
cuppy.options.currencyStyle = "code";
cuppy.options.numberStyle = "compactShort";
cuppy.options.useGrouping = false;
cuppy.options.maxDigits = 3;

// Load inflation dataset
cuppy.dataset(cpiU);

// Format all Cuppies on the page!
cuppy();

Note that per-element attributes will override global defaults.

You can modify how the year and hint are displayed by setting the yearFormatter and inlineHintFormatter options, respectively:

cuppy.options.yearFormatter = (value, year) => `${value} in ${year}`;
cuppy.options.inlineHintFormatter = (value, hint) => `${value} (${hint})`;

This will result in the following output:

<span
  data-cuppy
  data-from="1969"
  data-hint-display="inline"
  data-year-display="always"
  data-hint-year-display="ifFallback"
  data-currency-style="name"
  data-min-digits="4"
  data-max-digits="5"
>
  25_400_000_000
</span>
<!-- 25.40 billion US dollars in 1969 (210.88 billion US dollars in 2023) -->

Browser support

Cuppy supports all modern browsers supporting ES6 and above. The availability of some options may vary depending on the browser. See the MDN documentation for more information.

Contributing

If you like Cuppy and want to help make it better, please consider contributing! It can be as simple as opening an issue to report a bug or suggest an improvement, or as involved as implementing a new feature or fixing a bug yourself. For more information, see CONTRIBUTING.md.

License

Cuppy is licensed under the MIT License.