numbers_nn.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { Engine } from '../../common/engine.js';
  2. import { NUMBERS as NUMB } from '../messages.js';
  3. function hundredsToWordsRo_(num, ordinal = false) {
  4. let n = num % 1000;
  5. let str = '';
  6. const count = Math.floor(n / 100);
  7. const ones = NUMBERS.ones[count];
  8. str += ones ? (count === 1 ? '' : ones) + 'hundre' : '';
  9. n = n % 100;
  10. if (n) {
  11. str += str ? 'og' : '';
  12. if (ordinal) {
  13. const ord = NUMBERS.special.smallOrdinals[n];
  14. if (ord) {
  15. return str + ord;
  16. }
  17. if (n % 10) {
  18. return (str +
  19. NUMBERS.tens[Math.floor(n / 10)] +
  20. NUMBERS.special.smallOrdinals[n % 10]);
  21. }
  22. }
  23. str +=
  24. NUMBERS.ones[n] ||
  25. NUMBERS.tens[Math.floor(n / 10)] + (n % 10 ? NUMBERS.ones[n % 10] : '');
  26. }
  27. return ordinal ? replaceOrdinal(str) : str;
  28. }
  29. function numberToWordsRo(num, ordinal = false) {
  30. if (num === 0) {
  31. return ordinal ? NUMBERS.special.smallOrdinals[0] : NUMBERS.zero;
  32. }
  33. if (num >= Math.pow(10, 36)) {
  34. return num.toString();
  35. }
  36. let pos = 0;
  37. let str = '';
  38. while (num > 0) {
  39. const hundreds = num % 1000;
  40. if (hundreds) {
  41. const hund = hundredsToWordsRo_(num % 1000, pos ? false : ordinal);
  42. if (!pos && ordinal) {
  43. ordinal = !ordinal;
  44. }
  45. str =
  46. hund +
  47. (pos
  48. ? ' ' +
  49. (NUMBERS.large[pos] + (pos > 1 && hundreds > 1 ? 'er' : '')) +
  50. (str ? ' ' : '')
  51. : '') +
  52. str;
  53. }
  54. num = Math.floor(num / 1000);
  55. pos++;
  56. }
  57. return ordinal ? str + (str.match(/tusen$/) ? 'de' : 'te') : str;
  58. }
  59. function numberToOrdinal(num, _plural) {
  60. return wordOrdinal(num);
  61. }
  62. function replaceOrdinal(str) {
  63. const letOne = NUMBERS.special.endOrdinal[0];
  64. if (letOne === 'a' && str.match(/en$/)) {
  65. return str.slice(0, -2) + NUMBERS.special.endOrdinal;
  66. }
  67. if (str.match(/(d|n)$/) || str.match(/hundre$/)) {
  68. return str + 'de';
  69. }
  70. if (str.match(/i$/)) {
  71. return str + NUMBERS.special.endOrdinal;
  72. }
  73. if (letOne === 'a' && str.match(/e$/)) {
  74. return str.slice(0, -1) + NUMBERS.special.endOrdinal;
  75. }
  76. if (str.match(/e$/)) {
  77. return str + 'nde';
  78. }
  79. return str + 'nde';
  80. }
  81. function wordOrdinal(num) {
  82. const ordinal = numberToWords(num, true);
  83. return ordinal;
  84. }
  85. function numericOrdinal(num) {
  86. return num.toString() + '.';
  87. }
  88. export const NUMBERS = NUMB({
  89. wordOrdinal: wordOrdinal,
  90. numericOrdinal: numericOrdinal,
  91. numberToWords: numberToWords,
  92. numberToOrdinal: numberToOrdinal
  93. });
  94. function onePrefix_(num, thd = false) {
  95. const numOne = NUMBERS.ones[1];
  96. return num === numOne ? (num === 'ein' ? 'eitt ' : thd ? 'et' : 'ett') : num;
  97. }
  98. function hundredsToWordsGe_(num, ordinal = false) {
  99. let n = num % 1000;
  100. let str = '';
  101. let ones = NUMBERS.ones[Math.floor(n / 100)];
  102. str += ones ? onePrefix_(ones) + 'hundre' : '';
  103. n = n % 100;
  104. if (n) {
  105. str += str ? 'og' : '';
  106. if (ordinal) {
  107. const ord = NUMBERS.special.smallOrdinals[n];
  108. if (ord) {
  109. return (str += ord);
  110. }
  111. }
  112. ones = NUMBERS.ones[n];
  113. if (ones) {
  114. str += ones;
  115. }
  116. else {
  117. const tens = NUMBERS.tens[Math.floor(n / 10)];
  118. ones = NUMBERS.ones[n % 10];
  119. str += ones ? ones + 'og' + tens : tens;
  120. }
  121. }
  122. return ordinal ? replaceOrdinal(str) : str;
  123. }
  124. function numberToWordsGe(num, ordinal = false) {
  125. if (num === 0) {
  126. return ordinal ? NUMBERS.special.smallOrdinals[0] : NUMBERS.zero;
  127. }
  128. if (num >= Math.pow(10, 36)) {
  129. return num.toString();
  130. }
  131. let pos = 0;
  132. let str = '';
  133. while (num > 0) {
  134. const hundreds = num % 1000;
  135. if (hundreds) {
  136. const hund = hundredsToWordsGe_(num % 1000, pos ? false : ordinal);
  137. if (!pos && ordinal) {
  138. ordinal = !ordinal;
  139. }
  140. str =
  141. (pos === 1 ? onePrefix_(hund, true) : hund) +
  142. (pos > 1 ? NUMBERS.numSep : '') +
  143. (pos
  144. ?
  145. NUMBERS.large[pos] + (pos > 1 && hundreds > 1 ? 'er' : '')
  146. : '') +
  147. (pos > 1 && str ? NUMBERS.numSep : '') +
  148. str;
  149. }
  150. num = Math.floor(num / 1000);
  151. pos++;
  152. }
  153. return ordinal ? str + (str.match(/tusen$/) ? 'de' : 'te') : str;
  154. }
  155. function numberToWords(num, ordinal = false) {
  156. const word = Engine.getInstance().subiso === 'alt'
  157. ? numberToWordsGe(num, ordinal)
  158. : numberToWordsRo(num, ordinal);
  159. return word;
  160. }