numbers_fr.js 3.8 KB

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