-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
146 lines (123 loc) · 2.97 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
const numberDictionary = {
null: "0",
eins: "1",
zwei: "2",
zwo: "2",
drei: "3",
vier: "4",
fünf: "5",
sechs: "6",
sieben: "7",
acht: "8",
neun: "9",
zehn: "10",
elf: "11",
zwölf: "12",
sechzehn: "16",
siebzehn: "17",
zwanzig: "20",
dreißig: "30",
dreissig: "30",
vierzig: "40",
fünfzig: "50",
sechzig: "60",
siebzig: "70",
achtzig: "80",
neunzig: "90",
hundert: "100",
einhundert: "100",
tausend: "1000",
eintausend: "1000"
};
function flatMap(array) {
let a = [];
array.forEach(e => {
a = a.concat(e);
});
return a;
}
function insertSeparators(array, separator) {
const newArray = [];
let i = 1;
for (i; i < array.length; i++) {
newArray.push(array[i - 1]);
newArray.push(separator);
}
newArray.push(array[i - 1]);
return newArray;
}
function transformWithMultiplicand(str, index, multiplicand) {
let multiplier;
if (str.substr(0, index) === "ein") {
multiplier = 1;
} else {
multiplier = parseInt(mapWordToNumber(str.substr(0, index)), 10);
}
const rest = mapWordToNumber(str.substr(index + 7)) || 0;
return (multiplier * multiplicand + parseInt(rest, 10)).toString();
}
function mapWordToNumber(word) {
let index;
if (numberDictionary[word]) {
return numberDictionary[word];
}
for (const [digit, multiplicand] of [["tausend", 1000], ["hundert", 100]]) {
index = word.indexOf(digit);
if (index !== -1) {
return transformWithMultiplicand(word, index, multiplicand);
}
}
index = word.indexOf("zehn");
if (index !== -1 && word.endsWith("zehn")) {
return `${10 + parseInt(numberDictionary[word.substr(0, index)], 10)}`;
}
array = word.split("und");
if (array.length <= 1) {
return word;
}
return sumNumbers(array);
}
function sumNumbers(array) {
return array
.reduce((total, current) => {
if (!current) {
return total;
}
if (current === "ein") {
return total + 1;
}
return total + parseInt(numberDictionary[current], 10);
}, 0)
.toString();
}
module.exports = (str, smart = false) => {
if (!str) {
return str;
}
let a = [str];
[" ", ",", "."].forEach(separator => {
a = flatMap(
a.map(e => {
return insertSeparators(e.split(separator), separator);
})
);
});
const paragraph = a.map(mapWordToNumber).join("");
if (smart) {
// check for tax id
let regex = /(\d\s?\d\s?\d\s?\d\s?\d\s?\d\s?\d\s?\d\s?\d\s?\d\s?\d)/g;
let found = paragraph.match(regex);
if (found && found[0]) {
return paragraph.replace(found[0], found[0].replace(/\s/g, ''));
}
//check for zip codes
regex = /(\d\s?\d\s?\d\s?\d\s?\d)\s\D|(\d\s?\d\s?\d\s?\d\s?\d?\s?$)/;
found = paragraph.match(regex);
if (found && found[1]) {
return paragraph.replace(found[1], found[1].replace(/\s/g, ''));
} else if (found && found[2]) {
return paragraph.replace(found[2], found[2].replace(/\s/g, ''));
}
}
return paragraph;
};