All files split-words.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 1/1
100% Lines 11/11

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 21 22 23 24 25 26 27 28 29 30 31 321x 1x                                         1x 205x 205x 205x 205x 2x 2x 203x 203x  
import { clean } from './clean.ts';
import { isWhitespace } from './is-whitespace.ts';
 
/**
 * Options for the {@link splitWords} function
 * @group String
 * @category Deconstruction
 */
export type SplitWordsOptions = {
  /** The delimiter between words */
  delimiter?: string | RegExp;
};
 
/**
 * Split a string into an array of words
 * @param input - The string to split
 * @param options - see {@link SplitWordsOptions}
 * @defaultValue delimiter whitespace
 * @returns array of words
 * @group String
 * @category Deconstruction
 */
export function splitWords(
  input: string,
  { delimiter = /\s+/u }: SplitWordsOptions = {},
): string[] {
  if (input.length === 0 || isWhitespace(input)) {
    return [];
  }
  return clean(input, delimiter).split(delimiter);
}