Reversibly extracts File
, Blob
and ReactNativeFile
instances, with object paths, from an object tree and replaces them with null
. FileList
instances are treated as File
instance arrays.
Install with npm:
npm install extract-files
See the extractFiles
documentation to get started.
Loop and reinsert the extracted files using object-path
:
import { extractFiles } from 'extract-files'
import objectPath from 'object-path'
import tree from './tree'
const files = extractFiles(tree)
const treePath = objectPath(tree)
files.forEach(({ path, file }) => treePath.set(path, file))
FileList
instances in the original tree become File
instance arrays when reassembled.
- Node.js v6+
- Browsers
> 0.5%, not dead
- React Native
- class ReactNativeFile
- function extractFiles
- type ExtractedFile
- type ObjectPath
- type ReactNativeFileSubstitute
Used to mark a React Native File
substitute in an object tree for extractFiles
. It’s too risky to assume all objects with uri
, type
and name
properties are files to extract.
Parameter | Type | Description |
---|---|---|
file |
ReactNativeFileSubstitute | A React Native File substitute. |
An extractable file in React Native.
import { ReactNativeFile } from 'extract-files' const file = new ReactNativeFile({ uri: uriFromCameraRoll, name: 'a.jpg', type: 'image/jpeg' })
Reversibly extracts File
, Blob
and ReactNativeFile
instances, with object paths, from an object tree and replaces them with null
. FileList
instances are treated as File
instance arrays.
Parameter | Type | Description |
---|---|---|
tree |
Object | An object tree to extract files from. The tree itself must not be a file. |
treePath |
string? = '' |
Optional object tree path to prefix file object tree paths. |
Returns: Array<ExtractedFile> — Extracted files or an empty array if the tree is not an enumerable object.
Extracting files.
The following:
import { extractFiles } from 'extract-files' console.log( extractFiles( { a: new File(['a'], 'a.txt', { type: 'text/plain' }), b: [ { c: new File(['b'], 'b.txt', { type: 'text/plain' }) } ] }, 'prefix' ) )Logs:
[{ path: 'prefix.a', file: [object File] }, { path: 'prefix.b.0.c', file: [object File] }]
An extracted file.
Type: Object
Property | Type | Description |
---|---|---|
path |
ObjectPath | Object path to the file in the original object tree. |
file |
File | Blob | ReactNativeFile | The extracted file. |
String notation for the path to a node in an object tree.
Type: String
Object path is property a
, array index 0
, object property b
.
a.0.b
A React Native File
substitute for when using FormData
.
Type: Object
Property | Type | Description |
---|---|---|
uri |
String | Filesystem path. |
name |
String? | File name. |
type |
String? | File content type. |
A camera roll file.
{ uri: uriFromCameraRoll, name: 'a.jpg', type: 'image/jpeg' }