-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (220 loc) · 7.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* @typedef {number} LoanAmount - the amount being loaned
* @typedef {number} Rate - the interest rate in decimal form
* @typedef {number} Period - number of years for the loan
*
* @typedef {object} AmortizationFees
* @property {Array} principalFees - fees which are incurred only once
* @property {Array} periodFees - fees which are incurred every payment
*
* @typedef {object} AmortizationBase
* @property {LoanAmount} loanAmount
* @property {Rate} rate
* @property {Period} period
*
* @typedef {AmortizationBase & AmortizationFees} AmortizationWithFees
*/
export const nbsp = '\xa0'
/**
* Check if value is a number
* @type {(value: any) => boolean}
*/
export const isNumber = (value) => Number.isFinite(parseFloat(value))
/**
* Sum all numbers found in the array
* @type {(arr: Array) => number}
*/
export const sumArray = (arr) => arr.filter(isNumber).reduce((acc, e) => acc += parseFloat(e), 0)
/**
* Get monthly payments for a loan
* @type {(args: AmortizationWithFees) => number}
*/
export const amortizationWithFees = ({ loanAmount, rate, period, principalFees = [], periodFees = [] }) => _amortization({
loanAmount: loanAmount + sumArray(principalFees),
rate,
period
}) + sumArray(periodFees)
/**
* Get the loan to value ratio
* @arg {Object} args
* @arg {LoanAmount} args.loanAmount
* @arg {number} args.purchasePrice - the value of the object being loaned against
* @returns {number}
*/
export function LTVratio({ loanAmount, purchasePrice }) {
if (purchasePrice == 0) return 1
return roundDecimals(loanAmount / purchasePrice, 4)
}
/**
* Round n to the decimals specified
* @arg {number} n - the number to round
* @arg {number} [decimals = 2] - how many decimals the result will contain
* @returns {number}
*/
export function roundDecimals(n, decimals = 2) {
const rounding = decimals ? Math.pow(10, decimals) : 1
return Math.round(n * rounding) / rounding
}
/**
* Format a number in the Norwegian locale
* @arg {number} n - the number to format
* @arg {number} decimals - how many decimals the result will contain
* @returns {string}
*/
export function internationalize(n, decimals) {
// const formatter = new Intl.NumberFormat('no', {
// style: 'decimal',
// minimumFractionDigits: decimals,
// maximumFractionDigits: decimals,
// })
const formatter = new Intl.NumberFormat('en-US', {
style: 'decimal',
minimumFractionDigits: decimals,
maximumFractionDigits: decimals,
})
return formatter
.format(n) // get it into US locale to normalize it
.replace(/,/g, '') // drop all commas (thousand-separators)
.replace(/\./g, ',') // replace all periods with commas (decimal-separators)
.replace(/\B(?=(\d{3})+(?!\d))/g, nbsp) // reformat it to nb-NO
}
/**
* Convert a percentage to a rate
* @arg {number} n - the number to convert
* @returns {number}
*/
export function percentageToRate(n) {
const sigfig = n.toString().replace(/\D/, '').length
return Number((n / 100).toPrecision(sigfig))
}
/**
* Convert a rate to a percentage
* @arg {number} n - the number to convert
* @returns {number}
*/
export function rateToPercentage(n) {
const sigfig = parseInt(n.toString().replace(/\D/, ''), 10).toString().length
return Number((n * 100).toPrecision(sigfig))
}
/**
* @typedef {object} MinMax
* @property {number} min - the minimum bound
* @property {number} max - the maximum bound
*/
/**
* Clamp a value between min and max
* @type {(v: any, minmax: MinMax) => number}
*/
export const clamp = (v, { min, max }) => isNumber(v) ? Math.min(Math.max(v, min), max) : min
/**
* Format a number as Norwegian currency
* @type {(n: number, decimals?: number) => string}
*/
export const toMoney = (n, decimals = 0) => `${internationalize(n, decimals)}${nbsp}kr`
/**
* Vue 3 directive for toMoney
* @type [string, function]
*/
export const vMoney = ['money', (el, binding) => el.innerText = toMoney(binding.value, binding.arg)]
/**
* Format a number as a percentage
* @type {(n: number, decimals?: number) => string}
*/
export const toPercentage = (n, decimals = 2) => `${internationalize(n * 100, decimals)}${nbsp}%`
/**
* Vue 3 directive for toPercentage
* @type [string, function]
*/
export const vPercent = ['percent', (el, binding) => el.innerText = toPercentage(binding.value, binding.arg)]
/**
* Calculate the effective interest for a loan
* @arg {Object} args
* @arg {LoanAmount} args.loanAmount
* @arg {number} args.monthlyPayment - the loan's monthly payment
* @arg {Period} args.period
* @returns {number}
*/
export function effectiveInterest({ loanAmount, monthlyPayment, period }) {
const paymentsPerYear = 12.0
let high = 2.0
let low = 1.0
let guess = 0.0
let previousGuess = 0
let precision = 8
let guessLimit = Math.pow(10, -1 * precision)
do {
previousGuess = guess
guess = (high - low) / 2.0 + low
var prov = (monthlyPayment * guess * (1.0 - Math.pow(guess, period * paymentsPerYear))) /
(1.0 - guess) /
Math.pow(guess, period * paymentsPerYear + 1)
if (prov < loanAmount) high = guess
else low = guess
} while (Math.abs(guess - previousGuess) > guessLimit)
const foundRate = Math.pow(guess, paymentsPerYear)
return roundDecimals(foundRate - 1, 4)
}
/**
* Get monthly payments for a loan
* @arg {AmortizationBase} args
* @returns {number}
*/
export function _amortization({ loanAmount, rate, period }) {
// https://github.com/essamjoubori/finance.js
const ratePerPeriod = rate / 12
const buildNumerator = (numInterestAccruals) => ratePerPeriod * Math.pow((1 + ratePerPeriod), numInterestAccruals)
const numerator = buildNumerator(period * 12)
const denominator = Math.pow((1 + ratePerPeriod), period * 12) - 1
const am = loanAmount * (numerator / denominator)
return roundDecimals(am)
}
/**
* Get the month and year for an offset from a base date
* @arg {number} i - the offset index
* @arg {Date} d - the base date
* @returns {{ year: number, month: number }}
*/
export function dateAtIndex(i, d = new Date()) {
const offset = i + d.getMonth()
const yearsAhead = Math.floor(offset / 12)
const year = d.getFullYear() + yearsAhead
const month = offset - (yearsAhead * 12)
return { year, month }
}
/**
* Groups an array of objects by the attribute specified
* @type {(attr: string) => (arr: Array) => Object<string, Array>}
*/
export const _groupBy = attr => arr => arr.reduce((acc, e) => ((acc[e[attr]] = acc[e[attr]] || []).push(e), acc), {})
/**
* Creates a payback schedule for a loan
* @arg {AmortizationWithFees} args
* @returns { Array }
*/
export function paymentPlanFromToday({ loanAmount, rate, period, principalFees = [], periodFees = [] }) {
const months = period * 12
const monthlyRate = rate / 12
let remainder = loanAmount
const baseDownPayment = _amortization({ loanAmount, rate, period })
const date = new Date()
return Array.from({ length: months }, (_, i) => {
const feesPaid = sumArray(periodFees) + (i == 0 ? sumArray(principalFees) : 0)
const interest = Math.floor(remainder * monthlyRate)
const downPayment = Math.ceil(Math.min((baseDownPayment - interest), remainder))
remainder -= downPayment
return { downPayment, feesPaid, remainder, interest, ...dateAtIndex(i + 1, date) }
})
}
// not in use anywhere
// export function paymentPlanSerial({ loanAmount, rate, period, etableringsgebyr = 0, termingebyr = 0, depotgebyr = 0 }) {
// const months = period * 12
// const monthlyRate = rate / 12
// let remainder = loanAmount
// let downPayment = Math.ceil(loanAmount / months)
// return Array.from({ length: months }, () => {
// const interest = Math.floor(remainder * monthlyRate)
// downPayment = Math.ceil(Math.min(downPayment, remainder))
// remainder -= downPayment
// return { downPayment, remainder, interest }
// })
// }