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

money-types

v0.2.0

Published

Types for representing and manipulating money

Downloads

4

Readme

Money types for JavaScript and TypeScript

npm version Build Status Coverage Status Dependencies

This library contains implementations of useful types related to money, to work with currencies and amounts.

npm install money-types

Features and status

This is currently an early release that simply supports accurate representation of an amount of money.

  • Currency representation

    • CurrencyUnit with information about decimal places and rounding.
    • Currencies generated from Unicode CLDR
  • Money representation

    • Money for representing amounts with up to 15 digits of precision, for currencies with 2 decimal places that allows for values of up to 10 trillion (10 000 000 000 000).
    • BigMoney for representing amounts that need more than 15 digits of precision.

Currencies

Currencies are represented by their ISO-4217 three letter code. All currencies include information about their number of decimal digits and rounding, for both a default case and for the case when used in a cash scenario.

A currency unit contains the following data:

  • currencyUnit.code: string, the three letter ISO-4217 code
  • currencyUnit.decimalDigits: number, the number of digits this currency has in the decimal part of the amount. A value of 2 means that an amount of value is internally represented as 1.00.
  • currencyUnit.decimalRounding: number, rounding to apply to the decimal part. If specified this defines that the decimal part needs to be divisible by this number. So if a currency rounds to the nearest .50 this would be 50.
  • currencyUnit.cash: DecimalPrecision, contains decimalDigits and decimalRounding but as applied in a cash scenario.

Currencies are available as separate imports:

import { EUR, USD } from 'money-types/currency';

Money

  • static MoneyType.fromNumber(amount: number, currency: CurrencyUnit, options?: MoneyOptions): Money

    Create a money instance from the given number, rounding it according to the options, or to the number of digits of the currency.

  • static MoneyType.fromDecimal(amount: Decimal, currency: CurrencyUnit, options?: MoneyOptions): Money

    Create a money instance from the given decimal, rounding it according to the options, or to the number of digits of the currency.

  • static MoneyType.parse(value: string, currencies: Currencies): Money

    Parse a string representation of money, resolving the currency via the specified currencies object. Currencies currently need to support a single function: get(currency: string): CurrencyUnit | undefined.

import { Money } from 'money-types/money';
import { EUR, USD } from 'money-types/currency';

// Create directly
const m1 = Money.fromNumber(12.2, EUR);

// Specify a custom number of decimal digits
const m2 = Money.fromNumber(0.00012, USD, { decimalDigits: 5 });

// Parse the toString format, providing available currencies
const currencies = new Map();
currencies.set('EUR', EUR);
currencies.set('USD', USD);

const m2 = Money.parse('EUR 20', currencies);