Skip to content

peterhoneder/extract-files

 
 

Repository files navigation

extract-files

npm version Build status

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

API

Table of contents

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.

See

Examples

A camera roll file.

{
  uri: uriFromCameraRoll,
  name: 'a.jpg',
  type: 'image/jpeg'
}

About

Reversibly extracts files from an object tree.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%