From c3ae5149802e31d231c545ed77fad3e7d05fb202 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Mon, 4 Apr 2022 19:07:50 +0800 Subject: [PATCH] fs: make params in writing methods optional This change allows passing objects as "named parameters": - `fs.write(fd, buffer[, options], callback)` - `fs.writeSync(fd, buffer[, options])` - `filehandle.write(buffer[, options])` Fixes: https://github.com/nodejs/node/issues/41666 PR-URL: https://github.com/nodejs/node/pull/42601 Reviewed-By: Antoine du Hamel Reviewed-By: Minwoo Jung Reviewed-By: James M Snell --- doc/api/fs.md | 67 +++++++++++- lib/fs.js | 37 +++++-- lib/internal/fs/promises.js | 11 +- .../test-fs-promises-write-optional-params.js | 95 ++++++++++++++++ .../parallel/test-fs-write-optional-params.js | 102 ++++++++++++++++++ .../test-fs-write-sync-optional-params.js | 89 +++++++++++++++ 6 files changed, 388 insertions(+), 13 deletions(-) create mode 100644 test/parallel/test-fs-promises-write-optional-params.js create mode 100644 test/parallel/test-fs-write-optional-params.js create mode 100644 test/parallel/test-fs-write-sync-optional-params.js diff --git a/doc/api/fs.md b/doc/api/fs.md index 0c9c6a9373d8c1..014d6cbadbea07 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -608,7 +608,7 @@ added: v10.0.0 Change the file system timestamps of the object referenced by the {FileHandle} then resolves the promise with no arguments upon success. -#### `filehandle.write(buffer[, offset[, length[, position]]])` +#### `filehandle.write(buffer, offset[, length[, position]])` + +* `buffer` {Buffer|TypedArray|DataView} +* `options` {Object} + * `offset` {integer} **Default:** `0` + * `length` {integer} **Default:** `buffer.byteLength - offset` + * `position` {integer} **Default:** `null` +* Returns: {Promise} + +Write `buffer` to the file. + +Similar to the above `filehandle.write` function, this version takes an +optional `options` object. If no `options` object is specified, it will +default with the above values. + #### `filehandle.write(string[, position[, encoding]])` + +* `fd` {integer} +* `buffer` {Buffer|TypedArray|DataView} +* `options` {Object} + * `offset` {integer} **Default:** `0` + * `length` {integer} **Default:** `buffer.byteLength - offset` + * `position` {integer} **Default:** `null` +* `callback` {Function} + * `err` {Error} + * `bytesWritten` {integer} + * `buffer` {Buffer|TypedArray|DataView} + +Write `buffer` to the file specified by `fd`. + +Similar to the above `fs.write` function, this version takes an +optional `options` object. If no `options` object is specified, it will +default with the above values. + ### `fs.write(fd, string[, position[, encoding]], callback)` + +* `fd` {integer} +* `buffer` {Buffer|TypedArray|DataView} +* `options` {Object} + * `offset` {integer} **Default:** `0` + * `length` {integer} **Default:** `buffer.byteLength - offset` + * `position` {integer} **Default:** `null` +* Returns: {number} The number of bytes written. + +For detailed information, see the documentation of the asynchronous version of +this API: [`fs.write(fd, buffer...)`][]. + ### `fs.writeSync(fd, string[, position[, encoding]])`