number-as-skeleton.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { MaskedValueError, PatternError } from '../errors.js';
  2. export function parseNumberAsSkeleton(tokens, onError) {
  3. const res = {};
  4. let hasGroups = false;
  5. let hasExponent = false;
  6. let intOptional = 0;
  7. let intDigits = '';
  8. let decimalPos = -1;
  9. let fracDigits = '';
  10. let fracOptional = 0;
  11. for (let pos = 0; pos < tokens.length; ++pos) {
  12. const token = tokens[pos];
  13. switch (token.char) {
  14. case '#': {
  15. if (decimalPos === -1) {
  16. if (intDigits) {
  17. const msg = 'Pattern has # after integer digits';
  18. onError(new PatternError('#', msg));
  19. }
  20. intOptional += token.width;
  21. }
  22. else {
  23. fracOptional += token.width;
  24. }
  25. break;
  26. }
  27. case '0': {
  28. if (decimalPos === -1) {
  29. intDigits += token.digits;
  30. }
  31. else {
  32. if (fracOptional) {
  33. const msg = 'Pattern has digits after # in fraction';
  34. onError(new PatternError('0', msg));
  35. }
  36. fracDigits += token.digits;
  37. }
  38. break;
  39. }
  40. case '@': {
  41. if (res.precision)
  42. onError(new MaskedValueError('precision', res.precision));
  43. res.precision = {
  44. style: 'precision-fraction',
  45. minSignificant: token.min,
  46. maxSignificant: token.width
  47. };
  48. break;
  49. }
  50. case ',':
  51. hasGroups = true;
  52. break;
  53. case '.':
  54. if (decimalPos === 1) {
  55. const msg = 'Pattern has more than one decimal separator';
  56. onError(new PatternError('.', msg));
  57. }
  58. decimalPos = pos;
  59. break;
  60. case 'E': {
  61. if (hasExponent)
  62. onError(new MaskedValueError('exponent', res.notation));
  63. if (hasGroups) {
  64. const msg = 'Exponential patterns may not contain grouping separators';
  65. onError(new PatternError('E', msg));
  66. }
  67. res.notation = { style: 'scientific' };
  68. if (token.expDigits > 1)
  69. res.notation.expDigits = token.expDigits;
  70. if (token.plus)
  71. res.notation.expSign = 'sign-always';
  72. hasExponent = true;
  73. }
  74. }
  75. }
  76. // imprecise mapping due to paradigm differences
  77. if (hasGroups)
  78. res.group = 'group-auto';
  79. else if (intOptional + intDigits.length > 3)
  80. res.group = 'group-off';
  81. const increment = Number(`${intDigits || '0'}.${fracDigits}`);
  82. if (increment)
  83. res.precision = { style: 'precision-increment', increment };
  84. if (!hasExponent) {
  85. if (intDigits.length > 1)
  86. res.integerWidth = { min: intDigits.length };
  87. if (!res.precision && (fracDigits.length || fracOptional)) {
  88. res.precision = {
  89. style: 'precision-fraction',
  90. minFraction: fracDigits.length,
  91. maxFraction: fracDigits.length + fracOptional
  92. };
  93. }
  94. }
  95. else {
  96. if (!res.precision || increment) {
  97. res.integerWidth = intOptional
  98. ? { min: 1, max: intOptional + intDigits.length }
  99. : { min: Math.max(1, intDigits.length) };
  100. }
  101. if (res.precision) {
  102. if (!increment)
  103. res.integerWidth = { min: 1, max: 1 };
  104. }
  105. else {
  106. const dc = intDigits.length + fracDigits.length;
  107. if (decimalPos === -1) {
  108. if (dc > 0)
  109. res.precision = { style: 'precision-fraction', maxSignificant: dc };
  110. }
  111. else {
  112. res.precision = {
  113. style: 'precision-fraction',
  114. maxSignificant: Math.max(1, dc) + fracOptional
  115. };
  116. if (dc > 1)
  117. res.precision.minSignificant = dc;
  118. }
  119. }
  120. }
  121. return res;
  122. }