Repeating Amount
A well-tested, functional library for handling time-based conversions between repeating amounts
Installation
yarn add repeating-amount
Usage
import { Amount, RepeatPeriod, reduce } from "repeating-amount"
// import the units that you need
const {
yearly,
twiceAYear,
quarterly,
monthly,
fortnightly,
weekly,
daily,
hourly
} = RepeatPeriod
// Create your amounts
const breakfast = new Amount(5, daily)
const lunch = new Amount(20, weekly)
// You don't have to use the Amount constructor
// You can use an object literal, like below
const dinner = { value: 100, repeated: fortnightly }
// You can now add the amounts together
const foodBudgetForMonth = reduce(breakfast, lunch, dinner)(monthly)
console.log(foodBudgetForMonth) // => {value: 460.21, repeated: "monthly"}
// OR you can convert individual amounts
const breakfastBudgetPerMonth = reduce(breakfast)(monthly)
console.log(breakfastBudgetPerMonth) // => {value: 153.4, repeated: "monthly"}
// You can also override the fundamental unit values
const hourlyPay = new Amount(36, hourly)
const salary = reduce(hourlyPay)(yearly, { hoursPerDay: 8, daysPerWeek: 5 })
console.log(salary) // => {value: 75135.6, repeated: "yearly"}