index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var buildFormattingTokensRegExp = require('../../_lib/build_formatting_tokens_reg_exp/index.js')
  2. function buildFormatLocale () {
  3. var months3char = ['janv.', 'févr.', 'mars', 'avr.', 'mai', 'juin', 'juill.', 'août', 'sept.', 'oct.', 'nov.', 'déc.']
  4. var monthsFull = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre']
  5. var weekdays2char = ['di', 'lu', 'ma', 'me', 'je', 've', 'sa']
  6. var weekdays3char = ['dim.', 'lun.', 'mar.', 'mer.', 'jeu.', 'ven.', 'sam.']
  7. var weekdaysFull = ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi']
  8. var meridiemUppercase = ['AM', 'PM']
  9. var meridiemLowercase = ['am', 'pm']
  10. var meridiemFull = ['du matin', 'de l’après-midi', 'du soir']
  11. var formatters = {
  12. // Month: Jan, Feb, …, Dec
  13. 'MMM': function (date) {
  14. return months3char[date.getMonth()]
  15. },
  16. // Month: January, February, …, December
  17. 'MMMM': function (date) {
  18. return monthsFull[date.getMonth()]
  19. },
  20. // Day of week: Su, Mo, …, Sa
  21. 'dd': function (date) {
  22. return weekdays2char[date.getDay()]
  23. },
  24. // Day of week: Sun, Mon, …, Sat
  25. 'ddd': function (date) {
  26. return weekdays3char[date.getDay()]
  27. },
  28. // Day of week: Sunday, Monday, …, Saturday
  29. 'dddd': function (date) {
  30. return weekdaysFull[date.getDay()]
  31. },
  32. // AM, PM
  33. 'A': function (date) {
  34. return (date.getHours() / 12) >= 1 ? meridiemUppercase[1] : meridiemUppercase[0]
  35. },
  36. // am, pm
  37. 'a': function (date) {
  38. return (date.getHours() / 12) >= 1 ? meridiemLowercase[1] : meridiemLowercase[0]
  39. },
  40. // a.m., p.m.
  41. 'aa': function (date) {
  42. var hours = date.getHours()
  43. if (hours <= 12) {
  44. return meridiemFull[0]
  45. }
  46. if (hours <= 16) {
  47. return meridiemFull[1]
  48. }
  49. return meridiemFull[2]
  50. },
  51. // ISO week, ordinal version: 1st, 2nd, …, 53rd
  52. // NOTE: Week has feminine grammatical gender in French: semaine
  53. 'Wo': function (date, formatters) {
  54. return feminineOrdinal(formatters.W(date))
  55. }
  56. }
  57. // Generate ordinal version of formatters: M → Mo, D → Do, etc.
  58. // NOTE: For words with masculine grammatical gender in French: mois, jour, trimestre
  59. var formatterTokens = ['M', 'D', 'DDD', 'd', 'Q']
  60. formatterTokens.forEach(function (formatterToken) {
  61. formatters[formatterToken + 'o'] = function (date, formatters) {
  62. return masculineOrdinal(formatters[formatterToken](date))
  63. }
  64. })
  65. // Special case for day of month ordinals in long date format context:
  66. // 1er mars, 2 mars, 3 mars, …
  67. // See https://github.com/date-fns/date-fns/issues/437
  68. //
  69. // NOTE: The below implementation works because parsing of tokens inside a
  70. // format string is done by a greedy regular expression, i.e. longer tokens
  71. // have priority. E.g. formatter for "Do MMMM" has priority over individual
  72. // formatters for "Do" and "MMMM".
  73. var monthsTokens = ['MMM', 'MMMM']
  74. monthsTokens.forEach(function (monthToken) {
  75. formatters['Do ' + monthToken] = function (date, commonFormatters) {
  76. var dayOfMonthToken = date.getDate() === 1
  77. ? 'Do'
  78. : 'D'
  79. var dayOfMonthFormatter = formatters[dayOfMonthToken] || commonFormatters[dayOfMonthToken]
  80. return dayOfMonthFormatter(date, commonFormatters) + ' ' + formatters[monthToken](date)
  81. }
  82. })
  83. return {
  84. formatters: formatters,
  85. formattingTokensRegExp: buildFormattingTokensRegExp(formatters)
  86. }
  87. }
  88. function masculineOrdinal (number) {
  89. if (number === 1) {
  90. return '1er'
  91. }
  92. return number + 'e'
  93. }
  94. function feminineOrdinal (number) {
  95. if (number === 1) {
  96. return '1re'
  97. }
  98. return number + 'e'
  99. }
  100. module.exports = buildFormatLocale