Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #5 - #hacktoberfest #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

> Transforms numbers written as words (in German language) into numbers

> Now it also does the reverse transformation (numbers into german words)

> Umwandlung von Zahlen als Wörter in Zahlen

> Example: "Mario ist dreiunddreißig Jahre alt" -> "Mario ist 33 Jahre alt"
Expand All @@ -19,10 +21,14 @@ npm install word-to-number --save
# Usage

```javascript
const wordToNumber = require("word-to-number");
const convertionOfNumber = require("word-to-number");

console.log(wordToNumber("eintausenddreihundertsiebenunddreißig"));
console.log(convertionOfNumber("eintausenddreihundertsiebenunddreißig"));
// "1337"

console.log(convertionOfNumber("999999"));
//

```

# License
Expand Down
51 changes: 50 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ const numberDictionary = {
hundert: "100",
einhundert: "100",
tausend: "1000",
eintausend: "1000"
eintausend: "1000"//,
//million: "1000000"
};

var maxMagnitudeOrder = 4;// 7

function flatMap(array) {
let a = [];
array.forEach(e => {
Expand Down Expand Up @@ -88,6 +91,50 @@ function mapWordToNumber(word) {
return sumNumbers(array);
}

function mapNumberToWord(number) {
number = cleanNumber(number);
var dictionary = Object.entries(numberDictionary).sort((el, el1) => el[1] > el1[1]);

if (dictionary.find(el => el[1] == number)) {
return dictionary.find(el => el[1] == number)[0];
}

var charArrayNumber = number.toString();
var magnitudeOrder = charArrayNumber.length;
var magnitudeBase = Math.pow(10, magnitudeOrder - 1);
var diffMagnitudes = magnitudeOrder - maxMagnitudeOrder;

if ([1, 2].indexOf(diffMagnitudes) != -1) {
return mapNumberToWord(charArrayNumber.substr(0, diffMagnitudes + 1)) + 'tausend' +
mapNumberToWord(charArrayNumber.substr(diffMagnitudes + 1));

} else if (diffMagnitudes > 2) {

return "Entschuldigung, wir konvertieren immer noch nicht so große Zahlen";

} else if (magnitudeOrder == 2) {

return dictionary.find(el => el[1] == charArrayNumber[1])[0] +
((number > 20) ? 'und' : '')
+ dictionary.find(el => el[1] == (charArrayNumber[0] * magnitudeBase))[0];

} else {

var firstPart = (charArrayNumber[0] == 1) ? '' : dictionary.find(el => el[1] == charArrayNumber[0])[0];

var secondPart = dictionary.find(el => el[1] == magnitudeBase &&
((charArrayNumber[0] == 1 && [3, 4].indexOf(magnitudeOrder) != -1)
? el[0].startsWith('ein') : true)
)[0];

return firstPart + secondPart + 'und' + mapNumberToWord(charArrayNumber.substr(1));
}
}

function cleanNumber(number) {
return Number(number);
}

function sumNumbers(array) {
return array
.reduce((total, current) => {
Expand All @@ -106,6 +153,8 @@ function sumNumbers(array) {
module.exports = str => {
if (!str) {
return str;
} else if (!isNaN(str)) {
return mapNumberToWord(str);
}

let a = [str];
Expand Down