2020-06-15
描述
检查给定的参数是否为双工流(可读和可写)。
提示
- 首先检查参数是否不等于
null - 其次使用
typeof检查参数是否为object类型,其pipe属性是否为function类型 - 最后还需要使用
typeof检查_read,_write和_readableState,_writableState属性是否分别为function和object
代码
const isDuplexStream = val =>
val !== null &&
typeof val === 'object' &&
typeof val.pipe === 'function' &&
typeof val._read === 'function' &&
typeof val._readableState === 'object' &&
typeof val._write === 'function' &&
typeof val._writableState === 'object';
示例
检测 new Stream.Duplex() 是否为双工流:
const Stream = require('stream');
isDuplexStream(new Stream.Duplex()); // true
ME!
链滴