clarity-pattern-parser 11.3.8 → 11.3.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1251,12 +1251,10 @@ class FiniteRepeat {
1251
1251
  }
1252
1252
  }
1253
1253
  }
1254
- if (this._trimDivider && this._hasDivider) {
1255
- const isDividerLastMatch = this.children.length > 1 && nodes.length > 1 && nodes[nodes.length - 1].name === this.children[1].name;
1256
- if (isDividerLastMatch) {
1257
- const node = nodes.pop();
1258
- cursor.moveTo(node.firstIndex);
1259
- }
1254
+ const endedOnDivider = this._hasDivider && nodes.length % modulo === 0;
1255
+ if (this._trimDivider && endedOnDivider) {
1256
+ const node = nodes.pop();
1257
+ cursor.moveTo(node.firstIndex);
1260
1258
  }
1261
1259
  if (matchCount < this._min) {
1262
1260
  const lastIndex = cursor.index;
@@ -1392,6 +1390,7 @@ class InfiniteRepeat {
1392
1390
  this._firstIndex = 0;
1393
1391
  this._nodes = [];
1394
1392
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1393
+ this._patterns = [];
1395
1394
  }
1396
1395
  _assignChildrenToParent(children) {
1397
1396
  for (const child of children) {
@@ -1407,6 +1406,7 @@ class InfiniteRepeat {
1407
1406
  parse(cursor) {
1408
1407
  this._firstIndex = cursor.index;
1409
1408
  this._nodes = [];
1409
+ this._patterns = [];
1410
1410
  const passed = this._tryToParse(cursor);
1411
1411
  if (passed) {
1412
1412
  cursor.resolveError();
@@ -1458,6 +1458,7 @@ class InfiniteRepeat {
1458
1458
  }
1459
1459
  if (repeatNode != null) {
1460
1460
  this._nodes.push(repeatNode);
1461
+ this._patterns.push(this._pattern);
1461
1462
  if (!cursor.hasNext()) {
1462
1463
  passed = true;
1463
1464
  break;
@@ -1482,6 +1483,7 @@ class InfiniteRepeat {
1482
1483
  }
1483
1484
  else {
1484
1485
  this._nodes.push(dividerNode);
1486
+ this._patterns.push(this._divider);
1485
1487
  if (!cursor.hasNext()) {
1486
1488
  passed = true;
1487
1489
  break;
@@ -1504,11 +1506,11 @@ class InfiniteRepeat {
1504
1506
  return passed;
1505
1507
  }
1506
1508
  _createNode(cursor) {
1507
- var _a;
1508
1509
  const hasDivider = this._divider != null;
1510
+ const lastPattern = this._patterns[this._patterns.length - 1];
1509
1511
  if (hasDivider &&
1510
1512
  this._trimDivider &&
1511
- this._nodes[this._nodes.length - 1].name === ((_a = this._divider) === null || _a === void 0 ? void 0 : _a.name)) {
1513
+ lastPattern === this._divider) {
1512
1514
  const dividerNode = this._nodes.pop();
1513
1515
  cursor.moveTo(dividerNode.firstIndex);
1514
1516
  }
@@ -4056,7 +4058,7 @@ class AutoComplete {
4056
4058
  const furthestError = cursor.furthestError;
4057
4059
  const furthestMatch = cursor.allMatchedNodes[cursor.allMatchedNodes.length - 1];
4058
4060
  if (furthestError && furthestMatch) {
4059
- if (furthestError.lastIndex > furthestMatch.endIndex) {
4061
+ if (furthestMatch.endIndex > furthestError.lastIndex) {
4060
4062
  return furthestMatch.endIndex;
4061
4063
  }
4062
4064
  else {
@@ -4111,7 +4113,7 @@ class AutoComplete {
4111
4113
  }
4112
4114
  _createSuggestionsFromRoot() {
4113
4115
  const suggestions = [];
4114
- const tokens = this._pattern.getTokens();
4116
+ const tokens = [...this._pattern.getTokens(), ...this._getTokensForPattern(this._pattern)];
4115
4117
  for (const token of tokens) {
4116
4118
  if (suggestions.findIndex(s => s.text === token) === -1) {
4117
4119
  suggestions.push(this._createSuggestion("", token));
@@ -4123,15 +4125,29 @@ class AutoComplete {
4123
4125
  if (match.pattern == null) {
4124
4126
  return this._createSuggestions(-1, this._getTokensForPattern(this._pattern));
4125
4127
  }
4126
- const leafPattern = match.pattern;
4127
- const parent = match.pattern.parent;
4128
- if (parent !== null && match.node != null) {
4129
- const patterns = leafPattern.getNextPatterns();
4130
- const tokens = patterns.reduce((acc, pattern) => {
4131
- acc.push(...this._getTokensForPattern(pattern));
4128
+ if (match.node != null) {
4129
+ const textStartingMatch = this._text.slice(match.node.startIndex, match.node.endIndex);
4130
+ const currentPatternsTokens = this._getTokensForPattern(match.pattern);
4131
+ /**
4132
+ * Compares tokens to current text and extracts remainder tokens
4133
+ * - IE. **currentText:** *abc*, **baseToken:** *abcdef*, **trailingToken:** *def*
4134
+ */
4135
+ const trailingTokens = currentPatternsTokens.reduce((acc, token) => {
4136
+ if (token.startsWith(textStartingMatch)) {
4137
+ const sliced = token.slice(textStartingMatch.length);
4138
+ if (sliced !== '') {
4139
+ acc.push(sliced);
4140
+ }
4141
+ }
4132
4142
  return acc;
4133
4143
  }, []);
4134
- return this._createSuggestions(match.node.lastIndex, tokens);
4144
+ const leafPatterns = match.pattern.getNextPatterns();
4145
+ const leafTokens = leafPatterns.reduce((acc, leafPattern) => {
4146
+ acc.push(...this._getTokensForPattern(leafPattern));
4147
+ return acc;
4148
+ }, []);
4149
+ const allTokens = [...trailingTokens, ...leafTokens];
4150
+ return this._createSuggestions(match.node.lastIndex, allTokens);
4135
4151
  }
4136
4152
  else {
4137
4153
  return [];
@@ -4141,44 +4157,54 @@ class AutoComplete {
4141
4157
  const augmentedTokens = this._getAugmentedTokens(pattern);
4142
4158
  if (this._options.greedyPatternNames != null && this._options.greedyPatternNames.includes(pattern.name)) {
4143
4159
  const nextPatterns = pattern.getNextPatterns();
4144
- const tokens = [];
4145
4160
  const nextPatternTokens = nextPatterns.reduce((acc, pattern) => {
4146
4161
  acc.push(...this._getTokensForPattern(pattern));
4147
4162
  return acc;
4148
4163
  }, []);
4149
- for (let token of augmentedTokens) {
4150
- for (let nextPatternToken of nextPatternTokens) {
4151
- tokens.push(token + nextPatternToken);
4164
+ // using set to prevent duplicates
4165
+ const tokens = new Set();
4166
+ for (const token of augmentedTokens) {
4167
+ for (const nextPatternToken of nextPatternTokens) {
4168
+ tokens.add(token + nextPatternToken);
4152
4169
  }
4153
4170
  }
4154
- return tokens;
4171
+ return [...tokens];
4155
4172
  }
4156
4173
  else {
4157
4174
  return augmentedTokens;
4158
4175
  }
4159
4176
  }
4160
4177
  _getAugmentedTokens(pattern) {
4178
+ var _a, _b;
4161
4179
  const customTokensMap = this._options.customTokens || {};
4162
4180
  const leafPatterns = pattern.getPatterns();
4163
- const tokens = customTokensMap[pattern.name] || [];
4164
- leafPatterns.forEach(p => {
4165
- const augmentedTokens = customTokensMap[p.name] || [];
4166
- tokens.push(...p.getTokens(), ...augmentedTokens);
4167
- });
4168
- return tokens;
4181
+ /** Using Set to
4182
+ * - prevent duplicates
4183
+ * - prevent mutation of original customTokensMap
4184
+ */
4185
+ const customTokensForPattern = new Set((_a = customTokensMap[pattern.name]) !== null && _a !== void 0 ? _a : []);
4186
+ for (const lp of leafPatterns) {
4187
+ const augmentedTokens = (_b = customTokensMap[lp.name]) !== null && _b !== void 0 ? _b : [];
4188
+ const lpsCombinedTokens = [...lp.getTokens(), ...augmentedTokens];
4189
+ for (const token of lpsCombinedTokens) {
4190
+ customTokensForPattern.add(token);
4191
+ }
4192
+ }
4193
+ return [...customTokensForPattern];
4169
4194
  }
4170
4195
  _createSuggestions(lastIndex, tokens) {
4171
- let substring = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
4196
+ let textToIndex = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
4172
4197
  const suggestionStrings = [];
4173
4198
  const options = [];
4174
4199
  for (const token of tokens) {
4175
- const suggestion = substring + token;
4176
- const startsWith = suggestion.startsWith(substring);
4200
+ // concatenated for start index identification inside createSuggestion
4201
+ const suggestion = textToIndex + token;
4177
4202
  const alreadyExist = suggestionStrings.includes(suggestion);
4178
4203
  const isSameAsText = suggestion === this._text;
4179
- if (startsWith && !alreadyExist && !isSameAsText) {
4204
+ if (!alreadyExist && !isSameAsText) {
4180
4205
  suggestionStrings.push(suggestion);
4181
- options.push(this._createSuggestion(this._cursor.text, suggestion));
4206
+ const suggestionOption = this._createSuggestion(this._cursor.text, suggestion);
4207
+ options.push(suggestionOption);
4182
4208
  }
4183
4209
  }
4184
4210
  const reducedOptions = getFurthestOptions(options);
@@ -4188,10 +4214,11 @@ class AutoComplete {
4188
4214
  _createSuggestion(fullText, suggestion) {
4189
4215
  const furthestMatch = findMatchIndex(suggestion, fullText);
4190
4216
  const text = suggestion.slice(furthestMatch);
4191
- return {
4217
+ const option = {
4192
4218
  text: text,
4193
4219
  startIndex: furthestMatch,
4194
4220
  };
4221
+ return option;
4195
4222
  }
4196
4223
  static suggestFor(text, pattern, options) {
4197
4224
  return new AutoComplete(pattern, options).suggestFor(text);