Repository: motion-canvas/motion-canvas
File: packages/2d/src/lib/code/CodeRange.ts (TypeScript)

Code before:
      [
        [0, 0],
        [Infinity, Infinity],
      ],
    ];
  }
  const firstRange = ranges[0];
  const result: CodeRange[] = [];
  for (let first = 0; first < ranges.length - 1; first++) {
    const range1 = ranges[first];
    const range2 = ranges[first + 1];
    result.push([range1[1], range2[0]]);
  }
  const lastRange = ranges.slice(-1)[0];
  return [
    [[0, 0], firstRange[0]],
    ...result,
    [lastRange[1], [Infinity, Infinity]],
  ];
}

/**
 * Find all code ranges that match the given pattern.
 *
 * @param code - The code to search in.
 * @param pattern - Either a string or a regular expression to search for. When a string is passed, it looks for **exact matches**. When a RegExp object is passed, it will match against it.
 * @param limit - An optional limit on the number of ranges to find.
 */
export function findAllCodeRanges(
  code: string,
  pattern: string | RegExp,
  limit = Infinity,
): CodeRange[] {
  if (typeof pattern === 'string') {
    pattern = new RegExp(escapeRegExpString(pattern), 'g');
  } else {
    pattern = new RegExp(pattern, 'g');
  }

  const matches = code.matchAll(pattern);
  const ranges: CodeRange[] = [];
  let index = 0;
  let line = 0;
  let column = 0;

  for (const match of matches) {
    if (match.index === undefined || ranges.length >= limit) {
      continue;
    }

    let from: CodePoint = [line, column];

Review comment (suggestion):
Nit but can you apply this formatting to stay in the character limit:
```suggestion
 * @param pattern - Either a string or a regular expression to search for.
 *                  When a string is passed, it looks for **exact matches**.
 *                  When a RegExp object is passed, it will match against it.
```
Sadly I don't have a formatter configured for that yet.

Code after:
      [
        [0, 0],
        [Infinity, Infinity],
      ],
    ];
  }
  const firstRange = ranges[0];
  const result: CodeRange[] = [];
  for (let first = 0; first < ranges.length - 1; first++) {
    const range1 = ranges[first];
    const range2 = ranges[first + 1];
    result.push([range1[1], range2[0]]);
  }
  const lastRange = ranges.slice(-1)[0];
  return [
    [[0, 0], firstRange[0]],
    ...result,
    [lastRange[1], [Infinity, Infinity]],
  ];
}

/**
 * Find all code ranges that match the given pattern.
 *
 * @param code - The code to search in.
 * @param pattern - Either a string or a regular expression to search for.
 *                  When a string is passed, it looks for **exact matches**.
 *                  When a RegExp object is passed, it will match against it.
 * @param limit - An optional limit on the number of ranges to find.
 */
export function findAllCodeRanges(
  code: string,
  pattern: string | RegExp,
  limit = Infinity,
): CodeRange[] {
  if (typeof pattern === 'string') {
    pattern = new RegExp(escapeRegExpString(pattern), 'g');
  } else {
    pattern = new RegExp(pattern, 'g');
  }

  const matches = code.matchAll(pattern);
  const ranges: CodeRange[] = [];
  let index = 0;
  let line = 0;
  let column = 0;

  for (const match of matches) {
    if (match.index === undefined || ranges.length >= limit) {
      continue;
    }