runtime.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * A set of utility functions that are called by the compiled Javascript
  3. * functions, these are included locally in the output of {@link MessageFormat.compile compile()}.
  4. */
  5. /** @private */
  6. export function _nf(lc) {
  7. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  8. // @ts-ignore
  9. return _nf[lc] || (_nf[lc] = new Intl.NumberFormat(lc));
  10. }
  11. /**
  12. * Utility function for `#` in plural rules
  13. *
  14. * @param lc The current locale
  15. * @param value The value to operate on
  16. * @param offset An offset, set by the surrounding context
  17. * @returns The result of applying the offset to the input value
  18. */
  19. export function number(lc, value, offset) {
  20. return _nf(lc).format(value - offset);
  21. }
  22. /**
  23. * Strict utility function for `#` in plural rules
  24. *
  25. * Will throw an Error if `value` or `offset` are non-numeric.
  26. *
  27. * @param lc The current locale
  28. * @param value The value to operate on
  29. * @param offset An offset, set by the surrounding context
  30. * @param name The name of the argument, used for error reporting
  31. * @returns The result of applying the offset to the input value
  32. */
  33. export function strictNumber(lc, value, offset, name) {
  34. var n = value - offset;
  35. if (isNaN(n))
  36. throw new Error('`' + name + '` or its offset is not a number');
  37. return _nf(lc).format(n);
  38. }
  39. /**
  40. * Utility function for `{N, plural|selectordinal, ...}`
  41. *
  42. * @param value The key to use to find a pluralization rule
  43. * @param offset An offset to apply to `value`
  44. * @param lcfunc A locale function from `pluralFuncs`
  45. * @param data The object from which results are looked up
  46. * @param isOrdinal If true, use ordinal rather than cardinal rules
  47. * @returns The result of the pluralization
  48. */
  49. export function plural(value, offset, lcfunc, data, isOrdinal) {
  50. if ({}.hasOwnProperty.call(data, value))
  51. return data[value];
  52. if (offset)
  53. value -= offset;
  54. var key = lcfunc(value, isOrdinal);
  55. return key in data ? data[key] : data.other;
  56. }
  57. /**
  58. * Utility function for `{N, select, ...}`
  59. *
  60. * @param value The key to use to find a selection
  61. * @param data The object from which results are looked up
  62. * @returns The result of the select statement
  63. */
  64. export function select(value, data) {
  65. return {}.hasOwnProperty.call(data, value) ? data[value] : data.other;
  66. }
  67. /**
  68. * Checks that all required arguments are set to defined values
  69. *
  70. * Throws on failure; otherwise returns undefined
  71. *
  72. * @param keys The required keys
  73. * @param data The data object being checked
  74. */
  75. export function reqArgs(keys, data) {
  76. for (var i = 0; i < keys.length; ++i)
  77. if (!data || data[keys[i]] === undefined)
  78. throw new Error("Message requires argument '".concat(keys[i], "'"));
  79. }