2020-06-16
描述
检查给定参数是否为可读流。
提示
- 首先检查参数是否不等于
null - 其次使用
typeof检查参数是否为object类型,其pipe属性是否为function类型 - 最后还需要使用
typeof检查_read和_readableState属性是否分别为function和object
代码
const isReadableStream = val =>
val !== null &&
typeof val === 'object' &&
typeof val.pipe === 'function' &&
typeof val._read === 'function' &&
typeof val._readableState === 'object';
示例
检测 fs.createReadStream() 是否为可读流:
const fs = require('fs');
isReadableStream(fs.createReadStream('test.txt')); // true
ME!
链滴