get-formatter.d.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { NumberFormatError } from './errors.js';
  2. import { Skeleton } from './types/skeleton.js';
  3. /**
  4. * Returns a number formatter function for the given locales and number skeleton
  5. *
  6. * @remarks
  7. * Uses `Intl.NumberFormat` (ES2020) internally.
  8. *
  9. * @public
  10. * @param locales - One or more valid BCP 47 language tags, e.g. `fr` or `en-CA`
  11. * @param skeleton - An ICU NumberFormatter pattern or `::`-prefixed skeleton
  12. * string, or a parsed `Skeleton` structure
  13. * @param currency - If `skeleton` is a pattern string that includes ¤ tokens,
  14. * their skeleton representation requires a three-letter currency code.
  15. * @param onError - If defined, will be called separately for each encountered
  16. * parsing error and unsupported feature.
  17. * @example
  18. * ```js
  19. * import { getNumberFormatter } from '@messageformat/number-skeleton'
  20. *
  21. * let src = ':: currency/CAD unit-width-narrow'
  22. * let fmt = getNumberFormatter('en-CA', src, console.error)
  23. * fmt(42) // '$42.00'
  24. *
  25. * src = '::percent scale/100'
  26. * fmt = getNumberFormatter('en', src, console.error)
  27. * fmt(0.3) // '30%'
  28. * ```
  29. */
  30. export declare function getNumberFormatter(locales: string | string[], skeleton: string | Skeleton, currency?: string | null, onError?: (err: NumberFormatError) => void): (value: number) => string;
  31. /**
  32. * Returns a string of JavaScript source that evaluates to a number formatter
  33. * function with the same `(value: number) => string` signature as the function
  34. * returned by {@link getNumberFormatter}.
  35. *
  36. * @remarks
  37. * The returned function will memoize an `Intl.NumberFormat` instance.
  38. *
  39. * @public
  40. * @param locales - One or more valid BCP 47 language tags, e.g. `fr` or `en-CA`
  41. * @param skeleton - An ICU NumberFormatter pattern or `::`-prefixed skeleton
  42. * string, or a parsed `Skeleton` structure
  43. * @param currency - If `skeleton` is a pattern string that includes ¤ tokens,
  44. * their skeleton representation requires a three-letter currency code.
  45. * @param onError - If defined, will be called separately for each encountered
  46. * parsing error and unsupported feature.
  47. * @example
  48. * ```js
  49. * import { getNumberFormatterSource } from '@messageformat/number-skeleton'
  50. *
  51. * getNumberFormatterSource('en', '::percent', console.error)
  52. * // '(function() {\n' +
  53. * // ' var opt = {"style":"percent"};\n' +
  54. * // ' var nf = new Intl.NumberFormat(["en"], opt);\n' +
  55. * // ' var mod = function(n) { return n * 0.01; };\n' +
  56. * // ' return function(value) { return nf.format(mod(value)); }\n' +
  57. * // '})()'
  58. *
  59. * const src = getNumberFormatterSource('en-CA', ':: currency/CAD unit-width-narrow', console.error)
  60. * // '(function() {\n' +
  61. * // ' var opt = {"style":"currency","currency":"CAD","currencyDisplay":"narrowSymbol","unitDisplay":"narrow"};\n' +
  62. * // ' var nf = new Intl.NumberFormat(["en-CA"], opt);\n'
  63. * // ' return function(value) { return nf.format(value); }\n' +
  64. * // '})()'
  65. * const fmt = new Function(`return ${src}`)()
  66. * fmt(42) // '$42.00'
  67. * ```
  68. */
  69. export declare function getNumberFormatterSource(locales: string | string[], skeleton: string | Skeleton, currency?: string | null, onError?: (err: NumberFormatError) => void): string;