X-Git-Url: http://git.scottworley.com/inverse-tax/blobdiff_plain/0993d859b81ccc99c56b5ded5ed85067dfe8196f..1c6e40691d86539d80018aa8fc495272b900c4c4:/tax.js?ds=sidebyside diff --git a/tax.js b/tax.js index 2b5bd4a..d26db5f 100644 --- a/tax.js +++ b/tax.js @@ -28,8 +28,8 @@ function apply_deductible(table, deductible) { function merge_tax_tables(t1, t2) { if (t1.length == 0) return t2; if (t2.length == 0) return t1; - [start1, end1, rate1] = t1[0]; - [start2, end2, rate2] = t2[0]; + const [start1, end1, rate1] = t1[0]; + const [start2, end2, rate2] = t2[0]; if (start1 == start2) { if (end1 == end2) { return [[start1, end1, rate1 + rate2]].concat(merge_tax_tables(t1.slice(1), t2.slice(1))); @@ -47,3 +47,44 @@ function merge_tax_tables(t1, t2) { } return merge_tax_tables(t2, t1); } + +function invert(table) { + if (table.length == 0) return x => x; + + // Here we solve + // net = m * gross + b + // for gross: + // net - b = m * gross + // (net - b) / m = gross + // and the calculate the inverse's bounds + + const ms = table.map(([start, end, rate]) => 1 - rate); + const full_brackets = [[0]].concat(table.map(([start, end, rate]) => (end - start) * rate)).slice(0, table.length); + function sum_lower_brackets(remaining_brackets, acc = 0) { + if (remaining_brackets.length == 0) return []; + return [acc + remaining_brackets[0]].concat(sum_lower_brackets(remaining_brackets.slice(1), acc + remaining_brackets[0])); + } + const bs = sum_lower_brackets(full_brackets).map((lower_brackets, i) => { + const [start, end, rate] = table[i]; + // Finding b: + // net = gross - lower_brackets - rate * (gross - start) + // net = gross - lower_brackets - rate * gross + rate * start + // net = gross - rate * gross - lower_brackets + rate * start + // net = (1 - rate) * gross - lower_brackets + rate * start + // net = m * gross - lower_brackets + rate * start + // \_____________________________/ - here is b + return rate * start - lower_brackets; + }); + const inverse_table = table.map(([start, end, rate], i) => { + const m = ms[i]; + const b = bs[i]; + return [(start - b) / m, (end - b) / m, m, b]; + }); + return function(net) { + for (const [start, end, m, b] of inverse_table) { + if (start < net && net < end) { + return (net - b) / m; + } + } + }; +}