Skip to content

Commit 21ef0df

Browse files
authored
Merge pull request #1 from lezhumain/main
Fix call stack exceeded error and add balance checks in typescript
2 parents 328d874 + 0b726a2 commit 21ef0df

File tree

9 files changed

+403
-52
lines changed

9 files changed

+403
-52
lines changed

.github/workflows/npm.js.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3+
4+
name: Node.js CI
5+
6+
on: [pull_request]
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [18.x]
16+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
17+
18+
steps:
19+
- uses: actions/checkout@v3
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
cache: 'npm'
25+
- run: npm ci
26+
- run: npm run build --if-present
27+
- run: npm test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test.js
2+
test_utils.js
3+
*.js.map

index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ function simplifyDebts(transactions){
5555
let minMax = getMaxMinCredit();
5656
if(minMax[0] == undefined || minMax[1] == undefined) return;
5757
let min_value = Math.min(-transaction_map.get(minMax[0]), transaction_map.get(minMax[1]));
58-
59-
transaction_map.set(minMax[0], transaction_map.get(minMax[0]) + min_value);
60-
transaction_map.set(minMax[1], transaction_map.get(minMax[1]) - min_value);
61-
62-
let res = [minMax[0], minMax[1], min_value];
63-
splits.push(res);
64-
helper();
58+
59+
if(min_value !== 0) {
60+
transaction_map.set(minMax[0], transaction_map.get(minMax[0]) + min_value);
61+
transaction_map.set(minMax[1], transaction_map.get(minMax[1]) - min_value);
62+
63+
let res = [minMax[0], minMax[1], min_value];
64+
splits.push(res);
65+
helper();
66+
}
6567
}
6668

6769
settleSimilarFigures();

package-lock.json

Lines changed: 204 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
{
2-
"name": "splitwise-js-map",
3-
"version": "1.0.3",
4-
"description": "Minimize Cash Flow among a given set of friends who have borrowed money from each other. Given a number of friends who have to give or take some amount of money from one another. Design an algorithm by which the total cash flow among all the friends is minimized.",
5-
"main": "index.js",
6-
"scripts": {
7-
"test": "node test"
8-
},
9-
"repository": {
10-
"type": "git",
11-
"url": "git+https://github.com/ahtrahdis7/splitwise.git"
12-
},
13-
"dependencies": {
14-
},
15-
"keywords": [
16-
"splitwise",
17-
"javascript",
18-
"algorithm",
19-
"Javascript",
20-
"split-wise",
21-
"split-wize",
22-
"splitwize",
23-
"split"
24-
],
25-
"author": "Sidhartha Mallick",
26-
"license": "ISC",
27-
"bugs": {
28-
"url": "https://github.com/ahtrahdis7/splitwise/issues"
29-
},
30-
"homepage": "https://github.com/ahtrahdis7/splitwise#readme"
2+
"name": "splitwise-js-map",
3+
"version": "1.0.3",
4+
"description": "Minimize Cash Flow among a given set of friends who have borrowed money from each other. Given a number of friends who have to give or take some amount of money from one another. Design an algorithm by which the total cash flow among all the friends is minimized.",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "npx tsc",
8+
"test": "npm run build && node test",
9+
"test-ts": "npm run build && npx ts-node test.ts"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/ahtrahdis7/node-splitwise-js.git"
14+
},
15+
"keywords": [
16+
"splitwise",
17+
"javascript",
18+
"algorithm",
19+
"Javascript",
20+
"split-wise",
21+
"split-wize",
22+
"splitwize",
23+
"split"
24+
],
25+
"author": "Sidhartha Mallick",
26+
"license": "ISC",
27+
"bugs": {
28+
"url": "https://github.com/ahtrahdis7/node-splitwise-js/issues"
29+
},
30+
"homepage": "https://github.com/ahtrahdis7/node-splitwise-js#readme",
31+
"devDependencies": {
32+
"ts-node": "^10.9.1",
33+
"typescript": "^5.1.3"
3134
}
32-
35+
}

test.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {checkBalance, SplitwiseInputItem} from "./test_utils";
2+
3+
// @ts-ignore
4+
const Splitwise = require('./index');
5+
6+
let splits;
7+
8+
let input: SplitwiseInputItem[] = [
9+
{
10+
'paidBy': 'A',
11+
'paidFor': { 'B': 300, 'C': 40, 'D': 30 }
12+
},
13+
{
14+
'paidBy': 'B',
15+
'paidFor': { 'A': 50, 'B': 100, 'C': 200 }
16+
}
17+
];
18+
splits = Splitwise(input);
19+
checkBalance(splits, input);
20+
console.log(splits);
21+
22+
23+
input = [
24+
{
25+
"paidBy": "A",
26+
"paidFor": {
27+
"B": 14.26,
28+
"C": 14.26,
29+
"D": 14.26
30+
}
31+
},
32+
{
33+
"paidBy": "B",
34+
"paidFor": {
35+
"C": 7,
36+
"D": 7
37+
}
38+
}
39+
];
40+
splits = Splitwise(input);
41+
checkBalance(splits, input);
42+
console.log(splits);

0 commit comments

Comments
 (0)