All files delimited.ts

100% Statements 9/9
100% Branches 5/5
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 211x                     1x 23x 2x 2x   21x 23x 23x 23x  
import { empty } from './unicode.ts';
 
/**
 * Return a field from a delimited string
 * @param input - The delimited string
 * @param delimiter - The delimiter string
 * @param index - The position of the desired field, 0 is the first field, negative numbers count backwards from the end (default 0)
 * @param count - The number of fields to return (default 1)
 * @group String
 * @category Deconstruction
 */
export function delimited(input: string, delimiter: string, index = 0, count = 1): string {
  if (count <= 0) {
    return empty;
  }
 
  const splits = input.split(delimiter);
  const start = index < 0 ? splits.length + index : index;
  return splits.slice(start, start + count).join(delimiter);
}