options.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { BadOptionError, MissingOptionError, TooManyOptionsError } from '../errors.js';
  2. const maxOptions = {
  3. 'compact-short': 0,
  4. 'compact-long': 0,
  5. 'notation-simple': 0,
  6. scientific: 2,
  7. engineering: 2,
  8. percent: 0,
  9. permille: 0,
  10. 'base-unit': 0,
  11. currency: 1,
  12. 'measure-unit': 1,
  13. 'per-measure-unit': 1,
  14. 'unit-width-narrow': 0,
  15. 'unit-width-short': 0,
  16. 'unit-width-full-name': 0,
  17. 'unit-width-iso-code': 0,
  18. 'unit-width-hidden': 0,
  19. 'precision-integer': 0,
  20. 'precision-unlimited': 0,
  21. 'precision-currency-standard': 1,
  22. 'precision-currency-cash': 0,
  23. 'precision-increment': 1,
  24. 'rounding-mode-ceiling': 0,
  25. 'rounding-mode-floor': 0,
  26. 'rounding-mode-down': 0,
  27. 'rounding-mode-up': 0,
  28. 'rounding-mode-half-even': 0,
  29. 'rounding-mode-half-down': 0,
  30. 'rounding-mode-half-up': 0,
  31. 'rounding-mode-unnecessary': 0,
  32. 'integer-width': 1,
  33. scale: 1,
  34. 'group-off': 0,
  35. 'group-min2': 0,
  36. 'group-auto': 0,
  37. 'group-on-aligned': 0,
  38. 'group-thousands': 0,
  39. latin: 0,
  40. 'numbering-system': 1,
  41. 'sign-auto': 0,
  42. 'sign-always': 0,
  43. 'sign-never': 0,
  44. 'sign-accounting': 0,
  45. 'sign-accounting-always': 0,
  46. 'sign-except-zero': 0,
  47. 'sign-accounting-except-zero': 0,
  48. 'decimal-auto': 0,
  49. 'decimal-always': 0
  50. };
  51. const minOptions = {
  52. currency: 1,
  53. 'integer-width': 1,
  54. 'measure-unit': 1,
  55. 'numbering-system': 1,
  56. 'per-measure-unit': 1,
  57. 'precision-increment': 1,
  58. scale: 1
  59. };
  60. function hasMaxOption(stem) {
  61. return stem in maxOptions;
  62. }
  63. function hasMinOption(stem) {
  64. return stem in minOptions;
  65. }
  66. /** @internal */
  67. export function validOptions(stem, options, onError) {
  68. if (hasMaxOption(stem)) {
  69. const maxOpt = maxOptions[stem];
  70. if (options.length > maxOpt) {
  71. if (maxOpt === 0) {
  72. for (const opt of options)
  73. onError(new BadOptionError(stem, opt));
  74. }
  75. else {
  76. onError(new TooManyOptionsError(stem, options, maxOpt));
  77. }
  78. return false;
  79. }
  80. else if (hasMinOption(stem) && options.length < minOptions[stem]) {
  81. onError(new MissingOptionError(stem));
  82. return false;
  83. }
  84. }
  85. return true;
  86. }