]> git.scottworley.com Git - inverse-tax/blobdiff - tax.js
Handle the fully-deductible case
[inverse-tax] / tax.js
diff --git a/tax.js b/tax.js
index d26db5fa15f0b8cf0cdd006fd4b19cc4f6e37076..d9bd34c15b03849b0440ce574469afc2db8f44f6 100644 (file)
--- a/tax.js
+++ b/tax.js
@@ -7,11 +7,15 @@ function parse_table(as_text) {
   return as_text.trim().split('\n').map(parse_line);
 }
 
-function parse_tax_table(as_text) {
-  return parse_table(as_text).map(([start, rate], i, table) =>
+function tax_table_from_table(table) {
+  return table.map(([start, rate], i, table) =>
     [start, i < table.length - 1 ? table[i+1][0] : Infinity, rate / 100.0]);
 }
 
+function parse_tax_table(as_text) {
+  return tax_table_from_table(parse_table(as_text));
+}
+
 function sum(nums) {
   return nums.reduce((total, num) => total + num, 0);
 }
@@ -49,8 +53,6 @@ function merge_tax_tables(t1, t2) {
 }
 
 function invert(table) {
-  if (table.length == 0) return x => x;
-
   // Here we solve
   //   net = m * gross + b
   // for gross:
@@ -86,5 +88,6 @@ function invert(table) {
         return (net - b) / m;
       }
     }
+    return net;
   };
 }