Package Exports
- extract-files
- extract-files/lib/index
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (extract-files) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
extract-files
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.
Usage
Install with npm:
npm install extract-files
See the extractFiles
documentation to get started.
Reassembly
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.
Support
- Node.js v6.10+
- Browsers >1% usage
- React Native
API
Table of contents
- class ReactNativeFile
- function extractFiles
- type ExtractedFile
- type ObjectPath
- type ReactNativeFileSubstitute
class ReactNativeFile
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. |
Examples
An extractable file in React Native.
import { ReactNativeFile } from 'extract-files' const file = new ReactNativeFile({ uri: uriFromCameraRoll, name: 'a.jpg', type: 'image/jpeg' })
function extractFiles
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.
Examples
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] }]
type ExtractedFile
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. |
type ObjectPath
String notation for the path to a node in an object tree.
Type: String
See
Examples
Object path is property a
, array index 0
, object property b
.
a.0.b
type ReactNativeFileSubstitute
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. |