runtime.js 3.0 KB

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