Skip to content

Commit

Permalink
Added deno support and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Jul 3, 2020
1 parent bc54c9a commit d8d7203
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ jobs:
with:
node-version: 12.x

- name: Use Deno 1.x
uses: denolib/setup-deno@v2
with:
deno-version: v1.x

- name: Use cached node_modules
uses: actions/cache@v1
with:
Expand Down Expand Up @@ -43,6 +48,9 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/lcov.info

- name: test with Deno
run: deno test test/deno.ts

- name: Build
run: yarn build
env:
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![codecov](https://codecov.io/gh/lmammino/financial/branch/master/graph/badge.svg)](https://codecov.io/gh/lmammino/financial)
[![Documentation](https://api.netlify.com/api/v1/badges/eca2653e-dcaa-41db-865c-ab635687e69d/deploy-status)](https://financialjs.netlify.app/)

A Zero-Dependency TypeScript / JavaScript financial utility library inspired by [numpy-financial](https://github.com/numpy/numpy-financial/) that can be used on both Node.js and the browser.
A Zero-Dependency TypeScript / JavaScript financial utility library inspired by [numpy-financial](https://github.com/numpy/numpy-financial/) that can be used on **Node.js**, **Deno** and **the browser**.

It does support the same functionality offered by `numpy-financial` but it only support scalar values (no numpy-like array values) and it does not support decimal values.

Expand Down Expand Up @@ -79,6 +79,18 @@ import { fv, pmt } from 'financial'
There's no `default` export in the ESM implementation, so you have to explicitely import the functionality you need, one by one.


### Use with Deno

Make sure you specify the version you prefer in the import URL:

```typescript
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts'
import * as f from 'https://deno.land/x/npm:financial@0.1.1/src/financial.ts'

assertEquals(f.fv(0.05 / 12, 10 * 12, -100, -100), 15692.928894335755)
```


## Implemented functions

- [X] `fv` (since v0.0.12)
Expand All @@ -104,6 +116,15 @@ Below is a list of commands you will probably find useful.
- `npm run test:watch` or `yarn test:watch`: runs the tests in watch mode


### Test with Deno

To test with Deno, run:

```bash
deno test test/deno.ts
```


## Contributing

Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "financial",
"description": "A Zero-dependency TypeScript/JavaScript port of numpy-financial",
"author": "Luciano Mammino <no@spam.com> (https://loige.co)",
"version": "0.1.0",
"version": "0.1.1",
"repository": {
"type": "git",
"url": "https://github.com/lmammino/financial.git"
Expand Down
60 changes: 60 additions & 0 deletions test/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* global Deno */
/* eslint-disable */
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts'
import * as f from 'https://deno.land/x/npm:financial@0.1.0/src/financial.ts'

Deno.test('fv()', () => {
assertEquals(f.fv(0.05 / 12, 10 * 12, -100, -100), 15692.928894335755)
})

Deno.test('pmt()', () => {
assertEquals(f.pmt(0.075 / 12, 12 * 15, 200000), -1854.0247200054619)
})

Deno.test('nper()', () => {
assertEquals(f.nper(0.07 / 12, -150, 8000), 64.07334877066185)
})

Deno.test('ipmt()', () => {
const principal = 2500
const periods = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
const ipmts = periods.map((per) => f.ipmt(0.0824 / 12, per, 1 * 12, principal))
assertEquals(ipmts, [
-17.166666666666668,
-15.789337457350777,
-14.402550587464257,
-13.006241114404524,
-11.600343649629737,
-10.18479235559687,
-8.759520942678298,
-7.324462666057678,
-5.879550322604295,
-4.424716247725826,
-2.9598923121998877,
-1.4850099189833388
])
const interestpd = ipmts.reduce((a, b) => a + b, 0)
assertEquals(interestpd, -112.98308424136215)
})

Deno.test('pv()', () => {
assertEquals(f.pv(0.05 / 12, 10 * 12, -100, 15692.93), -100.00067131625376)
})

Deno.test('irr()', () => {
assertEquals(f.irr([-100, 39, 59, 55, 20]), 0.2809484211599611)
assertEquals(f.irr([-100, 0, 0, 74]), -0.09549583034897252)
assertEquals(f.irr([-100, 100, 0, -7]), -0.08329966618495904)
assertEquals(f.irr([-100, 100, 0, 7]), 0.06205848562992961)
assertEquals(f.irr([-5, 10.5, 1, -8, 1]), 0.08859833852439172)
})

Deno.test('npv()', () => {
const rate = 0.08
const cashflows = [-40_000, 5000, 8000, 12000, 30000]
assertEquals(f.npv(rate, cashflows), 3065.2226681795255)

const initialCashflow = cashflows[0]
cashflows[0] = 0
assertEquals(f.npv(rate, cashflows) + initialCashflow, 3065.222668179529)
})

0 comments on commit d8d7203

Please sign in to comment.