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

@zarpay/amounts

v0.3.0

Published

Exact amounts of money, tokens and commodities in JavaScript — a mirror of the amounts gem, built from its exported registry

Readme

@zarpay/amounts

Exact amounts of money, tokens and commodities in JavaScript and TypeScript, for the web and React Native. It mirrors the amounts gem: the Rails app exports its registry, and the client parses, computes, converts, serializes and displays amounts exactly as the gem does, on bigint atomic values, with no runtime dependencies.

Install

npm install @zarpay/amounts

ESM and CommonJS builds ship with TypeScript declarations. It needs bigint (every current browser, Node 18+, React Native 0.70+ on Hermes).

Versions match the gem. @zarpay/amounts 0.3.0 mirrors amounts 0.3.0, and both are released together. Use the same version on both sides: the manifest a newer gem writes may not be one an older client reads.

Export the registry from Rails

class AmountsManifestsController < ActionController::API
  def show
    render json: Amount.manifest(locales: I18n.available_locales)
  end
end

Amount.manifest writes the registered types, the default rates and, for each locale, every translation the gem looks up. The client never needs I18n. Serve it from an endpoint, or write it to a file at build time and import it.

Use it

import { Amounts, AmountError } from "@zarpay/amounts";
import manifest from "./amounts.json";

const amounts = Amounts.fromManifest(manifest);

const total = amounts.of("USDC", "1.50").add(amounts.parse("USDC|0.25"));
total.ui();                     // "$1.75"
JSON.stringify({ total });      // '{"total":"USDC|1.750000"}'
amounts.load(total.toHash());   // back from { v: 1, atomic: "1750000", symbol: "USDC" }

Constructing

amounts.of("USDC", 1_500_000n);                     // bigint: atomic units
amounts.of("USDC", "1.50");                         // string: whole units
amounts.of("USDC", 1.5);                            // number: whole units, through "1.5"
amounts.of("USDC", "1500000", { from: "atomic" });  // override the inference
amounts.parse("USDC|1.50");                         // the compact string toJSON writes
amounts.parseUi("1.500,50 $", "USD", { locale: "de" }); // what a person typed

JavaScript can't tell 1 from 1.0, so the gem's Integer-is-atomic rule becomes bigint is atomic.

Math, comparison and conversion

a.add(b); a.subtract(b);            // same type, or through a registered default rate
a.multiply("0.3333333");            // exact, then truncated toward zero
a.divide(3);                        // exact, then truncated toward zero
a.divide(b);                        // a Ratio: exact fraction, b.toFixed(4) → "3.3333"
a.abs(); a.negate();
a.compare(b);                       // -1 | 0 | 1 | null
a.equals(b);                        // same type and value, never converts
a.lessThan(b);                      // also lessThanOrEqual, greaterThan, greaterThanOrEqual
a.to("SOL");                        // through the default rate
a.to("GOLD", { rate: "0.00042" });  // through an explicit one
const [parts, remainder] = a.split(3);   // parts + remainder always equal a
a.allocate([1, 1, 2]);

Scalars and rates take a bigint, a number or a decimal string. No value ever goes through floating point.

Display

amount.ui();                                  // "$1.50"
amount.ui({ locale: "de" });                  // "$1.500,50", or the locale's template if it has one
amount.ui({ direction: "ceil" });             // round away from zero instead of toward it
amount.ui({ decorated: false });              // the number alone
amount.ui({ trimZeros: true });
amount.ui({ unit: "gram" });                  // a display unit: "31.10 g"
amount.ui({ compact: true });                 // "$1.5M"
amount.unitLabel({ count: 1 });               // "search"
amount.displayFormat();                       // "%u%n"
amount.inUnit("gram");                        // "31.1034768", exact
amount.decimal;                               // "1.5", exact
amount.formatted();                           // "1.500000"
amount.toString();                            // "USDC|1.50"

Amounts.fromManifest(manifest, { locale: "de" }) makes every call follow a locale when it doesn't name one, like the gem's Amount.localize = true.

Errors

Every error is an AmountError with a code: TYPE_MISMATCH, INVALID_INPUT, UNKNOWN_TYPE, NO_DEFAULT_RATE, INVALID_DISPLAY_UNIT, ZERO_DIVISION, INVALID_ARGUMENT, NOT_COMPARABLE, UNKNOWN_LOCALE or INVALID_MANIFEST. Branch on the code, not the message.

Parity with the gem

The design is in specs/: what is mirrored, the manifest format, and the interface. The tests run against a manifest the gem exported from tests/fixtures/registry.rb, and their expected values are the gem's output for it. CI exports that manifest again from each commit's gem and fails if it differs from the committed fixture.

The client formats from the exact value. Past about 15 significant digits the gem's ui goes through a Float and prints wrong digits; the client prints the right ones.

Development

npm install
npm test             # vitest
npm run lint         # eslint
npm run typecheck    # tsc --noEmit
npm run format       # prettier
npm run build        # tsup: dist/ (ESM, CJS, .d.ts)

After changing the gem's export, regenerate the fixture from gem/:

bundle exec ruby -Ilib ../client/tests/fixtures/export-manifest.rb