numbers_es.js 3.0 KB

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