MCPcopy Create free account
hub / github.com/adobe/react-spectrum / findInText

Function findInText

packages/react-stately/src/tokenfield/TokenFieldValue.ts:441–468  ·  view source on GitHub ↗
(
  text: string,
  search: string | RegExp,
  direction: Direction,
  fromOffset?: number
)

Source from the content-addressed store, hash-verified

439}
440
441function findInText(
442 text: string,
443 search: string | RegExp,
444 direction: Direction,
445 fromOffset?: number
446): number {
447 if (typeof search === 'string') {
448 if (direction === Direction.Backward) {
449 return text.lastIndexOf(search, fromOffset !== undefined ? fromOffset - 1 : text.length - 1);
450 }
451 return text.indexOf(search, fromOffset ?? 0);
452 }
453
454 if (direction === Direction.Forward) {
455 let start = fromOffset ?? 0;
456 let index = text.slice(start).search(search);
457 return index >= 0 ? start + index : -1;
458 }
459
460 let limit = fromOffset !== undefined ? fromOffset : text.length;
461 if (limit < 0) {
462 return -1;
463 }
464
465 let re = search.flags.includes('g') ? search : new RegExp(search.source, search.flags + 'g');
466 let matches = Array.from(text.slice(0, limit).matchAll(re));
467 return matches.at(-1)?.index ?? -1;
468}
469
470function appendSegments(
471 segments: TokenFieldSegment[],

Callers 1

findTextMethod · 0.85

Calls 2

sliceMethod · 0.80
atMethod · 0.65

Tested by

no test coverage detected