numbers_es.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { Grammar } from '../../rule_engine/grammar.js';
  2. import { NUMBERS as NUMB } from '../messages.js';
  3. function tensToWords_(num) {
  4. const n = num % 100;
  5. if (n < 30) {
  6. return NUMBERS.ones[n];
  7. }
  8. const tens = NUMBERS.tens[Math.floor(n / 10)];
  9. const ones = NUMBERS.ones[n % 10];
  10. return tens && ones ? tens + ' y ' + ones : tens || ones;
  11. }
  12. function hundredsToWords_(num) {
  13. const n = num % 1000;
  14. const hundred = Math.floor(n / 100);
  15. const hundreds = NUMBERS.special.hundreds[hundred];
  16. const tens = tensToWords_(n % 100);
  17. if (hundred === 1) {
  18. if (!tens) {
  19. return hundreds;
  20. }
  21. return hundreds + 'to' + ' ' + tens;
  22. }
  23. return hundreds && tens ? hundreds + ' ' + tens : hundreds || tens;
  24. }
  25. function numberToWords(num) {
  26. if (num === 0) {
  27. return NUMBERS.zero;
  28. }
  29. if (num >= Math.pow(10, 36)) {
  30. return num.toString();
  31. }
  32. let pos = 0;
  33. let str = '';
  34. while (num > 0) {
  35. const hundreds = num % 1000;
  36. if (hundreds) {
  37. let large = NUMBERS.large[pos];
  38. const huns = hundredsToWords_(hundreds);
  39. if (!pos) {
  40. str = huns;
  41. }
  42. else if (hundreds === 1) {
  43. large = large.match('/^mil( |$)/') ? large : 'un ' + large;
  44. str = large + (str ? ' ' + str : '');
  45. }
  46. else {
  47. large = large.replace(/\u00f3n$/, 'ones');
  48. str = hundredsToWords_(hundreds) + ' ' + large + (str ? ' ' + str : '');
  49. }
  50. }
  51. num = Math.floor(num / 1000);
  52. pos++;
  53. }
  54. return str;
  55. }
  56. function numberToOrdinal(num, _plural) {
  57. if (num > 1999) {
  58. return num.toString() + 'a';
  59. }
  60. if (num <= 12) {
  61. return NUMBERS.special.onesOrdinals[num - 1];
  62. }
  63. const result = [];
  64. if (num >= 1000) {
  65. num = num - 1000;
  66. result.push('milésima');
  67. }
  68. if (!num) {
  69. return result.join(' ');
  70. }
  71. let pos = 0;
  72. pos = Math.floor(num / 100);
  73. if (pos > 0) {
  74. result.push(NUMBERS.special.hundredsOrdinals[pos - 1]);
  75. num = num % 100;
  76. }
  77. if (num <= 12) {
  78. result.push(NUMBERS.special.onesOrdinals[num - 1]);
  79. }
  80. else {
  81. pos = Math.floor(num / 10);
  82. if (pos > 0) {
  83. result.push(NUMBERS.special.tensOrdinals[pos - 1]);
  84. num = num % 10;
  85. }
  86. if (num > 0) {
  87. result.push(NUMBERS.special.onesOrdinals[num - 1]);
  88. }
  89. }
  90. return result.join(' ');
  91. }
  92. function numericOrdinal(num) {
  93. const gender = Grammar.getInstance().getParameter('gender');
  94. return num.toString() + (gender === 'f' ? 'a' : 'o');
  95. }
  96. export const NUMBERS = NUMB({
  97. numericOrdinal: numericOrdinal,
  98. numberToWords: numberToWords,
  99. numberToOrdinal: numberToOrdinal
  100. });