index.js 464 B

1234567891011121314151617181920
  1. export default function splitOnFirst(string, separator) {
  2. if (!(typeof string === 'string' && typeof separator === 'string')) {
  3. throw new TypeError('Expected the arguments to be of type `string`');
  4. }
  5. if (string === '' || separator === '') {
  6. return [];
  7. }
  8. const separatorIndex = string.indexOf(separator);
  9. if (separatorIndex === -1) {
  10. return [];
  11. }
  12. return [
  13. string.slice(0, separatorIndex),
  14. string.slice(separatorIndex + separator.length)
  15. ];
  16. }