numbers_it.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 hundredsToWords_(num) {
  7. let n = num % 1000;
  8. let str = '';
  9. str += exports.NUMBERS.ones[Math.floor(n / 100)]
  10. ? exports.NUMBERS.ones[Math.floor(n / 100)] + exports.NUMBERS.numSep + 'cento'
  11. : '';
  12. n = n % 100;
  13. if (n) {
  14. str += str ? exports.NUMBERS.numSep : '';
  15. const ones = exports.NUMBERS.ones[n];
  16. if (ones) {
  17. str += ones;
  18. }
  19. else {
  20. let tens = exports.NUMBERS.tens[Math.floor(n / 10)];
  21. const rest = n % 10;
  22. if (rest === 1 || rest === 8) {
  23. tens = tens.slice(0, -1);
  24. }
  25. str += tens;
  26. str += rest ? exports.NUMBERS.numSep + exports.NUMBERS.ones[n % 10] : '';
  27. }
  28. }
  29. return str;
  30. }
  31. function numberToWords(num) {
  32. if (num === 0) {
  33. return exports.NUMBERS.zero;
  34. }
  35. if (num >= Math.pow(10, 36)) {
  36. return num.toString();
  37. }
  38. if (num === 1 && grammar_js_1.Grammar.getInstance().getParameter('fraction')) {
  39. return 'un';
  40. }
  41. let pos = 0;
  42. let str = '';
  43. while (num > 0) {
  44. const hundreds = num % 1000;
  45. if (hundreds) {
  46. str =
  47. hundredsToWords_(num % 1000) +
  48. (pos ? '-' + exports.NUMBERS.large[pos] + '-' : '') +
  49. str;
  50. }
  51. num = Math.floor(num / 1000);
  52. pos++;
  53. }
  54. return str.replace(/-$/, '');
  55. }
  56. function numberToOrdinal(num, plural) {
  57. if (num === 2) {
  58. return plural ? 'mezzi' : 'mezzo';
  59. }
  60. const ordinal = wordOrdinal(num);
  61. if (!plural) {
  62. return ordinal;
  63. }
  64. const gender = ordinal.match(/o$/) ? 'i' : 'e';
  65. return ordinal.slice(0, -1) + gender;
  66. }
  67. function wordOrdinal(num) {
  68. const gender = grammar_js_1.Grammar.getInstance().getParameter('gender');
  69. const postfix = gender === 'm' ? 'o' : 'a';
  70. let ordinal = exports.NUMBERS.special.onesOrdinals[num];
  71. if (ordinal) {
  72. return ordinal.slice(0, -1) + postfix;
  73. }
  74. ordinal = numberToWords(num);
  75. return ordinal.slice(0, -1) + 'esim' + postfix;
  76. }
  77. function numericOrdinal(num) {
  78. const gender = grammar_js_1.Grammar.getInstance().getParameter('gender');
  79. return num.toString() + (gender === 'm' ? 'o' : 'a');
  80. }
  81. exports.NUMBERS = (0, messages_js_1.NUMBERS)({
  82. wordOrdinal: wordOrdinal,
  83. numericOrdinal: numericOrdinal,
  84. numberToWords: numberToWords,
  85. numberToOrdinal: numberToOrdinal
  86. });