Installations
npm install ts-money
Developer
macor161
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
16.15.1
NPM Version
8.11.0
Statistics
48 Stars
61 Commits
9 Forks
3 Watching
3 Branches
2 Contributors
Updated on 26 Nov 2024
Languages
JavaScript (60.32%)
TypeScript (39.68%)
Total Downloads
Cumulative downloads
Total Downloads
2,302,363
Last day
-10.7%
1,801
Compared to previous day
Last week
6.4%
9,827
Compared to previous week
Last month
19%
39,106
Compared to previous month
Last year
-15.2%
430,366
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
5
TS Money
TS Money is a Typescript port of the great js-money package, which is an implementation of Martin Fowlers Money pattern.
Install
1npm install ts-money
Usage
First we need to import the library.
1import { Money, Currencies } from 'ts-money'
or in javascript:
1const TsMoney = require('ts-money') 2const Money = TsMoney.Money 3const Currencies = TsMoney.Currencies
Creating a new instance
There are multiple options of what to pass into the constructor to create a new Money instance:
- amount as number, currency as string
- amount as number, currency as object
- object with amount and currency fields (only with
fromInteger
andfromDecimal
methods)
Amounts can be supplied either as integers or decimal numbers.
Instances of Money are immutable and each arithmetic operation will return a new instance of the object.
When using decimals the library will allow only decimals with the precision allowed by the currencies smallest unit.
1const fiveEur = new Money(500, Currencies.EUR) 2const tenDollars = Money.fromInteger({ amount: 1000, currency: Currencies.USD }) 3const someDollars = Money.fromDecimal(15.25, 'USD') 4 5// the following will fail and throw an Error since USD allows for 2 decimals 6const moreDollars = Money.fromDecimal(15.3456, Currencies.USD) 7// but with rounder function provider the following will work 8const someMoreDollars = Money.fromDecimal(15.12345, 'USD', Math.ceil)
The Currency
interface hold the following properties:
1interface Currency { 2 symbol: string 3 name: string 4 symbol_native: string 5 decimal_digits: number 6 rounding: number 7 code: string 8 name_plural: string 9}
Ex:
1import { Currency } from 'ts-money' 2 3const usd: Currency = { 4 "symbol": "$", 5 "name": "US Dollar", 6 "symbol_native": "$", 7 "decimal_digits": 2, 8 "rounding": 0, 9 "code": "USD", 10 "name_plural": "US dollars" 11}
Basic arithmetics
Arithmetic operations involving multiple objects are only possible on instances with the same currency and will throw an Error otherwise.
1const fiveEur = new Money(500, Currencies.EUR) // 5 EUR 2 3// add 4fiveEur.add(new Money(250, Currencies.EUR)) // 7.50 EUR 5 6// subtract 7fiveEur.subtract(new Money(470, Currencies.EUR)) // 0.30 EUR 8 9// multiply 10fiveEur.multiply(1.2345) // 6.17 EUR 11fiveEur.multiply(1.2345, Math.ceil) // 6.18 EUR 12 13// divide 14fiveEur.divide(2.3456) // 2.13 EUR 15fiveEur.divide(2.3456, Math.ceil) // 2.14 EUR
Allocating funds
Will divide the funds based on the ratio without loosing any pennies.
1const tenEur = new Money(1000, Currencies.EUR) 2 3// divide 10 EUR into 3 parts 4const shares = tenEur.allocate([1,1,1]) 5// returns an array of Money instances worth [334,333,333] 6 7// split 5 EUR 70/30 8const fiveEur = new Money(500, Currencies.EUR) 9const shares = fiveEur.allocate([70,30]) 10// returns an array of money [350,150] 11
Comparison and equality
Two objects are equal when they are of the same amount and currency. Trying to compare 2 objects with different currencies will throw an Error.
1const fiveEur = new Money(500, Currencies.EUR) 2const anotherFiveEur = new Money(500, Currencies.EUR) 3const sevenEur = new Money(700, Currencies.EUR) 4const fiveDollars = new Money(500, Currencies.USD) 5 6fiveEur.equals(fiveDollars) // return false 7fiveEur.equals(anotherFiveEur) // return true 8 9fiveEur.compare(sevenEur) // return -1 10sevenEur.compare(fiveEur) // return 1 11fiveEur.compare(anotherFiveEur) // return 0 12 13fiveEur.compare(fileDollars) // throw Error 14 15fiveEur.greaterThan(sevenEur) // return false 16fiveEur.greaterThanOrEqual(sevenEur) // return false 17fiveEur.lessThan(sevenEur) // return true 18fiveEur.lessThanOrEqual(fiveEur) // return true
Modifications
Some changes have been made compared with the javascript version:
Currencies object
Currencies are now exported in a standalone object:
1import { Money, Currencies } from 'ts-money' 2 3Currencies.LTC = { 4 symbol: "Ł", 5 name: "Litecoin", 6 symbol_native: "Ł", 7 decimal_digits: 8, 8 rounding: 0, 9 code: "LTC", 10 name_plural: "Litecoins" 11} 12 13const m1 = new Money(12, 'LTC') 14const m2 = new Money(234, Currencies.USD) 15const m3 = new Money(543, Currencies.LTC) 16
Case insensitive currencies
Money accepts currencies as case insensitive:
1const m1 = new Money(1, 'usd') 2const m2 = new Money(2, 'USD') 3const m3 = new Money(3, 'Usd')
Development
Install dependencies
1npm install
Build library
1npm run build
Run tests
1npm test
🎁 Thank you for your donations
TS Money is an open source library and is completely free to use.
If you find this project useful and would like to support its development, consider making a donation.
License
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
5 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
Reason
Found 1/20 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/macor161/ts-money/main.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:24
- Info: 0 out of 1 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 15 are checked with a SAST tool
Score
3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to ts-money
@terra-money/terra.js
The JavaScript SDK for Terra
@terra-money/feather.js
TypeScript SDK for Terra and Cosmos chains
@terra-money/wallet-types
This is a separate package because it is used for [`@terra-money/use-wallet`](https://www.npmjs.com/package/@terra-money/use-wallet), [`@terra-money/wallet-controller`](https://www.npmjs.com/package/@terra-money/wallet-controller), and [`@terra-money/wall
ng2-currency-mask
A very simple currency mask directive for Angular that allows using a number attribute with the ngModel. In other words, the model is a number, and not a string with a mask. It was tested in Angular version 13.