Giter Site home page Giter Site logo

justinvos / money-fns Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 1.0 327 KB

Zero-dependency Money library (BigInt, Typescript and FP-oriented).

Home Page: https://www.npmjs.com/package/money-fns

License: MIT License

JavaScript 1.39% TypeScript 98.61%
money bigint fp currency pure

money-fns's Introduction

money-fns

money-fns is a zero-dependency Money library built with BigInt and an FP-oriented design.

This package is side-effect free and is only 679 Bytes when gzipped and minified. Compare with other packages on Bundlephobia.

Installation

NPM:

npm i money-fns

Yarn:

yarn add money-fns

Usage

import {
  addAmounts,
  compareAmount,
  discountAmount,
  floatToAmount,
  formatAmount,
  isAmountNegative,
  isAmountPositive,
  isAmountZero,
  isValidAmount,
  multiplyAmount,
  scaleAmount,
  subtractAmount,
  sumAmounts,
} from "money-fns";

addAmounts("1.00", "2.00"); // '3.00'
subtractAmount("3.00", "2.00"); // '1.00'
multiplyAmount("5.00", "1.15"); // '5.75'
scaleAmount("5.00", 3n); // '15.00'

floatToAmount(4.507); // '4.51'

sumAmounts(["1.00", "2.00", "4.00"]); // '7.00'
discountAmount("100.00", 2.5); // '97.50'
discountAmount("200.00", 4.28); // '191.44'

isAmountPositive("12.00"); // true
isAmountNegative("-12.00"); // true
isAmountZero("0.00"); // true
compareAmount("1.00", "3.00"); // -1

isValidAmount("0"); // false
isValidAmount("0.00"); // true

formatAmount("1234.00", "USD"); // 1,234.00

Try now with RunKit

Amount type

Also exported is the Amount TypeScript type.

import { Amount } from "money-fns";

const price: Amount = "50.00";

It represents a valid money value to be used by these functions.

An Amount is defined as a number with exactly 2 subdecimal digits. E.g. 123.45 or 1.00

These values are passed around as strings but you can import the Amount type and use in your code for more specificity.

Acknowledgments

money-fns was heavily inspired by money-math. money-fns hopes to bring it's concepts and modernise them with BigInts and out-of-the-box Typescript support.

money-fns's People

Contributors

justinvos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

linecode

money-fns's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.