diff --git a/index.js b/index.js index d914070..14d4e3c 100644 --- a/index.js +++ b/index.js @@ -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 [ diff --git a/readme.md b/readme.md index 2463cf1..8380dc6 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -27,9 +25,11 @@ splitOnFirst('a---b---c', '---'); //=> ['a', 'b---c'] splitOnFirst('a-b-c', '+'); -//=> ['a-b-c'] -``` +//=> [] +splitOnFirst('abc', ''); +//=> [] +``` ## API @@ -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) diff --git a/test.js b/test.js index 9b7ddf2..10f00e8 100644 --- a/test.js +++ b/test.js @@ -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);