Skip to content

Commit

Permalink
Add support for Symbol.split (#59)
Browse files Browse the repository at this point in the history
The readme says that split2 takes the same arguments as String.split, but
it was missing support for objects with the [Symbol.split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/split) property.

This has been supported since Node 6, which is far below `engines.node`.
  • Loading branch information
connor4312 authored Mar 26, 2023
1 parent c3ecc9c commit 37baeae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function split (matcher, mapper, options) {
mapper = matcher
matcher = /\r?\n/
// If options is only argument.
} else if (typeof matcher === 'object' && !(matcher instanceof RegExp)) {
} else if (typeof matcher === 'object' && !(matcher instanceof RegExp) && !matcher[Symbol.split]) {
options = matcher
matcher = /\r?\n/
}
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,20 @@ test('mapper throws on transform', function (t) {
input.write('\n')
input.end('b')
})

test('supports Symbol.split', function (t) {
t.plan(2)

const input = split({
[Symbol.split] (str) {
return str.split('~')
}
})

input.pipe(strcb(function (err, list) {
t.error(err)
t.deepEqual(list, ['hello', 'world'])
}))

input.end('hello~world')
})

0 comments on commit 37baeae

Please sign in to comment.