forked from vschoettke/lohnsteuer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (70 loc) · 3.03 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
/*jslint node: true*/
"use strict";
var lohnsteuerFuncs = require("./lohnsteuer");
function resultsToNumbers(results) {
return Object.keys(results).reduce(function (res, key) {
res[key] = Number(String(results[key]));
return res;
}, {});
}
function resultsToStrings(results) {
return Object.keys(results).reduce(function (res, key) {
res[key] = String(results[key]);
return res;
}, {});
}
function chain(a, b) {
return function (...args) {
return a(b.apply(undefined, args));
};
}
var algorithms = {
"2006": {from: new Date(2006, 0, 1), exclusiveTo: new Date(2007, 0, 1)},
"2007": {from: new Date(2007, 0, 1), exclusiveTo: new Date(2008, 0, 1)},
"2008": {from: new Date(2008, 0, 1), exclusiveTo: new Date(2009, 0, 1)},
"2009": {from: new Date(2009, 0, 1), exclusiveTo: new Date(2010, 0, 1)},
"2010": {from: new Date(2010, 0, 1), exclusiveTo: new Date(2011, 0, 1)},
"2011BisNov": {from: new Date(2011, 0, 1), exclusiveTo: new Date(2011, 11, 1)},
"2011Dez": {from: new Date(2011, 11, 1), exclusiveTo: new Date(2012, 0, 1)},
"2012": {from: new Date(2012, 0, 1), exclusiveTo: new Date(2013, 0, 1)},
"2013": {from: new Date(2013, 0, 1), exclusiveTo: new Date(2014, 0, 1)},
"2014": {from: new Date(2014, 0, 1), exclusiveTo: new Date(2015, 0, 1)},
"2015BisNov": {from: new Date(2015, 0, 1), exclusiveTo: new Date(2015, 11, 1)},
"2015Dez": {from: new Date(2015, 11, 1), exclusiveTo: new Date(2016, 0, 1)},
"2016": {from: new Date(2016, 0, 1), exclusiveTo: new Date(2017, 0, 1)},
"2017": {from: new Date(2017, 0, 1), exclusiveTo: new Date(2018, 0, 1)},
"2018": {from: new Date(2018, 0, 1), exclusiveTo: new Date(2019, 0, 1)},
"2019": {from: new Date(2019, 0, 1), exclusiveTo: new Date(2020, 0, 1)},
"2020": {from: new Date(2020, 0, 1), exclusiveTo: new Date(2021, 0, 1)}
};
function algorithmByName(name, options) {
var lohnsteuerFunc = lohnsteuerFuncs["lohnsteuer" + name];
if (!lohnsteuerFunc) {
throw new Error("No german income tax algorithm for name " + String(name) + " available");
}
options = options || {};
if (options.asNumbers) {
return chain(resultsToNumbers, lohnsteuerFunc);
}
if (options.asStrings) {
return chain(resultsToStrings, lohnsteuerFunc);
}
return lohnsteuerFunc;
}
function algorithmForDate(date, options) {
var dateTs = new Date(date).getTime();
var foundAlgorithmName = Object.keys(algorithms).filter(function (algorithmName) {
var info = algorithms[algorithmName];
return dateTs >= info.from.getTime() && dateTs < info.exclusiveTo.getTime();
})[0];
if (!foundAlgorithmName) {
throw new Error("No german income tax algorithm for given date " + new Date(dateTs).toISOString() + " available");
}
return algorithmByName(foundAlgorithmName, options);
}
module.exports = {
algorithmForDate: algorithmForDate,
algorithmByName: algorithmByName,
resultsToNumbers: resultsToNumbers,
resultsToStrings: resultsToStrings
};