Code Monkey home page Code Monkey logo

extract-files's Introduction

extract-files

npm version Licence Github issues Github stars

Reversibly extracts files from an object tree.

Files are extracted along with their object path to allow reassembly. Extracted files are removed from the tree. Array item files are removed without reindexing the array to simplify reassembly.

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 an object tree 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 object tree is null or not an object. The top tree node 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, '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, 'tree')
const treePath = objectPath(tree)

files.forEach(({path, file}) => treePath.set(path, file))

FileList instances in an original tree become arrays when reassembled.

Support

  • > 2% market share browsers.
  • React Native.

extract-files's People

Contributors

jaydenseric avatar giautm avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.