]> git.ardis.lu - csvio/commitdiff
Accept PathLike input for createCSVWritableStream
authorArdis Lu <ardisglu@gmail.com>
Thu, 9 Jan 2025 07:27:33 +0000 (23:27 -0800)
committerArdis Lu <ardisglu@gmail.com>
Thu, 9 Jan 2025 07:27:33 +0000 (23:27 -0800)
To be consistent with createCSVReadableStream and also allow paths relative to JS file (vs. execution directory only).

src/core.js

index 1fad6a2cc5f5e1804fa0afdcafb3a8b3299fb5d6..48f327fe629714e5580e4daa74f579ae45e89b14 100644 (file)
@@ -168,12 +168,21 @@ export function createCSVTransformStream(fn, options = {}) {
  * A simple wrapper around Node.js's [`fs.writeFile`](https://nodejs.org/api/fs.html#fspromiseswritefilefile-data-options) to write
  * streamed data to a file. If the input data is JSON, the data is converted to a string representing a RFC 4180 CSV record. If the
  * data is not JSON (e.g., if it is already converted to a CSV string), the data is written to the CSV file directly.
- * @param {string} path A `string` representing a path to a local CSV file destination. If the file already exists, its **data
- * will be overwritten**.
+ * @param {PathLike} path A `string`, `Buffer`, or `URL` representing a path to a local CSV file destination. If the file already
+ * exists, its **data will be overwritten**.
  * @returns {WritableStream} A `WritableStream` to write data to a file on disk.
  */
 export function createCSVWritableStream(path) {
-  const fullPath = normalize(path);
+  let fullPath;
+  if (typeof path === 'string') {
+    fullPath = normalize(path);
+  }
+  else if (ArrayBuffer.isView(path) || path instanceof ArrayBuffer) {
+    fullPath = normalize(new TextDecoder().decode(path));
+  }
+  else { // Assuming URL
+    fullPath = normalize(path.pathname.substring(1));
+  }
   const dir = dirname(fullPath);
   let handle;
 
This page took 0.040235 seconds and 4 git commands to generate.