Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
71151e2
Remove lodash dependency
screeny05 Jan 20, 2023
0592d8d
Move ts-money to a modern build and test stack
screeny05 Aug 16, 2024
479e904
Fix npm build
screeny05 Aug 16, 2024
ffad7d1
patch dependencies
phlogisticfugu Jan 28, 2025
339aaea
apply prettier to currencies.ts
phlogisticfugu Jan 28, 2025
25ffa12
update list of currencies using CandisIO/ts-money
phlogisticfugu Jan 28, 2025
61571d9
switch tests to terminate after completion, add github workflow
phlogisticfugu Jan 28, 2025
82d0c7d
rename to use master branch
phlogisticfugu Jan 28, 2025
bc2b0dd
fix types in unit test, add typecheck to ci
phlogisticfugu Jan 28, 2025
cc92eb3
fix formatting of ci file
phlogisticfugu Jan 28, 2025
fc26a0f
add node v22 to the ci tests
phlogisticfugu Jan 28, 2025
f3552f2
Merge pull request #1 from phlogisticfugu/master
screeny05 Feb 26, 2025
38cf1be
remove unnecessary currency data
screeny05 Feb 26, 2025
b9892ea
fix allocation bug with macord161/ts-money#5
screeny05 Feb 26, 2025
7cfdccf
prepare 1.0.0 release
screeny05 Feb 26, 2025
d46a4b6
integrate changes from 0.7.0
screeny05 Feb 26, 2025
9b8ed36
prepare release 0.7.0
screeny05 Feb 26, 2025
40723bf
Merge branch 'master' into feature/remove-currency-data
screeny05 Feb 26, 2025
b7fc780
Merge pull request #2 from screeny05/feature/remove-currency-data
screeny05 Feb 26, 2025
9a2bae1
correct link to bundlejs in readme
screeny05 Feb 26, 2025
09bec24
fix wrong currency-type in readme
screeny05 Feb 26, 2025
cf9827a
use correct typescript declaration file for cjs entry-point
screeny05 Feb 26, 2025
6e2211f
add Money#fractionOf and Money#percentageOf
screeny05 May 13, 2025
a6e63eb
throw more descriptivve error, when trying to divide by zero
screeny05 Nov 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Continuous Integration

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18, 20, 22]

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install --include=dev

- name: Typescript checks
run: npm run typecheck

- name: Run tests
run: npm run test
29 changes: 0 additions & 29 deletions .github/workflows/main.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/node_modules/*
/nbproject/
*.iml
dist
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*
!build/**/*
!dist/**/*
!package.json
!*.md
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

10 changes: 10 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"printWidth": 88,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": false
}
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Copyright (c) 2014 David Kalosi
Copyright (c) 2017 Mathew Cormier
Copyright (c) 2022 Sebastian Langer

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand All @@ -20,4 +21,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
OTHER DEALINGS IN THE SOFTWARE.
103 changes: 44 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
# TS Money
# @screeny05/ts-money

[![NPM version](https://img.shields.io/npm/v/ts-money.svg)](https://www.npmjs.com/package/ts-money)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![](https://img.shields.io/npm/dm/ts-money.svg?style=flat-square)](https://www.npmjs.com/package/ts-money)
[![](https://img.shields.io/npm/dt/ts-money.svg?style=flat-square)](https://www.npmjs.com/package/ts-money)
[![NPM version](https://img.shields.io/npm/v/@screeny05/ts-money.svg?style=flat-square)](https://www.npmjs.com/package/@screeny05/ts-money)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg?style=flat-square)](https://opensource.org/licenses/MIT)
[![](https://img.shields.io/npm/dm/@screeny05/ts-money.svg?style=flat-square)](https://www.npmjs.com/package/@screeny05/ts-money)
[![](https://deno.bundlejs.com/?q=@screeny05/ts-money&badge&badge-style=flat-square)](https://bundlejs.com/?q=%40screeny05%2Fts-money)


TS Money is a Typescript port of the great [js-money](https://www.npmjs.com/package/js-money) package, which is an implementation of Martin Fowlers [Money pattern](http://martinfowler.com/eaaCatalog/money.html).
TS Money is a Typescript port of the great [js-money](https://www.npmjs.com/package/js-money) package, which is an implementation of Martin Fowlers [Money pattern](http://martinfowler.com/eaaCatalog/money.html).

## Install

```sh
npm install ts-money
npm install @screeny05/ts-money
```

## Differences from original [ts-money](https://github.com/macor161/ts-money)

- Currencies are now exported in a standalone object.
- Fixed bug with allocate method when a single allocation is zero. (thanks to [@dotpack](https://github.com/dotpack), see [PR #5](https://github.com/macor161/ts-money/pull/5))
- Drastically reduced bundle-size by getting rid of lodash and removing unnecessary currency-data.
- Instead of using currency-data like currency-name, symbols, etc. from this package you should rely on either your environments [Intl.DisplayNames](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames) API or the [currency-codes](https://www.npmjs.com/package/currency-codes) package.
- Added `Money.from(amount: Amount)` as shortcut for `Money.fromInteger(amount: Amount)`
- Added `Money#fractionOf()` and `Money#percentageOf()` (see [below](#basic-arithmetics))
- Migrated package to vitest and unbuild.
- Throws a more descriptive error when dividing by zero.

## Usage

First we need to import the library.

```typescript
import { Money, Currencies } from 'ts-money'
import { Money, Currencies } from '@screeny05/ts-money'
```

or in javascript:

```javascript
const TsMoney = require('ts-money')
const TsMoney = require('@screeny05/ts-money')
const Money = TsMoney.Money
const 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` and `fromDecimal` methods)

- amount as number, currency as string
- amount as number, currency as object
- object with amount and currency fields (only with `fromInteger` and `fromDecimal` methods)

Amounts can be supplied either as integers or decimal numbers.

Expand All @@ -59,29 +69,19 @@ The `Currency` interface hold the following properties:

```typescript
interface Currency {
symbol: string
name: string
symbol_native: string
decimal_digits: number
rounding: number
code: string
name_plural: string
decimal_digits: number
code: string
}
```

Ex:

```typescript
import { Currency } from 'ts-money'
import { Currency } from '@screeny05/ts-money'

const usd: Currency = {
"symbol": "$",
"name": "US Dollar",
"symbol_native": "$",
"decimal_digits": 2,
"rounding": 0,
"code": "USD",
"name_plural": "US dollars"
decimal_digits: 2,
code: 'USD',
}
```

Expand All @@ -95,34 +95,41 @@ const fiveEur = new Money(500, Currencies.EUR) // 5 EUR
// add
fiveEur.add(new Money(250, Currencies.EUR)) // 7.50 EUR

// subtract
// subtract
fiveEur.subtract(new Money(470, Currencies.EUR)) // 0.30 EUR

// multiply
fiveEur.multiply(1.2345) // 6.17 EUR
fiveEur.multiply(1.2345, Math.ceil) // 6.18 EUR

// divide
// divide
fiveEur.divide(2.3456) // 2.13 EUR
fiveEur.divide(2.3456, Math.ceil) // 2.14 EUR

// fraction calculation
fiveEur.fractionOf(new Money(750, Currencies.EUR)) // 0.6667

// percentage calculation (same as fraction but with rounding and precision)
fiveEur.percentageOf(new Money(750, Currencies.EUR)) // 67
fiveEur.percentageOf(new Money(750, Currencies.EUR), 2) // 66.67
fiveEur.percentageOf(new Money(750, Currencies.EUR), 2, 'floor') // 66.66
```

### Allocating funds

Will divide the funds based on the ratio without loosing any pennies.
Will divide the funds based on the ratio without losing any pennies.

```typescript
const tenEur = new Money(1000, Currencies.EUR)

// divide 10 EUR into 3 parts
const shares = tenEur.allocate([1,1,1])
const shares = tenEur.allocate([1, 1, 1])
// returns an array of Money instances worth [334,333,333]

// split 5 EUR 70/30
const fiveEur = new Money(500, Currencies.EUR)
const shares = fiveEur.allocate([70,30])
const shares = fiveEur.allocate([70, 30])
// returns an array of money [350,150]

```

### Comparison and equality
Expand Down Expand Up @@ -151,7 +158,6 @@ fiveEur.lessThan(sevenEur) // return true
fiveEur.lessThanOrEqual(fiveEur) // return true
```


## Modifications

Some changes have been made compared with the javascript version:
Expand All @@ -161,22 +167,16 @@ Some changes have been made compared with the javascript version:
Currencies are now exported in a standalone object:

```typescript
import { Money, Currencies } from 'ts-money'
import { Money, Currencies } from '@screeny05/ts-money'

Currencies.LTC = {
symbol: "Ł",
name: "Litecoin",
symbol_native: "Ł",
decimal_digits: 8,
rounding: 0,
code: "LTC",
name_plural: "Litecoins"
decimal_digits: 8,
code: 'LTC',
}

const m1 = new Money(12, 'LTC')
const m2 = new Money(234, Currencies.USD)
const m3 = new Money(543, Currencies.LTC)

```

### Case insensitive currencies
Expand Down Expand Up @@ -209,21 +209,6 @@ npm run build
npm 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.


[![Donate with Bitcoin](https://en.cryptobadges.io/badge/big/1A71NTVtocr1WG6qFuQjhbVsEwXC7pKB5R)](https://en.cryptobadges.io/donate/1A71NTVtocr1WG6qFuQjhbVsEwXC7pKB5R)

[![Donate with Ethereum](https://en.cryptobadges.io/badge/big/0x5cE72fB54733a15640AD23f8c8c296AadEeC53Cb)](https://en.cryptobadges.io/donate/0x5cE72fB54733a15640AD23f8c8c296AadEeC53Cb)

[![Donate with Monero](https://en.cryptobadges.io/badge/big/4AzAgF56m5ihvDR5ctPVUE1RH78JEMsBHc63yYokHXbYGUCWsxphsmsgKzUkoQKmk7Tv6CSr3MosZ1wTR1wfHGt2187nHgj)](https://en.cryptobadges.io/donate/4AzAgF56m5ihvDR5ctPVUE1RH78JEMsBHc63yYokHXbYGUCWsxphsmsgKzUkoQKmk7Tv6CSr3MosZ1wTR1wfHGt2187nHgj)



## License

[The MIT License](http://opensource.org/licenses/MIT)
Loading