-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: move duplicated code to an internal module
Create a utils module for isIterable(), isReadable(), and isStream(). PR-URL: #37508 Backport-PR-URL: #39973 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
Showing
4 changed files
with
40 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const { | ||
SymbolAsyncIterator, | ||
SymbolIterator, | ||
} = primordials; | ||
|
||
function isReadable(obj) { | ||
return !!(obj && typeof obj.pipe === 'function'); | ||
} | ||
|
||
function isWritable(obj) { | ||
return !!(obj && typeof obj.write === 'function'); | ||
} | ||
|
||
function isStream(obj) { | ||
return isReadable(obj) || isWritable(obj); | ||
} | ||
|
||
function isIterable(obj, isAsync) { | ||
if (!obj) return false; | ||
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function'; | ||
if (isAsync === false) return typeof obj[SymbolIterator] === 'function'; | ||
return typeof obj[SymbolAsyncIterator] === 'function' || | ||
typeof obj[SymbolIterator] === 'function'; | ||
} | ||
|
||
module.exports = { | ||
isIterable, | ||
isReadable, | ||
isStream, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters