Gathering detailed insights and metrics for ts-money
Gathering detailed insights and metrics for ts-money
Gathering detailed insights and metrics for ts-money
Gathering detailed insights and metrics for ts-money
@terra-money/terra.proto
JS and TS types relating to Protocol Buffers used by Terra project
@screeny05/ts-money
Maintained and improved version of ts-money.
@epicallan/money-ts
TypeScript library for type-safe and lossless encoding and manipulation of world currencies and precious metals
@abstract-money/ts-codegen
@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.
Typescript port of js-money. Implementation of the Money value object.
npm install ts-money
Typescript
Module System
Node Version
NPM Version
JavaScript (60.32%)
TypeScript (39.68%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
53 Stars
61 Commits
10 Forks
2 Watchers
3 Branches
2 Contributors
Updated on Mar 11, 2025
Latest Version
0.4.8
Package Id
ts-money@0.4.8
Unpacked Size
56.32 kB
Size
10.36 kB
File Count
13
NPM Version
8.11.0
Node Version
16.15.1
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
5
TS Money is a Typescript port of the great js-money package, which is an implementation of Martin Fowlers Money pattern.
1npm install ts-money
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
There are multiple options of what to pass into the constructor to create a new Money instance:
fromInteger
and fromDecimal
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}
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
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
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
Some changes have been made compared with the javascript version:
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
Money accepts currencies as case insensitive:
1const m1 = new Money(1, 'usd') 2const m2 = new Money(2, 'USD') 3const m3 = new Money(3, 'Usd')
1npm install
1npm run build
1npm test
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
8 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/20 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-30
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 More