numbers_ca.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 < 20) {
  9. return exports.NUMBERS.ones[n];
  10. }
  11. const ten = Math.floor(n / 10);
  12. const tens = exports.NUMBERS.tens[ten];
  13. const ones = exports.NUMBERS.ones[n % 10];
  14. return tens && ones ? tens + (ten === 2 ? '-i-' : '-') + ones : tens || ones;
  15. }
  16. function hundredsToWords_(num) {
  17. const n = num % 1000;
  18. const hundred = Math.floor(n / 100);
  19. const hundreds = hundred
  20. ? hundred === 1
  21. ? 'cent'
  22. : exports.NUMBERS.ones[hundred] + '-cents'
  23. : '';
  24. const tens = tensToWords_(n % 100);
  25. return hundreds && tens ? hundreds + exports.NUMBERS.numSep + tens : hundreds || tens;
  26. }
  27. function numberToWords(num) {
  28. if (num === 0) {
  29. return exports.NUMBERS.zero;
  30. }
  31. if (num >= Math.pow(10, 36)) {
  32. return num.toString();
  33. }
  34. let pos = 0;
  35. let str = '';
  36. while (num > 0) {
  37. const hundreds = num % (pos > 1 ? 1000000 : 1000);
  38. if (hundreds) {
  39. let large = exports.NUMBERS.large[pos];
  40. if (!pos) {
  41. str = hundredsToWords_(hundreds);
  42. }
  43. else if (pos === 1) {
  44. str =
  45. (hundreds === 1 ? '' : hundredsToWords_(hundreds) + exports.NUMBERS.numSep) +
  46. large +
  47. (str ? exports.NUMBERS.numSep + str : '');
  48. }
  49. else {
  50. const thousands = numberToWords(hundreds);
  51. large = hundreds === 1 ? large : large.replace(/\u00f3$/, 'ons');
  52. str =
  53. thousands +
  54. exports.NUMBERS.numSep +
  55. large +
  56. (str ? exports.NUMBERS.numSep + str : '');
  57. }
  58. }
  59. num = Math.floor(num / (pos > 1 ? 1000000 : 1000));
  60. pos++;
  61. }
  62. return str;
  63. }
  64. function numberToOrdinal(num, _plural) {
  65. if (num > 1999) {
  66. return numericOrdinal(num);
  67. }
  68. if (num <= 10) {
  69. return exports.NUMBERS.special.onesOrdinals[num - 1];
  70. }
  71. const result = numberToWords(num);
  72. if (result.match(/mil$/)) {
  73. return result.replace(/mil$/, 'mil·lèsima');
  74. }
  75. if (result.match(/u$/)) {
  76. return result.replace(/u$/, 'vena');
  77. }
  78. if (result.match(/a$/)) {
  79. return result.replace(/a$/, 'ena');
  80. }
  81. return result + (result.match(/e$/) ? 'na' : 'ena');
  82. }
  83. function numericOrdinal(num) {
  84. const gender = grammar_js_1.Grammar.getInstance().getParameter('gender');
  85. return num.toString() + (gender === 'f' ? 'a' : 'n');
  86. }
  87. exports.NUMBERS = (0, messages_js_1.NUMBERS)({
  88. numericOrdinal: numericOrdinal,
  89. numberToWords: numberToWords,
  90. numberToOrdinal: numberToOrdinal
  91. });