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