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

safe-money

v1.0.0-beta.0

Published

safe-money in typescript, based on: https://ren.zone/articles/safe-money

Readme

safe-money

Build Status

An implementation of safe-money in typescript.

⚠️ WARNING: This implementation hasn't been battle-tested yet. Use with caution.

This software is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright holder or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.

Documentation

Getting started

Install safe-money with npm or yarn:

yarn add safe-money
npm install safe-money

Examples

Dense values

Dense values can represent arbitrary fractions of currency units.

import { Dense, Rational } from "safe-money";

// exact arithmetic
Dense.fromDecimal("0.2", "EUR")
  .add(Dense.fromDecimal("0.1", "EUR"))
  .toDecimal(); // "0.3"

// based on rationals
const x = Dense.of(Rational.of(1, 3));
x.toDecimal(); // "0.333333333333"
x.mul(Rational.nat(3)).toDecimal(); // "1.0"

// mixing currencies raises a type error
Dense.fromDecimal("10", "EUR").add(Dense.fromDecimal("10", "USD")); // USD is not assignable to EUR

Discrete values

A discrete value can only represent an integer multiple of a currency unit.

A discrete value is based on a scale. The scale is the ratio between the chosen unit and the main currency unit.

import { Discrete, Rational } from "safe-money";

const euroCent = Discrete.scale("EUR", "cent", Rational.of(100, 1)); // 100 cents in 1 euro
const euro = Discrete.scale("EUR", "euro", Rational.of(1, 1)); // 1 euro in 1 euro
const usdCent = Discrete.scale("USD", "cent", Rational.nat(100)); // 100 cents in 1 dollar

Discrete.of(1337, euroCent).dense().toDecimal(); // "13.37"

Discrete.of(42, euroCent).add(Discrete.of(1, usdCent)); // type error
Discrete.of(1137, euroCent).add(Discrete.of(23, euro)); // type error

Conversions

To convert between currencies you can use an ExchangeRate:

import * as Money from "safe-money";

const a = Money.Dense.fromDecimal("0.2", "EUR");
const b = Money.Dense.fromDecimal("0.1", "USD");
const eurToUsd = Money.exchangeRate("EUR", "USD", Rational.fromDecimal("1.12"));

Money.exchange(eurToUsd, a).add(b); // ok
Money.exchange(eurToUsd, b); // type error

To convert a dense value to a discrete value you can use floor, ceil or round. Those operations will return the closest discrete value and a dense remainder.

const x = Dense.fromDecimal("1.337", "EUR");
const [value, remainder] = Money.round(x, euroCent);
value.toDecimal(); // "1.34"
remainder.toDecimal(); // "-0.003"

const [value, remainder] = Money.floor(x, euroCent);
value.toDecimal(); // "1.33"
remainder.toDecimal(); // "0.007"

// mismatched units raise a type error
Money.floor(Dense.fromDecimal("1", "EUR"), usdCent);

License

Copyright © 2020, Stefan Oestreicher and contributors.

Distributed under the terms of the BSD-3-Clause license.