All Projects → xxczaki → Cashify

xxczaki / Cashify

Licence: mit
💸 Lightweight currency conversion library, successor of money.js

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Cashify

Cash Cli
💰💰 Convert currency rates directly from your terminal!
Stars: ✭ 168 (-48.94%)
Mutual labels:  currency, money, exchange, npm
currency-converter
💰 Easily convert between 32 currencies
Stars: ✭ 16 (-95.14%)
Mutual labels:  money, converter, currency, exchange
Currencyconverter
Utilities for doing currency conversion with the Money library
Stars: ✭ 78 (-76.29%)
Mutual labels:  currency, money, converter
money
Crystal shard for dealing with money and currency conversion
Stars: ✭ 26 (-92.1%)
Mutual labels:  money, currency, exchange
stockholm
💵 Modern Python library for working with money and monetary amounts. Human friendly and flexible approach for development. 100% test coverage + built-in support for GraphQL and Protocol Buffers transports using current best-practices.
Stars: ✭ 26 (-92.1%)
Mutual labels:  money, currency, exchange
Currencyviewer
Short python framework that dynamically displays and converts the cryptocurrencies in your Kraken wallet into equivalents fiat money.
Stars: ✭ 13 (-96.05%)
Mutual labels:  currency, converter, exchange
django-prices-openexchangerates
openexchangerates.org support for django-prices
Stars: ✭ 33 (-89.97%)
Mutual labels:  money, currency, exchange
Length.js
📏 JavaScript library for length units conversion.
Stars: ✭ 292 (-11.25%)
Mutual labels:  converter, convert, npm
SwiftUI-Currency-Converter
Currency Converter project coded by SwiftUI and Swift5
Stars: ✭ 56 (-82.98%)
Mutual labels:  converter, currency
react-numeric
A react component for formatted number form fields
Stars: ✭ 30 (-90.88%)
Mutual labels:  money, currency
xbytes
Parse bytes to human readable sizes (4747) → ('4.75 KB') and vice versa.
Stars: ✭ 17 (-94.83%)
Mutual labels:  converter, convert
svg2vector
Online batch converter of SVG images to Android vector drawable XML resource files
Stars: ✭ 39 (-88.15%)
Mutual labels:  converter, convert
to-json-schema
Converts JS objects to JSON Schema
Stars: ✭ 83 (-74.77%)
Mutual labels:  converter, convert
tellerbot
Telegram Bot for over-the-counter trading
Stars: ✭ 17 (-94.83%)
Mutual labels:  currency, exchange
uniswap-python
🦄 The unofficial Python client for the Uniswap exchange.
Stars: ✭ 533 (+62.01%)
Mutual labels:  currency, exchange
Narvalo.NET
Applied functional patterns for C#. Money and Currency types. MVP framework. (Obsolete)
Stars: ✭ 16 (-95.14%)
Mutual labels:  money, currency
money-parser
Price and currency parsing utility
Stars: ✭ 26 (-92.1%)
Mutual labels:  money, currency
tex-equation-to-svg
Convert a TeX or LaTeX string to an SVG.
Stars: ✭ 34 (-89.67%)
Mutual labels:  converter, convert
FinanceKit
FinanceKit is a Framework for iOS and Mac to build apps working with financial data, like money, currencies, stocks, portfolio, transactions and other concepts.
Stars: ✭ 15 (-95.44%)
Mutual labels:  money, currency
react-local-currency
💵 💴Shows the price of your services in the customer's currency 💶 💷
Stars: ✭ 21 (-93.62%)
Mutual labels:  money, currency

Cashify 💸

Lightweight currency conversion library, successor of money.js

Build Status Coverage Status XO code style install size minified size Mentioned in Awesome Node.js


Motivation

This package was created, because the popular money.js library:

  • is not maintained (last commit was ~5 years ago)
  • has over 20 open issues
  • does not support TypeScript
  • has implicit globals
  • does not have any unit tests
  • has floating point issues

Highlights

Install

$ npm install cashify

Usage

With constructor

const {Cashify} = require('cashify');

const rates = {
	GBP: 0.92,
	EUR: 1.00,
	USD: 1.12
};

const cashify = new Cashify({base: 'EUR', rates});

const result = cashify.convert(10, {from: 'EUR', to: 'GBP'});

console.log(result); //=> 9.2

Without constructor

Using the Cashify constructor is not required. Instead, you can just use the convert function:

const {convert} = require('cashify');

const rates = {
	GBP: 0.92,
	EUR: 1.00,
	USD: 1.12
};

const result = convert(10, {from: 'EUR', to: 'GBP', base: 'EUR', rates});

console.log(result); //=> 9.2

Parsing

Cashify supports parsing, so you can pass a string to the amount argument and the from and/or to currency will be automatically detected:

const {Cashify} = require('cashify');

const rates = {
	GBP: 0.92,
	EUR: 1.00,
	USD: 1.12
};

const cashify = new Cashify({base: 'EUR', rates});

// Basic parsing
cashify.convert('€10 EUR', {to: 'GBP'});

// Full parsing
cashify.convert('10 EUR to GBP');

Alternatively, if you just want to parse a string without conversion, you can use the parse function, which returns an object with parsing results:

const {parse} = require('cashify');

parse('10 EUR to GBP'); //=> {amount: 10, from: 'EUR', to: 'GBP'}

Note: If you want to use full parsing, you need to pass a string with specific format:

10 usd to pln
12.5 GBP in EUR
3.1415 eur as chf

You can use to, in or as to separate the expression (case insensitive). Used currencies name case doesn't matter, as cashify will automatically convert them to upper case.

Integration with currency.js

currency.js is a small and lightweight library for working with currency values. It works great with cashify. In the following example we are using it to format the conversion result:

const {Cashify} = require('cashify');
const currency = require('currency.js');

const rates = {
	GBP: 0.92,
	EUR: 1.00,
	USD: 1.12
};

const cashify = new Cashify({base: 'EUR', rates});

const converted = cashify.convert(8635619, {from: 'EUR', to: 'GBP'}); // => 7944769.48

// Format the conversion result
currency(converted, {symbol: '€', formatWithSymbol: true}).format(); // => €7,944,769.48

API

Cashify({base, rates})

Constructor

base

Type: string

Base currency

rates

Type: object

Object containing currency rates (for example from an API, such as Open Exchange Rates)

convert(amount, {from, to, base, rates}) with and without constructor

Returns conversion result (number)

amount

Type: number or string

Amount of money you want to convert. You can either use a number or a string. If you choose the second option, you can take advantage of parsing and not specify from and/or to argument(s).

from

Type: string

Currency from which you want to convert. You might not need to specify it if you are using parsing.

to

Type: string

Currency to which you want to convert. You might not need to specify it if you are using parsing.

base

Type: string

Base currency

rates

Type: object

Object containing currency rates (for example from an API, such as Open Exchange Rates)

parse(expression)

Returns an object, which contains parsing results:

{
	amount: number;
	from: string | undefined;
	to: string | undefined;
}
expression

Type: string

Expression you want to parse, ex. 10 usd to pln or €1.23 eur

Migrating from money.js

With Cashify constructor:

- const fx = require('money');
+ const {Cashify} = require('cashify');

- fx.base = 'EUR';
- fx.rates = {
-	GBP: 0.92,
-	EUR: 1.00,
-	USD: 1.12
- };

+ const rates = {
+	 GBP: 0.92,
+	 EUR: 1.00,
+	 USD: 1.12
+ };

+ const cashify = new Cashify({base: 'EUR', rates});

- fx.convert(10, {from: 'GBP', to: 'EUR'});
+ cashify.convert(10, {from: 'GBP', to: 'EUR'});

With convert function:

- const fx = require('money');
+ const {convert} = require('cashify');

- fx.base = 'EUR';
- fx.rates = {
-	GBP: 0.92,
-	EUR: 1.00,
-	USD: 1.12
- };

+ const rates = {
+	 GBP: 0.92,
+	 EUR: 1.00,
+	 USD: 1.12
+ };

- fx.convert(10, {from: 'GBP', to: 'EUR'});
+ convert(10, {from: 'GBP', to: 'EUR', base: 'EUR', rates});

Floating point issues

When working with currencies, decimals only need to be precise up to the smallest cent value while avoiding common floating point errors when performing basic arithmetic.

Let's take a look at the following example:

const fx = require('money');
const {Cashify} = require('cashify');

const rates = {
	GBP: 0.92,
	USD: 1.12
};

fx.rates = rates;
fx.base = 'EUR';

const cashify = new Cashify({base: 'EUR', rates});

fx.convert(10, {from: 'EUR', to: 'GBP'}); //=> 9.200000000000001
cashify.convert(10, {from: 'EUR', to: 'GBP'}); //=> 9.2

As you can see, money.js doesn't handle currencies correctly and therefore a floating point issues are occuring. Even though there's just a minor discrepancy between the results, if you're converting large amounts, that can add up.

Cashify solves this problem the same way as currency.js - by working with integers behind the scenes. This should be okay for most reasonable values of currencies.

Related projects

  • currency.js - Lightweight javascript library for working with currency values.
  • cashify-rs - Cashify port for Rust.

License

MIT © Antoni Kepinski

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].