mirror of
https://github.com/nodejs/node.git
synced 2025-05-21 17:44:15 +00:00

Create a utils module for isIterable(), isReadable(), and isStream(). PR-URL: https://github.com/nodejs/node/pull/37508 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>
33 lines
707 B
JavaScript
33 lines
707 B
JavaScript
'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,
|
|
};
|