numbers_fr.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { Engine } from '../../common/engine.js';
  2. import { Grammar } from '../../rule_engine/grammar.js';
  3. import { NUMBERS as NUMB } from '../messages.js';
  4. function hundredsToWords_(num) {
  5. let n = num % 1000;
  6. let str = '';
  7. str += NUMBERS.ones[Math.floor(n / 100)]
  8. ? NUMBERS.ones[Math.floor(n / 100)] + '-cent'
  9. : '';
  10. n = n % 100;
  11. if (n) {
  12. str += str ? '-' : '';
  13. let ones = NUMBERS.ones[n];
  14. if (ones) {
  15. str += ones;
  16. }
  17. else {
  18. const tens = NUMBERS.tens[Math.floor(n / 10)];
  19. if (tens.match(/-dix$/)) {
  20. ones = NUMBERS.ones[(n % 10) + 10];
  21. str += tens.replace(/-dix$/, '') + '-' + ones;
  22. }
  23. else {
  24. str += tens + (n % 10 ? '-' + NUMBERS.ones[n % 10] : '');
  25. }
  26. }
  27. }
  28. const match = str.match(/s-\w+$/);
  29. return match
  30. ? str.replace(/s-\w+$/, match[0].slice(1))
  31. : str.replace(/-un$/, '-et-un');
  32. }
  33. function numberToWords(num) {
  34. if (num === 0) {
  35. return NUMBERS.zero;
  36. }
  37. if (num >= Math.pow(10, 36)) {
  38. return num.toString();
  39. }
  40. if (NUMBERS.special['tens-' + Engine.getInstance().subiso]) {
  41. NUMBERS.tens = NUMBERS.special['tens-' + Engine.getInstance().subiso];
  42. }
  43. let pos = 0;
  44. let str = '';
  45. while (num > 0) {
  46. const hundreds = num % 1000;
  47. if (hundreds) {
  48. let large = NUMBERS.large[pos];
  49. const huns = hundredsToWords_(hundreds);
  50. if (large && large.match(/^mille /)) {
  51. const rest = large.replace(/^mille /, '');
  52. if (str.match(RegExp(rest))) {
  53. str = huns + (pos ? '-mille-' : '') + str;
  54. }
  55. else if (str.match(RegExp(rest.replace(/s$/, '')))) {
  56. str =
  57. huns +
  58. (pos ? '-mille-' : '') +
  59. str.replace(rest.replace(/s$/, ''), rest);
  60. }
  61. else {
  62. str = huns + (pos ? '-' + large + '-' : '') + str;
  63. }
  64. }
  65. else {
  66. large = hundreds === 1 && large ? large.replace(/s$/, '') : large;
  67. str = huns + (pos ? '-' + large + '-' : '') + str;
  68. }
  69. }
  70. num = Math.floor(num / 1000);
  71. pos++;
  72. }
  73. return str.replace(/-$/, '');
  74. }
  75. const SMALL_ORDINAL = {
  76. 1: 'unième',
  77. 2: 'demi',
  78. 3: 'tiers',
  79. 4: 'quart'
  80. };
  81. function numberToOrdinal(num, plural) {
  82. const ordinal = SMALL_ORDINAL[num] || wordOrdinal(num);
  83. return num === 3 ? ordinal : plural ? ordinal + 's' : ordinal;
  84. }
  85. function wordOrdinal(num) {
  86. if (num === 1) {
  87. return 'première';
  88. }
  89. let ordinal = numberToWords(num);
  90. if (ordinal.match(/^neuf$/)) {
  91. ordinal = ordinal.slice(0, -1) + 'v';
  92. }
  93. else if (ordinal.match(/cinq$/)) {
  94. ordinal = ordinal + 'u';
  95. }
  96. else if (ordinal.match(/trois$/)) {
  97. ordinal = ordinal + '';
  98. }
  99. else if (ordinal.match(/e$/) || ordinal.match(/s$/)) {
  100. ordinal = ordinal.slice(0, -1);
  101. }
  102. ordinal = ordinal + 'ième';
  103. return ordinal;
  104. }
  105. function numericOrdinal(num) {
  106. const gender = Grammar.getInstance().getParameter('gender');
  107. return num === 1
  108. ? num.toString() + (gender === 'm' ? 'er' : 're')
  109. : num.toString() + 'e';
  110. }
  111. export const NUMBERS = NUMB({
  112. wordOrdinal: wordOrdinal,
  113. numericOrdinal: numericOrdinal,
  114. numberToWords: numberToWords,
  115. numberToOrdinal: numberToOrdinal
  116. });