]>
Commit | Line | Data |
---|---|---|
1 | "use strict"; | |
2 | ||
3 | function parse_table(as_text) { | |
4 | function parse_line(line) { | |
5 | return line.trim().split(' ').filter(x => x !== '').map(x => parseFloat(x)); | |
6 | } | |
7 | return as_text.trim().split('\n').map(parse_line); | |
8 | } | |
9 | ||
10 | function tax_table_from_table(table) { | |
11 | return table.map(([start, rate], i, table) => | |
12 | [start, i < table.length - 1 ? table[i+1][0] : Infinity, rate / 100.0]); | |
13 | } | |
14 | ||
15 | function parse_tax_table(as_text) { | |
16 | return tax_table_from_table(parse_table(as_text)); | |
17 | } | |
18 | ||
19 | function sum(nums) { | |
20 | return nums.reduce((total, num) => total + num, 0); | |
21 | } | |
22 | ||
23 | function tax(table, income) { | |
24 | return sum(table.map(([start, end, rate]) => | |
25 | Math.max(0, Math.min(income, end) - start) * rate)); | |
26 | } | |
27 | ||
28 | function apply_deductible(table, deductible) { | |
29 | return table.map(([start, end, rate]) => [start + deductible, end + deductible, rate]); | |
30 | } | |
31 | ||
32 | function merge_tax_tables(t1, t2) { | |
33 | if (t1.length == 0) return t2; | |
34 | if (t2.length == 0) return t1; | |
35 | const [start1, end1, rate1] = t1[0]; | |
36 | const [start2, end2, rate2] = t2[0]; | |
37 | if (start1 == start2) { | |
38 | if (end1 == end2) { | |
39 | return [[start1, end1, rate1 + rate2]].concat(merge_tax_tables(t1.slice(1), t2.slice(1))); | |
40 | } | |
41 | if (end1 < end2) { | |
42 | return [[start1, end1, rate1 + rate2]].concat(merge_tax_tables(t1.slice(1), [[end1, end2, rate2]].concat(t2.slice(1)))); | |
43 | } | |
44 | return merge_tax_tables(t2, t1); | |
45 | } | |
46 | if (start1 < start2) { | |
47 | if (end1 <= start2) { | |
48 | return [t1[0]].concat(merge_tax_tables(t1.slice(1), t2)); | |
49 | } | |
50 | return [[start1, start2, rate1]].concat(merge_tax_tables([[start2, end1, rate1]].concat(t1.slice(1)), t2)); | |
51 | } | |
52 | return merge_tax_tables(t2, t1); | |
53 | } | |
54 | ||
55 | function invert(table) { | |
56 | // Here we solve | |
57 | // net = m * gross + b | |
58 | // for gross: | |
59 | // net - b = m * gross | |
60 | // (net - b) / m = gross | |
61 | ||
62 | const ms = table.map(([start, end, rate]) => 1 - rate); | |
63 | const full_brackets = [0].concat(table.map(([start, end, rate]) => (end - start) * rate)).slice(0, table.length); | |
64 | function sum_lower_brackets(remaining_brackets, acc = 0) { | |
65 | if (remaining_brackets.length == 0) return []; | |
66 | return [acc + remaining_brackets[0]].concat(sum_lower_brackets(remaining_brackets.slice(1), acc + remaining_brackets[0])); | |
67 | } | |
68 | const bs = sum_lower_brackets(full_brackets).map((lower_brackets, i) => { | |
69 | const [start, end, rate] = table[i]; | |
70 | // Finding b: | |
71 | // net = gross - lower_brackets - rate * (gross - start) | |
72 | // net = gross - lower_brackets - rate * gross + rate * start | |
73 | // net = gross - rate * gross - lower_brackets + rate * start | |
74 | // net = (1 - rate) * gross - lower_brackets + rate * start | |
75 | // net = m * gross - lower_brackets + rate * start | |
76 | // \_____________________________/ - here is b | |
77 | return rate * start - lower_brackets; | |
78 | }); | |
79 | const inverse_table = table.map(([start, end, rate], i) => { | |
80 | const m = ms[i]; | |
81 | const b = bs[i]; | |
82 | return [m * start + b, m * end + b, m, b]; | |
83 | }); | |
84 | return function(net) { | |
85 | for (const [start, end, m, b] of inverse_table) { | |
86 | if (start <= net && net <= end) { | |
87 | return (net - b) / m; | |
88 | } | |
89 | } | |
90 | return net; | |
91 | }; | |
92 | } |