forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
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: nodejs#37508 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- 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