jaydenseric / extract-files

A function to recursively extract files and their object paths within a value, replacing them with null in a deep clone without mutating the original value. FileList instances are treated as File instance arrays. Files are typically File and Blob instances.
https://npm.im/extract-files
MIT License
56 stars 23 forks source link

Optional Environment Support beside Browsers/React-Native #10

Closed n1ru4l closed 4 years ago

n1ru4l commented 5 years ago

I know that this package is intended for browser/react-native environments.

By adding a new parameter for extractFiles, e.g. isFileValue: (value: unknown) => boolean, other higher level implementations that are not browser-specific could also leverage the logic of this package.

This is how I would imagine a Node.js implementation that uses fs.ReadStream.

"use strict";

const fs = require("fs");
const extractFiles = require("extract-files");

function isFileValue(value) {
  return value instanceof fs.ReadStream;
}

const { clone, files } = extractFiles(input, "", isFileValue);

The isFileValue function would then (if defined) used instead of the code block here:

https://github.com/jaydenseric/extract-files/blob/30292c400a48e8685ca541621095dbf87502ef2d/src/extractFiles.mjs#L69-L73

The default value stays the same.

function defaultIsFileValue(value) {
  return (
    (typeof File !== 'undefined' && value instanceof File) ||
    (typeof Blob !== 'undefined' && value instanceof Blob) ||
    value instanceof ReactNativeFile
  )
}

@jaydenseric What is your opinion on this? Are there any concerns? Would the impact on the bundle size be too huge? Could you imagine such a feature becoming part of this package and would you accept a pull request that implements this feature?

jaydenseric commented 5 years ago

This is a nice idea :)

Consideration: If a consumer wants to just extend the default set with one more type of "file", they will have to copy and paste the defaults across into their own function, and both sets will appear in their bundle. Maybe if the default isFileValue() was exported, they could use it inside their custom function 🤔

jaydenseric commented 4 years ago

This enhancement has been published in v6.0.0 🚀