Giter VIP home page Giter VIP logo

extract-files's Introduction

extract-files

npm version Build status

Clones a value, recursively extracting File, Blob and ReactNativeFile instances with their object paths, replacing them with null. FileList instances are treated as File instance arrays.

Used by GraphQL multipart request spec client implementations such as graphql-react and apollo-upload-client.

Usage

Install with npm:

npm install extract-files

See the extractFiles documentation to get started.

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

Clones a value, recursively extracting File, Blob and ReactNativeFile instances with their object paths, replacing them with null. FileList instances are treated as File instance arrays.

Parameter Type Description
value * Value (typically an object tree) to extract files from.
path ObjectPath? = '' Prefix for object paths for extracted files.

Returns: ExtractFilesResult — Result.

Examples

Extract files from an object.

For the following:

import { extractFiles } from 'extract-files'

const file1 = new File(['1'], '1.txt', { type: 'text/plain' })
const file2 = new File(['2'], '2.txt', { type: 'text/plain' })
const value = {
  a: file1,
  b: [file1, file2]
}

const { clone, files } = extractFiles(value, 'prefix')

value remains the same.

clone is:

{
  a: null,
  b: [null, null]
}

files is a Map instance containing:

Key Value
file1 ['prefix.a', 'prefix.b.0']
file2 ['prefix.b.1']

type ExtractableFile

An extractable file.

Type: File | Blob | ReactNativeFile

type ExtractFilesResult

What extractFiles returns.

Type: Object

Property Type Description
clone * Clone of the original input value with files recursively replaced with null.
files Map<ExtractableFileArray<ObjectPath>> Extracted files and their locations within the original value.

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'
}

extract-files's People

Contributors

jaydenseric avatar samcoenen avatar giautm avatar loremaps avatar

Watchers

James Cloos 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.