martijnversluis/ChordSheetJS

View on GitHub
src/parser/parser_helpers.ts

Summary

Maintainability
A
0 mins
Test Coverage
export function chopFirstWord(string: string) {
  const result = /(\s+)(\S+)/.exec(string);
  const secondWordPosition = result ? (result.index + result[1].length) : null;

  if (secondWordPosition && secondWordPosition !== -1) {
    return [
      string.substring(0, secondWordPosition).trim(),
      string.substring(secondWordPosition),
    ];
  }

  return [
    /.+\s+$/.test(string) ? `${string.trim()} ` : string,
    null,
  ];
}