numbers_ca.js 2.7 KB

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