0.3.0 • Published 8 years ago
expense-split v0.3.0
Expense split
Group expense splitting library.
Usage
const expenseSplit = require('expense-split')
const group = ['Michael', 'Trevor', 'Franklin']
const transactions = expenseSplit(group, [
{ name: 'Michael', amount: 7920 }, // Ammu-Nation
{ name: 'Trevor', amount: 2880 }, // Plane "rental"
{ name: 'Trevor', amount: 300 }, // Parachutes
{ name: 'Franklin', amount: 8000, for: ['Michael', 'Franklin'] }, // Sanchez
{ name: 'Franklin', amount: 50, for: ['Michael', 'Franklin'] } // Gas
])
Will output:
[
{ from: 'Trevor', to: 'Franklin', amount: 325 },
{ from: 'Trevor', to: 'Michael', amount: 195 }
]
Can also work with the light syntax:
const transactions = expenseSplit(group, [
['Michael', 7920], // Ammu-Nation
['Trevor', 2880], // Plane "rental"
['Trevor', 300], // Parachutes
['Franklin', 8000, ['Michael', 'Franklin']], // Sanchez
['Franklin', 50, ['Michael', 'Franklin']] // Gas
])
Also comes with a function to get aggregated balances for all users:
const balances = expenseSplit.getBalances(group, [
{ name: 'Michael', amount: 7920 }, // Ammu-Nation
{ name: 'Trevor', amount: 2880 }, // Plane "rental"
{ name: 'Trevor', amount: 300 }, // Parachutes
{ name: 'Franklin', amount: 8000, for: ['Michael', 'Franklin'] }, // Sanchez
{ name: 'Franklin', amount: 50, for: ['Michael', 'Franklin'] } // Gas
])
Will output:
{
Michael: -195,
Trevor: 520,
Franklin: -325
}
If you have a balances object, you can also compute the split directly:
const transactions = expenseSplit.fromBalances(balances)