index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // @flow
  2. var LONG = 'long'
  3. var SHORT = 'short'
  4. var NARROW = 'narrow'
  5. var NUMERIC = 'numeric'
  6. var TWODIGIT = '2-digit'
  7. /**
  8. * formatting information
  9. **/
  10. module.exports = {
  11. number: {
  12. decimal: {
  13. style: 'decimal'
  14. },
  15. integer: {
  16. style: 'decimal',
  17. maximumFractionDigits: 0
  18. },
  19. currency: {
  20. style: 'currency',
  21. currency: 'USD'
  22. },
  23. percent: {
  24. style: 'percent'
  25. },
  26. default: {
  27. style: 'decimal'
  28. }
  29. },
  30. date: {
  31. short: {
  32. month: NUMERIC,
  33. day: NUMERIC,
  34. year: TWODIGIT
  35. },
  36. medium: {
  37. month: SHORT,
  38. day: NUMERIC,
  39. year: NUMERIC
  40. },
  41. long: {
  42. month: LONG,
  43. day: NUMERIC,
  44. year: NUMERIC
  45. },
  46. full: {
  47. month: LONG,
  48. day: NUMERIC,
  49. year: NUMERIC,
  50. weekday: LONG
  51. },
  52. default: {
  53. month: SHORT,
  54. day: NUMERIC,
  55. year: NUMERIC
  56. }
  57. },
  58. time: {
  59. short: {
  60. hour: NUMERIC,
  61. minute: NUMERIC
  62. },
  63. medium: {
  64. hour: NUMERIC,
  65. minute: NUMERIC,
  66. second: NUMERIC
  67. },
  68. long: {
  69. hour: NUMERIC,
  70. minute: NUMERIC,
  71. second: NUMERIC,
  72. timeZoneName: SHORT
  73. },
  74. full: {
  75. hour: NUMERIC,
  76. minute: NUMERIC,
  77. second: NUMERIC,
  78. timeZoneName: SHORT
  79. },
  80. default: {
  81. hour: NUMERIC,
  82. minute: NUMERIC,
  83. second: NUMERIC
  84. }
  85. },
  86. duration: {
  87. default: {
  88. hours: {
  89. minimumIntegerDigits: 1,
  90. maximumFractionDigits: 0
  91. },
  92. minutes: {
  93. minimumIntegerDigits: 2,
  94. maximumFractionDigits: 0
  95. },
  96. seconds: {
  97. minimumIntegerDigits: 2,
  98. maximumFractionDigits: 3
  99. }
  100. }
  101. },
  102. parseNumberPattern: function (pattern/*: ?string */) {
  103. if (!pattern) return
  104. var options = {}
  105. var currency = pattern.match(/\b[A-Z]{3}\b/i)
  106. var syms = pattern.replace(/[^¤]/g, '').length
  107. if (!syms && currency) syms = 1
  108. if (syms) {
  109. options.style = 'currency'
  110. options.currencyDisplay = syms === 1 ? 'symbol' : syms === 2 ? 'code' : 'name'
  111. options.currency = currency ? currency[0].toUpperCase() : 'USD'
  112. } else if (pattern.indexOf('%') >= 0) {
  113. options.style = 'percent'
  114. }
  115. if (!/[@#0]/.test(pattern)) return options.style ? options : undefined
  116. options.useGrouping = pattern.indexOf(',') >= 0
  117. if (/E\+?[@#0]+/i.test(pattern) || pattern.indexOf('@') >= 0) {
  118. var size = pattern.replace(/E\+?[@#0]+|[^@#0]/gi, '')
  119. options.minimumSignificantDigits = Math.min(Math.max(size.replace(/[^@0]/g, '').length, 1), 21)
  120. options.maximumSignificantDigits = Math.min(Math.max(size.length, 1), 21)
  121. } else {
  122. var parts = pattern.replace(/[^#0.]/g, '').split('.')
  123. var integer = parts[0]
  124. var n = integer.length - 1
  125. while (integer[n] === '0') --n
  126. options.minimumIntegerDigits = Math.min(Math.max(integer.length - 1 - n, 1), 21)
  127. var fraction = parts[1] || ''
  128. n = 0
  129. while (fraction[n] === '0') ++n
  130. options.minimumFractionDigits = Math.min(Math.max(n, 0), 20)
  131. while (fraction[n] === '#') ++n
  132. options.maximumFractionDigits = Math.min(Math.max(n, 0), 20)
  133. }
  134. return options
  135. },
  136. parseDatePattern: function (pattern/*: ?string */) {
  137. if (!pattern) return
  138. var options = {}
  139. for (var i = 0; i < pattern.length;) {
  140. var current = pattern[i]
  141. var n = 1
  142. while (pattern[++i] === current) ++n
  143. switch (current) {
  144. case 'G':
  145. options.era = n === 5 ? NARROW : n === 4 ? LONG : SHORT
  146. break
  147. case 'y':
  148. case 'Y':
  149. options.year = n === 2 ? TWODIGIT : NUMERIC
  150. break
  151. case 'M':
  152. case 'L':
  153. n = Math.min(Math.max(n - 1, 0), 4)
  154. options.month = [NUMERIC, TWODIGIT, SHORT, LONG, NARROW][n]
  155. break
  156. case 'E':
  157. case 'e':
  158. case 'c':
  159. options.weekday = n === 5 ? NARROW : n === 4 ? LONG : SHORT
  160. break
  161. case 'd':
  162. case 'D':
  163. options.day = n === 2 ? TWODIGIT : NUMERIC
  164. break
  165. case 'h':
  166. case 'K':
  167. options.hour12 = true
  168. options.hour = n === 2 ? TWODIGIT : NUMERIC
  169. break
  170. case 'H':
  171. case 'k':
  172. options.hour12 = false
  173. options.hour = n === 2 ? TWODIGIT : NUMERIC
  174. break
  175. case 'm':
  176. options.minute = n === 2 ? TWODIGIT : NUMERIC
  177. break
  178. case 's':
  179. case 'S':
  180. options.second = n === 2 ? TWODIGIT : NUMERIC
  181. break
  182. case 'z':
  183. case 'Z':
  184. case 'v':
  185. case 'V':
  186. options.timeZoneName = n === 1 ? SHORT : LONG
  187. break
  188. }
  189. }
  190. return Object.keys(options).length ? options : undefined
  191. }
  192. }