globalize.d.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Type definitions for Globalize
  2. // Project: https://github.com/jquery/globalize
  3. // Definitions by: Grégoire Castre <https://github.com/gcastre/>, Aram Taieb <https://github.com/afromogli/>
  4. // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
  5. // Note: You'll need the cldr.js definition file to use the globalize (https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/cldr.js)
  6. /// <reference path="cldr.js.d.ts" />
  7. interface DateFormatterOptions {
  8. /**
  9. * String value indicating a skeleton (see description above), eg. { skeleton: "GyMMMd" }.
  10. * Skeleton provides a more flexible formatting mechanism than the predefined list full, long, medium, or short represented by date, time, or datetime.
  11. * Instead, they are an open-ended list of patterns containing only date field information, and in a canonical order.
  12. */
  13. skeleton?: string;
  14. /**
  15. * One of the following String values: full, long, medium, or short, eg. { date: "full" }.
  16. */
  17. date?: "full" | "long" | "medium" | "short";
  18. /**
  19. * One of the following String values: full, long, medium, or short, eg. { time: "full" }.
  20. */
  21. time?: "full" | "long" | "medium" | "short";
  22. /**
  23. * One of the following String values: full, long, medium, or short, eg. { datetime: "full" }
  24. */
  25. datetime?: "full" | "long" | "medium" | "short";
  26. /**
  27. * String value indicating a machine raw pattern (anything in the "Sym." column) eg. { raw: "dd/mm" }.
  28. * Note this is NOT recommended for i18n in general. Use skeleton instead.
  29. */
  30. raw?: string;
  31. }
  32. interface CommonNumberFormatterOptions {
  33. /**
  34. * Non-negative integer Number value indicating the minimum integer digits to be used. Numbers will be padded with leading zeroes if necessary.
  35. */
  36. minimumIntegerDigits?: number;
  37. /**
  38. * Non-negative integer Number values indicating the minimum and maximum fraction digits to be used.
  39. * Numbers will be rounded or padded with trailing zeroes if necessary.
  40. * Either one or both of these properties must be present.
  41. * If they are, they will override minimum and maximum fraction digits derived from the CLDR patterns.
  42. */
  43. minimumFractionDigits?: number;
  44. /**
  45. * Non-negative integer Number values indicating the minimum and maximum fraction digits to be used.
  46. * Numbers will be rounded or padded with trailing zeroes if necessary.
  47. * Either one or both of these properties must be present.
  48. * If they are, they will override minimum and maximum fraction digits derived from the CLDR patterns.
  49. */
  50. maximumFractionDigits?: number;
  51. /**
  52. * Positive integer Number values indicating the minimum and maximum fraction digits to be shown.
  53. * Either none or both of these properties are present
  54. * If they are, they override minimum and maximum integer and fraction digits.
  55. * The formatter uses however many integer and fraction digits are required to display the specified number of significant digits.
  56. */
  57. minimumSignificantDigits?: number;
  58. /**
  59. * Positive integer Number values indicating the minimum and maximum fraction digits to be shown.
  60. * Either none or both of these properties are present.
  61. * If they are, they override minimum and maximum integer and fraction digits.
  62. * The formatter uses however many integer and fraction digits are required to display the specified number of significant digits.
  63. */
  64. maximumSignificantDigits?: number;
  65. /**
  66. * String with rounding method ceil, floor, round (default), or truncate.
  67. */
  68. round?: "ceil" | "floor" | "round" | "truncate";
  69. /**
  70. * Boolean (default is true) value indicating whether a grouping separator should be used.
  71. */
  72. useGrouping ?: boolean;
  73. }
  74. interface NumberFormatterOptions extends CommonNumberFormatterOptions {
  75. /**
  76. * decimal (default), or percent
  77. */
  78. style?: "decimal" | "percent";
  79. }
  80. interface CurrencyFormatterOptions extends CommonNumberFormatterOptions {
  81. /**
  82. * symbol (default), accounting, code or name.
  83. */
  84. style?: "symbol" | "accounting" | "code" | "name";
  85. }
  86. interface NumberParserOptions {
  87. /**
  88. * decimal (default), or percent.
  89. */
  90. style?: "decimal" | "percent";
  91. }
  92. interface PluralGeneratorOptions {
  93. /**
  94. * cardinal (default), or ordinal.
  95. */
  96. type?: "cardinal" | "ordinal";
  97. }
  98. interface RelativeTimeFormatterOptions {
  99. /**
  100. * eg. "short" or "narrow". Or falsy for default long form
  101. */
  102. form?: "short" | "narrow";
  103. }
  104. interface UnitFormatterOptions {
  105. /**
  106. * form: [String] eg. "long", "short" or "narrow".
  107. */
  108. form?: "long" | "short" | "narrow";
  109. /**
  110. * numberFormatter: [Function] a number formatter function. Defaults to Globalize .numberFormatter() for the current locale using the default options.
  111. */
  112. numberFormatter ?: NumberFormatterOptions;
  113. }
  114. interface GlobalizeStatic {
  115. cldr: cldr.CldrStatic;
  116. /**
  117. * Globalize.load( json, ... )
  118. * @param {Object} [JSON]
  119. * Load resolved or unresolved cldr data.
  120. * Somewhat equivalent to previous Globalize.addCultureInfo(...).
  121. */
  122. load(json: Object): void;
  123. /**
  124. * Globalize.locale()
  125. * Return the default Cldr instance.
  126. */
  127. locale(): cldr.CldrStatic;
  128. /**
  129. * Globalize.locale( [locale] )
  130. * @param {string} locale
  131. * Set default Cldr instance
  132. * Return the default Cldr instance.
  133. */
  134. locale(locale: string): cldr.CldrStatic;
  135. /**
  136. * Globalize.locale( cldr )
  137. * @param {Cldr} cldr [Cldr instance]
  138. * Set default Cldr instance
  139. * Return the default Cldr instance.
  140. */
  141. locale(cldr: cldr.CldrStatic): cldr.CldrStatic;
  142. /**
  143. * .dateFormatter( options )
  144. * @param {DateFormatterOptions} options see date/expand_pattern for more info.
  145. * @returns {Function} Return a function that formats a date according to the given `options` and the default/instance locale.
  146. */
  147. dateFormatter(options?: DateFormatterOptions): (value: Date) => string;
  148. //Return a function that parses a string representing a date into a JavaScript Date object according to the given options. The default parsing assumes numeric year, month, and day (i.e., { skeleton: "yMd" }).
  149. dateParser(options?: DateFormatterOptions): (value: string) => Date;
  150. //Alias for .dateFormatter( [options] )( value ).
  151. formatDate(value: Date, options?: DateFormatterOptions): string;
  152. /**
  153. * Alias for .dateParser( [options] )( value ).
  154. * @param {string} value The object whose module id you wish to determine.
  155. * @param {DateFormatterOptions} options The object whose module id you wish to determine.
  156. * @returns {Date} Return the value as a Date.
  157. */
  158. parseDate(value: string, options?: DateFormatterOptions): Date;
  159. /**
  160. * Load messages data.
  161. * @param {Object} json JSON object of messages data. Keys can use any character, except /, { and }. Values (i.e., the message content itself) can contain any character.
  162. * @returns {void}
  163. */
  164. loadMessages(json: Object): void;
  165. /**
  166. * Return a function that formats a message (using ICU message format pattern) given its path and a set of variables into a user-readable string. It supports pluralization and gender inflections.
  167. * @param path String or Array containing the path of the message content, eg. "greetings/bye", or [ "greetings", "bye" ].
  168. * @returns {Function} Return A function that formats a message (using ICU message format pattern) given its path and a set of variables into a user-readable string. It supports pluralization and gender inflections.
  169. */
  170. messageFormatter(path: string | string[]): (variables?: string | string[] | Object) => string;
  171. /**
  172. * Formats a message (using ICU message format pattern) given its path and a set of variables into a user-readable string
  173. * @param path String or Array containing the path of the message content, eg. "greetings/bye", or [ "greetings", "bye" ].
  174. * @param variables Variables can be Objects, where each property can be referenced by name inside a message; or Arrays, where each entry of the Array can be used inside a message, using numeric indices. When passing one or more arguments of other types, they're converted to an Array and used as such.
  175. * @returns {string} Return a user-readable string.
  176. */
  177. formatMessage(path: string | string[], variables?: string | string[] | Object): string
  178. /**
  179. * Return a function that formats a number according to the given options or locale's defaults.
  180. * @param {NumberFormatterOptions} options A JSON object including none or any of the following options.
  181. * style Optional String decimal (default), or percent.
  182. * minimumIntegerDigits Optional Non-negative integer Number value indicating the minimum integer digits to be used. Numbers will be padded with leading zeroes if necessary.
  183. * minimumFractionDigits and maximumFractionDigits Optional Non-negative integer Number values indicating the minimum and maximum fraction digits to be used. Numbers will be rounded or padded with trailing zeroes if necessary. Either one or both of these properties must be present. If they are, they will override minimum and maximum fraction digits derived from the CLDR patterns.
  184. * minimumSignificantDigits and maximumSignificantDigits Optional Positive integer Number values indicating the minimum and maximum fraction digits to be shown. Either none or both of these properties are present. If they are, they override minimum and maximum integer and fraction digits. The formatter uses however many integer and fraction digits are required to display the specified number of significant digits.
  185. * round Optional String with rounding method ceil, floor, round (default), or truncate.
  186. * useGrouping Optional Boolean (default is true) value indicating whether a grouping separator should be used.
  187. * @returns {Function} Return a function that formats a number according to the given options.
  188. */
  189. numberFormatter(options?: NumberFormatterOptions): (value: number) => string;
  190. /**
  191. * Return a function that parses a string representing a number according to the given options or locale's defaults.
  192. * @param {NumberParserOptions} options A JSON object including none or any of the following options.
  193. * style Optional String decimal (default), or percent.
  194. * @returns {Function} Return a function that parses a String representing a number according to the given options. If value is invalid, NaN is returned.
  195. */
  196. numberParser(options?: NumberParserOptions): (value: string) => number
  197. /**
  198. * Return a number formatted according to the given options or locale's defaults.
  199. * @param {number} value The number to format
  200. * @param {NumberFormatterOptions} options A JSON object including none or any of the following options.
  201. * style Optional String decimal (default), or percent.
  202. * minimumIntegerDigits Optional Non-negative integer Number value indicating the minimum integer digits to be used. Numbers will be padded with leading zeroes if necessary.
  203. * minimumFractionDigits and maximumFractionDigits Optional Non-negative integer Number values indicating the minimum and maximum fraction digits to be used. Numbers will be rounded or padded with trailing zeroes if necessary. Either one or both of these properties must be present. If they are, they will override minimum and maximum fraction digits derived from the CLDR patterns.
  204. * minimumSignificantDigits and maximumSignificantDigits Optional Positive integer Number values indicating the minimum and maximum fraction digits to be shown. Either none or both of these properties are present. If they are, they override minimum and maximum integer and fraction digits. The formatter uses however many integer and fraction digits are required to display the specified number of significant digits.
  205. * round Optional String with rounding method ceil, floor, round (default), or truncate.
  206. * useGrouping Optional Boolean (default is true) value indicating whether a grouping separator should be used.
  207. * @returns {string} Return the number formatted
  208. */
  209. formatNumber(value: number, options?: NumberFormatterOptions): string;
  210. /**
  211. * A function that parses a string representing a number according to the given options or locale's defaults.
  212. * @param {string} value The number as string to parse
  213. * @param {NumberParserOptions} options A JSON object including none or any of the following options.
  214. * style Optional String decimal (default), or percent.
  215. * @returns {number} Return a number according to the given options. If value is invalid, NaN is returned.
  216. */
  217. parseNumber(value: string, options?: NumberParserOptions): number;
  218. /**
  219. * Return a function that formats a currency according to the given options or locale's defaults.
  220. * The returned function is invoked with one argument: the Number value to be formatted.
  221. * @param {string} currency 3-letter currency code as defined by ISO 4217, eg. USD.
  222. * @param {CurrencyFormatterOptions} options A JSON object including none or any of the following options.
  223. * @returns {Function} Return a function that formats a currency
  224. */
  225. currencyFormatter(currency: string, options?: CurrencyFormatterOptions): (value: number) => string;
  226. /**
  227. * Return a currency formatted according to the given options or locale's defaults.
  228. * @param {number} value The value to format.
  229. * @param {string} currency 3-letter currency code as defined by ISO 4217, eg. USD.
  230. * @param {CurrencyFormatterOptions} options A JSON object including none or any of the following options.
  231. * @returns {string} Return a string formatted in the currency according to the value and the options
  232. */
  233. formatCurrency(value: number, currency: string, options?: CurrencyFormatterOptions): string;
  234. /**
  235. * Return a function that returns the value's corresponding plural group: zero, one, two, few, many, or other.
  236. * The returned function is invoked with one argument: the Number value for which to return the plural group.
  237. * @param {PluralGeneratorOptions} options A JSON object including none or any of the following options.
  238. * type Optional String cardinal (default), or ordinal.
  239. * @returns {Function} Return a function that returns the value's corresponding plural group: zero, one, two, few, many, or other.
  240. */
  241. pluralGenerator(options?: PluralGeneratorOptions): (value: number) => string;
  242. /**
  243. * Returns the value's corresponding plural group: zero, one, two, few, many, or other.
  244. * @param {number} value A Number for which to return the plural group.
  245. * @param {PluralGeneratorOptions} options A JSON object including none or any of the following options.
  246. * type Optional String cardinal (default), or ordinal.
  247. * @returns {string} Returns the value's corresponding plural group: zero, one, two, few, many, or other.
  248. */
  249. plural(value: number, options?: PluralGeneratorOptions): string;
  250. /**
  251. * Returns a function that formats a relative time according to the given unit, options, and the default/instance locale.
  252. * The returned function is invoked with one argument: the number value to be formatted.
  253. * @param unit String value indicating the unit to be formatted. eg. "day", "week", "month", etc.
  254. * @param options form: [String] eg. "short" or "narrow". Or falsy for default long form.
  255. * @returns {Function} Returns a function that formats a relative time according to the given unit.
  256. */
  257. relativeTimeFormatter(unit: string, options?: RelativeTimeFormatterOptions): (value: number) => string;
  258. /**
  259. * Return a relative time according to the given unit
  260. * @param {number} value The number to be formatted.
  261. * @param {string} unit String value indicating the unit to be formatted. eg. "day", "week", "month", etc.
  262. * @param options form: [String] eg. "short" or "narrow". Or falsy for default long form.
  263. * @returns {string} Return a relative time according to the given unit.
  264. */
  265. formatRelativeTime(value: number, unit: string, options?: RelativeTimeFormatterOptions): string;
  266. /**
  267. * Returns a function that formats a unit according to the given unit, options, and the default/instance locale.
  268. * The returned function is invoked with one argument: the number value to be formatted.
  269. * @param unit String value indicating the unit to be formatted. eg. "day", "week", "month", etc. Could also be a compound unit, eg. "mile-per-hour" or "mile/hour"
  270. * @param options form: [String] eg. "long", "short" or "narrow".
  271. * @returns {Function} Returns a function that formats a unit according to the given unit, options, and the default/instance locale.
  272. */
  273. unitFormatter(unit: string, options?: UnitFormatterOptions): (value: number) => string;
  274. /**
  275. * Alias for .unitFormatter( unit, options )( value ).
  276. * @param {number} value The number to be formatted.
  277. * @param {string} unit String value indicating the unit to be formatted. eg. "day", "week", "month", etc. Could also be a compound unit, eg. "mile-per-hour" or "mile/hour"
  278. * @param {UnitFormatterOptions} options form: [String] eg. "long", "short" or "narrow".
  279. * @returns {string} Returns the unit formatted.
  280. */
  281. formatUnit(value: number, unit: string, options?: UnitFormatterOptions): string
  282. /**
  283. * Create a Globalize instance.
  284. * @param {string} Locale string of the instance.
  285. * @returns {Globalize} A Globalize instance
  286. */
  287. new (locale: string): GlobalizeStatic;
  288. /**
  289. * Create a Globalize instance.
  290. * @param cldr Cldr instance of the instance.
  291. * @returns {Globalize} A Globalize instance
  292. */
  293. new (cldr: cldr.CldrStatic): GlobalizeStatic;
  294. }
  295. declare var Globalize: GlobalizeStatic;