JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3974075
  • Score
    100M100P100Q214794F
  • License MIT

Reversibly extracts files from an object tree.

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

npm version Licence Github issues Github stars Travis status

Reversibly extracts files from a tree object.

Files are extracted along with their object path to allow reassembly and are replaced with null in the original tree.

Files may be File and ReactNativeFile instances. FileList instances are converted to arrays and the items are extracted as File instances.

Usage

Install with npm:

npm install extract-files

extractFiles accepts a tree object to extract files from, along with an optional tree path to prefix file paths:

import extractFiles from 'extract-files'
import tree from './tree'

const files = extractFiles(tree, 'tree')

Extracted files are an array:

[{
  path: 'tree.foo',
  file: /* File instance */
}, {
  path: 'tree.bar.0',
  file: /* File instance */
}, {
  path: 'tree.bar.1',
  file: /* File instance */
}]

extractFiles will return an empty array if the tree is not an object or null. The tree itself must not be a file.

React Native

React Native polyfills FormData under the hood and objects with the properties uri, type and name substitute window.File. It would be risky to assume all objects with those properties in a tree are files. Use ReactNativeFile instances within a tree to explicitly mark files:

import extractFiles, { ReactNativeFile } from 'extract-files'

const tree = {
  singleFile: new ReactNativeFile({
    uri: uriFromCameraRoll,
    type: 'image/jpeg',
    name: 'photo.jpg'
  }),
  multipleFiles: ReactNativeFile.list([
    {
      uri: uriFromCameraRoll1,
      type: 'image/jpeg',
      name: 'photo-1.jpg'
    },
    {
      uri: uriFromCameraRoll2,
      type: 'image/jpeg',
      name: 'photo-2.jpg'
    }
  ])
}

const files = extractFiles(tree)

Reassembly

object-path can be used to loop and reinsert the extracted files:

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 an original tree become arrays when reassembled.

Support

  • Node.js v6.10+, see package.json engines.
  • Browsers >1% usage, see package.json browserslist.
  • React Native.