From 38338dd26da9e624d72910b0dc5373f8d44b98c2 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Fri, 24 Mar 2023 14:41:16 -0700 Subject: [PATCH] Add support for Symbol.split 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`. --- index.js | 2 +- test.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e969e4b..9b59f6c 100644 --- a/index.js +++ b/index.js @@ -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/ } diff --git a/test.js b/test.js index 964e121..a7f9838 100644 --- a/test.js +++ b/test.js @@ -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') +})