Skip to content

Commit 74d0f9a

Browse files
committed
os: add os.devnull()
Returns the platform-specific file path of the null device.
1 parent aed17e9 commit 74d0f9a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

doc/api/os.md

+12
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ The properties included on each object include:
122122
`nice` values are POSIX-only. On Windows, the `nice` values of all processors
123123
are always 0.
124124

125+
## `os.devnull()`
126+
<!-- YAML
127+
added: REPLACEME
128+
-->
129+
130+
* Returns: {string}
131+
132+
Returns the platform-specific file path of the null device:
133+
134+
* `\\.\nul` on Windows
135+
* `/dev/null` on POSIX
136+
125137
## `os.endianness()`
126138
<!-- YAML
127139
added: v0.9.4

lib/os.js

+9
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,18 @@ function userInfo(options) {
349349
return user;
350350
}
351351

352+
/**
353+
* @returns {'\\.\nul' | '/dev/nul'}
354+
*/
355+
function devnull() {
356+
return isWindows ? '\\\\.\\nul' : '/dev/null';
357+
}
358+
devnull[SymbolToPrimitive] = devnull;
359+
352360
module.exports = {
353361
arch,
354362
cpus,
363+
devnull,
355364
endianness,
356365
freemem: getFreeMem,
357366
getPriority,

test/parallel/test-os.js

+10
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,13 @@ if (!common.isIBMi) {
259259

260260
is.number(+os.freemem, 'freemem');
261261
is.number(os.freemem(), 'freemem');
262+
263+
const devnull = os.devnull();
264+
265+
if (common.isWindows) {
266+
assert.strictEqual(devnull, '\\\\.\\nul');
267+
} else {
268+
assert.strictEqual(devnull, '/dev/null');
269+
}
270+
271+
assert.strictEqual(`${os.devnull}`, devnull);

0 commit comments

Comments
 (0)