expense-split

0.3.0 • Public • Published

Expense split npm version

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)

Readme

Keywords

none

Package Sidebar

Install

npm i expense-split

Weekly Downloads

0

Version

0.3.0

License

Unlicense

Last publish

Collaborators

  • valeriangalliat