Skip to content

Commit

Permalink
Return empty array if there's no match or if the separator is an e… (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb authored and sindresorhus committed Nov 13, 2019
1 parent b7a32b6 commit 91c3ac2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module.exports = (string, separator) => {
}

if (separator === '') {
return [string];
return [];
}

const separatorIndex = string.indexOf(separator);

if (separatorIndex === -1) {
return [string];
return [];
}

return [
Expand Down
10 changes: 4 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
This is similar to [`String#split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split), but that one splits on all the occurrences, not just the first one.


## Install

```
$ npm install split-on-first
```


## Usage

```js
Expand All @@ -27,9 +25,11 @@ splitOnFirst('a---b---c', '---');
//=> ['a', 'b---c']

splitOnFirst('a-b-c', '+');
//=> ['a-b-c']
```
//=> []

splitOnFirst('abc', '');
//=> []
```

## API

Expand All @@ -47,12 +47,10 @@ Type: `string`

The separator to split on.


## Related

- [split-at](https://github.com/sindresorhus/split-at) - Split a string at one or more indices


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ test('main', t => {
t.deepEqual(splitOnFirst('a-b-c', '-'), ['a', 'b-c']);
t.deepEqual(splitOnFirst('key:value:value2', ':'), ['key', 'value:value2']);
t.deepEqual(splitOnFirst('a---b---c', '---'), ['a', 'b---c']);
t.deepEqual(splitOnFirst('a-b-c', '+'), ['a-b-c']);
t.deepEqual(splitOnFirst('abc', ''), ['abc']);
t.deepEqual(splitOnFirst('a-b-c', '+'), []);
t.deepEqual(splitOnFirst('abc', ''), []);

t.throws(() => {
splitOnFirst('abc', null);
Expand Down

0 comments on commit 91c3ac2

Please sign in to comment.