lineSpans

tcmts.cli.main.lineSpans · #qupkxn2r · term · runs anywhere

The lines --lines asks for, as [first, last] spans counted from 1: "120", "120-180", "120-" (to the end), or several, comma-separated. Forgiving of how they're written (quotes, spaces, "120:180", "120..180", an en dash). Undefined if it isn't lines.

export function lineSpans(spec: string): [number, number][] | undefined {
  const parts = spec.replace(/["'`]/g, "").replace(/\s+/g, "").split(",").filter(Boolean);
  const spans: [number, number][] = [];
  for (const p of parts) {
    const m = /^(\d+)(?:(?:-|–|:|\.\.)(\d*))?$/.exec(p);
    if (!m) return undefined;
    const from = Math.max(1, Number(m[1]));
    const to = m[2] === undefined ? from : m[2] === "" ? Infinity : Number(m[2]);
    spans.push(to < from ? [to, from] : [from, to]);
  }
  return spans.length ? spans : undefined;
}

used by