All files int2words_de.js

98.63% Statements 144/146
96.55% Branches 28/29
100% Functions 2/2
98.63% Lines 144/146

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 14712x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x     1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 125x 125x 125x 125x 125x 53x 53x 53x 125x 5x 5x 5x 123x 32x 32x 9x 32x 23x 23x 32x 40x 15x 15x 15x 14x 14x 14x 15x 25x 13x 13x 13x 12x 6x 6x 6x 6x 6x 40x 6x 6x 5x 6x 1x 1x 6x 3x 3x 6x 6x 40x 6x 72x 72x 125x 12x 12x 12x 12x 12x  
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
/* eslint-disable @cspell/spellchecker */
 
'use strict';
 
// MODULES //
 
var floor = require( '@stdlib/math/base/special/floor' );
var endsWith = require( '@stdlib/string/base/ends-with' );
var UNITS = require( './units.json' );
 
 
// VARIABLES //
 
var ONES = [ 'null', 'eins', 'zwei', 'drei', 'vier', 'fünf', 'sechs', 'sieben', 'acht', 'neun', 'zehn', 'elf', 'zwölf', 'dreizehn', 'vierzehn', 'fünfzehn', 'sechzehn', 'siebzehn', 'achtzehn', 'neunzehn' ];
var TENS = [ 'null', 'zehn', 'zwanzig', 'dreißig', 'vierzig', 'fünfzig', 'sechzig', 'siebzig', 'achtzig', 'neunzig' ];
 
 
// FUNCTIONS //
 
/**
* Pluralizes a word by adding a 'n' or 'en' suffix.
*
* @private
* @param {string} word - word to pluralize
* @returns {string} pluralized word
*/
function pluralize( word ) {
	if ( endsWith( word, 'e' ) ) {
		return word + 'n';
	}
	return word + 'en';
}
 
 
// MAIN //
 
/**
* Converts a number to a word representation in German.
*
* @private
* @param {number} num - number to convert
* @param {string} out - output string
* @returns {string} word representation
*
* @example
* var words = int2wordsDE( 1243, '' );
* // returns 'eintausendzweihundertdreiundvierzig'
*
* @example
* var words = int2wordsDE( 387, '' );
* // returns 'dreihundertsiebenundachtzig'
*
* @example
* var words = int2wordsDE( 100, '' );
* // returns 'einhundert'
*
* @example
* var words = int2wordsDE( 1421, '' );
* // returns 'eintausendvierhunderteinundzwanzig'
*
* @example
* var words = int2wordsDE( 100381, '' );
* // returns 'einhunderttausenddreihunderteinundachtzig'
*
* @example
* var words = int2wordsDE( -13, '' );
* // returns 'minus dreizehn'
*/
function int2wordsDE( num, out ) {
	var word;
	var rem;
	var i;
	if ( num === 0 ) {
		// Case: We have reached the end of the number and the number is zero.
		return out || 'null';
	}
	if ( num < 0 ) {
		out += 'minus ';
		num *= -1;
	}
	if ( num < 20 ) {
		rem = 0;
		if ( num === 1 && out.length === 0 ) {
			word = 'ein';
		} else {
			word = ONES[ num ];
		}
	}
	else if ( num < 100 ) {
		rem = num % 10;
		word = TENS[ floor( num / 10 ) ];
		if ( rem ) {
			word = ( ( rem === 1 ) ? 'ein' : ONES[ rem ] ) + 'und' + word;
			rem = 0;
		}
	}
	else if ( num < 1e3 ) {
		rem = num % 100;
		word = int2wordsDE( floor( num / 100 ), '' ) + 'hundert';
	}
	else if ( num < 1e6 ) {
		rem = num % 1e3;
		word = int2wordsDE( floor( num / 1e3 ), '' ) + 'tausend';
	}
	else {
		for ( i = 5; i < UNITS.length; i++ ) {
			if ( num < UNITS[ i ].VAL ) {
				rem = num % UNITS[ i-1 ].VAL;
				if ( floor( num / UNITS[ i-1 ].VAL ) === 1 ) {
					word = 'eine ' + UNITS[ i-1 ].DE;
				} else {
					word = int2wordsDE( floor( num / UNITS[ i-1 ].VAL ), '' ) + ' ' + pluralize( UNITS[ i-1 ].DE );
				}
				if ( rem ) {
					word += ' ';
				}
				break;
			}
		}
	}
	out += word;
	return int2wordsDE( rem, out );
}
 
 
// EXPORTS //
 
module.exports = int2wordsDE;