30秒学会 JavaScript 片段 · 2017年10月8日

30秒学会 JavaScript 片段 – isWritableStream

Checks if the given argument is a writable stream.

Check if the value is different from null, use typeof to check if the value is of type object and the pipe property is of type function.
Additionally check if the typeof the _write and _writableState properties are function and object respectively.

代码片段

const isWritableStream = val =>
  val !== null &&
  typeof val === 'object' &&
  typeof val.pipe === 'function' &&
  typeof val._write === 'function' &&
  typeof val._writableState === 'object';

使用样例

const fs = require('fs');
isWritableStream(fs.createWriteStream('test.txt')); // true