Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

extract-files

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extract-files - npm Package Compare versions

Comparing version 3.1.0 to 4.0.0

lib/extractFiles.js

133

changelog.md
# extract-files changelog
## 4.0.0
### Major
- `extractFiles` is a named export again, and there is no longer a default export; mixed named and default exports causes native ESM and CJS interoperability issues.
- `isObject` is no longer exported. It was not a documented API anyway.
- Removed the `ReactNativeFile` static function `list`. It added surface area to the API and bundles and is simple to do manually.
### Minor
- Updated Babel, removing the `@babel/runtime` dependency.
- Refactored package scripts to use `prepare` to support installation via Git (e.g. `npm install jaydenseric/extract-files`).
- Package [marked side-effect free](https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free) for bundlers and tree-shaking.
### Patch
- Use [`jsdoc-md`](https://npm.im/jsdoc-md) to generate readme API docs from source JSDoc, which has been much improved.
- Use [`tap`](https://npm.im/tap) instead of [`ava`](https://npm.im/ava). Tests no longer transpile on the fly, are faster and AVA no longer dictates the Babel version.
- Tests run against the actual dist `.mjs` and `.js` files in both native ESM (`--experimental-modules`) and CJS environments.
- Added a package `test:size` script, using [`size-limit`](https://npm.im/size-limit) to guarantee < 500 byte ESM and CJS bundle sizes.
- Removed the package clean script `rimraf` dev dependency in favour of native `rm -rf`.
- Removed the package `fix` script.
- Renamed the `MODULE` environment variable to `BABEL_ESM` to be more specific for the package `prepare:mjs` script.
- Lint `.json`, `.yml` and `.md` files.
- Use `.prettierignore` to leave `package.json` formatting to npm.
- Use [`eslint-config-env`](https://npm.im/eslint-config-env).
- Compact package `repository` field.
- Updated package description.
- HTTPS package author URL.
- Added package keywords.
- Replaced [shields.io](https://shields.io) readme badges with:
- A [Badgen](https://badgen.net) npm version badge.
- An official Travis badge that only tracks `master` branch.
- Changelog version entries now have “Major”, “Minor” and “Patch” subheadings.
## 3.1.0
* Updated dependencies.
* Added support for [`Blob`](https://developer.mozilla.org/en/docs/Web/API/Blob) types, via [#5](https://github.com/jaydenseric/extract-files/pull/5).
### Minor
- Added support for [`Blob`](https://developer.mozilla.org/en/docs/Web/API/Blob) types, via [#5](https://github.com/jaydenseric/extract-files/pull/5).
### Patch
- Updated dependencies.
## 3.0.0
* Updated dependencies.
* The `extractFiles` function is now the default export.
* Replace extracted files with `null` instead of deletion, fixing [#4](https://github.com/jaydenseric/extract-files/issues/4).
* Simplified npm scripts.
### Major
- The `extractFiles` function is now the default export.
- Replace extracted files with `null` instead of deletion, fixing [#4](https://github.com/jaydenseric/extract-files/issues/4).
### Patch
- Updated dev dependencies.
- Simplified npm scripts.
## 2.1.1
* Setup Travis to test using the latest stable Node.js version and the oldest supported in `package.json` `engines` (v6.10).
* Added a Travis readme badge.
### Patch
- Setup Travis to test using the latest stable Node.js version and the oldest supported in `package.json` `engines` (v6.10).
- Added a Travis readme badge.
## 2.1.0
* Updated dependencies.
* Updated Prettier and ESLint config.
* Prettier formats distribution code as well as source code, along with the readme and changelog.
* No more source maps; Prettier does not support them.
* Added a clean step to builds.
* Renamed `dist` directory to `lib`.
* Modular project structure that works better for native ESM.
* Module files now have `.mjs` extension.
* Using Babel directly instead of Rollup.
* Smarter Babel config with `.babelrc.js`.
* Reduced bundle size, fixing [#3](https://github.com/jaydenseric/extract-files/issues/3):
* Using `babel-preset-env` to handle polyfills so only required ones are included for supported environments.
* Using `Array.prototype.slice.call` to convert `FileList` instances to arrays instead of `Array.from` which requires a lot of polyfills.
* Target Node.js v6.10+ for transpilation and polyfills via `package.json` `engines`.
* Support [browsers with >1% global usage](http://browserl.ist/?q=%3E1%25) (was >2%).
* Updated the readme support section.
* Added links to readme badges.
### Minor
- Support [browsers with >1% global usage](http://browserl.ist/?q=%3E1%25) (was >2%).
- Target Node.js v6.10+ for transpilation and polyfills via `package.json` `engines`.
- Support Node.js native ESM via `--experimental-modules`:
- Module files now have `.mjs` extension.
- Modular project structure that works better for native ESM.
- Reduced bundle size, fixing [#3](https://github.com/jaydenseric/extract-files/issues/3):
- Using `babel-preset-env` to handle polyfills so only required ones are included for supported environments.
- Using `Array.prototype.slice.call` to convert `FileList` instances to arrays instead of `Array.from` which requires a lot of polyfills.
### Patch
- Updated dependencies.
- Updated Prettier and ESLint config.
- Prettier formats distribution code as well as source code, along with the readme and changelog.
- No more source maps; Prettier does not support them.
- Added a clean step to builds.
- Renamed `dist` directory to `lib`.
- Using Babel directly instead of Rollup.
- Smarter Babel config with `.babelrc.js`.
- Added links to readme badges.
- Updated the readme support section.
## 2.0.1
* Updated dependencies.
* Fixed incorrect usage example code for `ReactNativeFile.list`, via [#1](https://github.com/jaydenseric/extract-files/pull/1).
### Patch
- Updated dependencies.
- Fixed incorrect usage example code for `ReactNativeFile.list`, via [#1](https://github.com/jaydenseric/extract-files/pull/1).
## 2.0.0
* Updated dependencies.
* Extracted file paths no longer begin with `.` when no tree path is passed to `extractFiles`.
### Major
- Extracted file paths no longer begin with `.` when no tree path is passed to `extractFiles`.
### Patch
- Updated dev dependencies.
## 1.1.0
* Updated dependencies.
* Added tests.
* Removed `lint-staged`, linting and tests now run on commit.
* Fixed `extractFiles` bugs by using logic that worked in [`apollo-upload-client@5`](https://github.com/jaydenseric/apollo-upload-client/tree/v5.0.0).
### Minor
- Added tests.
### Patch
- Updated dev dependencies.
- Removed `lint-staged`, linting and tests now run on commit.
- Fixed `extractFiles` bugs by using logic that worked in [`apollo-upload-client@5`](https://github.com/jaydenseric/apollo-upload-client/tree/v5.0.0).
## 1.0.0
* Initial release.
Initial release.
'use strict'
exports.__esModule = true
exports.default = extractFiles
exports.ReactNativeFile = exports.isObject = void 0
require('core-js/modules/es6.function.name')
var _extractFiles = require('./extractFiles')
var isObject = function isObject(node) {
return typeof node === 'object' && node !== null
}
Object.keys(_extractFiles).forEach(function(key) {
if (key === 'default' || key === '__esModule') return
exports[key] = _extractFiles[key]
})
exports.isObject = isObject
var _ReactNativeFile = require('./ReactNativeFile')
function extractFiles(tree, treePath) {
if (treePath === void 0) {
treePath = ''
}
var files = []
var recurse = function recurse(node, nodePath) {
Object.keys(node).forEach(function(key) {
if (!isObject(node[key])) return
var path = '' + nodePath + key
if (
(typeof File !== 'undefined' && node[key] instanceof File) ||
(typeof Blob !== 'undefined' && node[key] instanceof Blob) ||
node[key] instanceof ReactNativeFile
) {
files.push({
path: path,
file: node[key]
})
node[key] = null
return
}
if (typeof FileList !== 'undefined' && node[key] instanceof FileList)
node[key] = Array.prototype.slice.call(node[key])
recurse(node[key], path + '.')
})
}
if (isObject(tree)) recurse(tree, treePath === '' ? treePath : treePath + '.')
return files
}
var ReactNativeFile = function ReactNativeFile(_ref) {
var uri = _ref.uri,
type = _ref.type,
name = _ref.name
this.uri = uri
this.type = type
this.name = name
}
exports.ReactNativeFile = ReactNativeFile
ReactNativeFile.list = function(files) {
return files.map(function(file) {
return new ReactNativeFile(file)
})
}
Object.keys(_ReactNativeFile).forEach(function(key) {
if (key === 'default' || key === '__esModule') return
exports[key] = _ReactNativeFile[key]
})
{
"name": "extract-files",
"version": "3.1.0",
"description": "Reversibly extracts files from an object tree.",
"version": "4.0.0",
"description": "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.",
"license": "MIT",

@@ -9,8 +9,5 @@ "author": {

"email": "me@jaydenseric.com",
"url": "http://jaydenseric.com"
"url": "https://jaydenseric.com"
},
"repository": {
"type": "git",
"url": "https://github.com/jaydenseric/extract-files.git"
},
"repository": "github:jaydenseric/extract-files",
"homepage": "https://github.com/jaydenseric/extract-files#readme",

@@ -21,3 +18,10 @@ "bugs": "https://github.com/jaydenseric/extract-files/issues",

"file",
"files"
"files",
"File",
"FileList",
"Blob",
"react",
"native",
"esm",
"mjs"
],

@@ -30,2 +34,3 @@ "files": [

"module": "lib/index.mjs",
"sideEffects": false,
"engines": {

@@ -35,71 +40,67 @@ "node": ">=6.10"

"browserslist": ">1%",
"dependencies": {
"@babel/runtime": "^7.0.0-beta.38"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.38",
"@babel/core": "^7.0.0-beta.38",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.38",
"@babel/plugin-transform-runtime": "^7.0.0-beta.38",
"@babel/preset-env": "^7.0.0-beta.38",
"ava": "^0.25.0",
"babel-eslint": "^8.2.1",
"eslint": "^4.16.0",
"eslint-plugin-prettier": "^2.5.0",
"@babel/cli": "^7.0.0-rc.1",
"@babel/core": "^7.0.0-rc.1",
"@babel/plugin-proposal-class-properties": "^7.0.0-rc.1",
"@babel/preset-env": "^7.0.0-rc.1",
"babel-eslint": "^8.2.6",
"eslint": "^5.3.0",
"eslint-config-env": "^1.0.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-prettier": "^2.6.2",
"husky": "^0.14.3",
"lint-staged": "^6.1.0",
"prettier": "^1.10.2",
"rimraf": "^2.6.2"
"if-ver": "^1.0.6",
"jsdoc-md": "^1.4.0",
"lint-staged": "^7.2.2",
"prettier": "^1.14.2",
"size-limit": "^0.19.2",
"tap": "^12.0.1"
},
"scripts": {
"build": "rimraf lib && npm run build:js && npm run build:mjs && prettier --write 'lib/**/*.{js,mjs}'",
"build:js": "babel src --out-dir lib",
"build:mjs": "MODULE=true babel src --out-dir lib --keep-file-extension",
"lint": "eslint . --ext js,mjs",
"fix": "npm run lint -- --fix && prettier --write '**/*.md'",
"pretest": "npm run lint && npm run build",
"test": "ava lib/test.js --serial --verbose",
"precommit": "lint-staged",
"prepublishOnly": "npm run test"
"prepare": "npm run prepare:clean && npm run prepare:mjs && npm run prepare:js && npm run prepare:jsdoc && npm run prepare:prettier",
"prepare:clean": "rm -rf lib",
"prepare:mjs": "BABEL_ESM=1 babel src -d lib --keep-file-extension",
"prepare:js": "babel src -d lib",
"prepare:jsdoc": "jsdoc-md",
"prepare:prettier": "prettier 'lib/**/*.{mjs,js}' readme.md --write",
"test": "npm run test:eslint && npm run test:prettier && npm run test:size && npm run test:mjs && npm run test:js",
"test:eslint": "eslint . --ext mjs,js",
"test:prettier": "prettier '**/*.{json,yml,md}' -l",
"test:size": "size-limit",
"test:mjs": "if-ver -ge 8.5 || exit 0; node --experimental-modules --no-warnings lib/test | tap-mocha-reporter spec",
"test:js": "node lib/test | tap-mocha-reporter spec",
"prepublishOnly": "npm run prepare && npm test",
"precommit": "lint-staged"
},
"lint-staged": {
"*.mjs": "eslint"
"*.{mjs,js}": "eslint",
"*.{json,yml,md}": "prettier -l"
},
"eslintIgnore": [
"!.babelrc.js",
"lib"
],
"eslintConfig": {
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017,
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
"env": {
"es6": true,
"node": true,
"browser": true
},
"extends": [
"eslint:recommended"
],
"plugins": [
"prettier"
],
"rules": {
"curly": [
"error",
"multi"
],
"prettier/prettier": "error"
}
"env"
]
},
"prettier": {
"proseWrap": "never",
"semi": false,
"singleQuote": true
}
"singleQuote": true,
"semi": false
},
"size-limit": [
{
"name": "ESM",
"path": "lib/index.mjs",
"limit": "500 B"
},
{
"name": "CJS",
"path": "lib/index.js",
"limit": "500 B"
}
]
}
# extract-files
[![npm version](https://img.shields.io/npm/v/extract-files.svg)](https://npm.im/extract-files) ![Licence](https://img.shields.io/npm/l/extract-files.svg) [![Github issues](https://img.shields.io/github/issues/jaydenseric/extract-files.svg)](https://github.com/jaydenseric/extract-files/issues) [![Github stars](https://img.shields.io/github/stars/jaydenseric/extract-files.svg)](https://github.com/jaydenseric/extract-files/stargazers) [![Travis status](https://img.shields.io/travis/jaydenseric/extract-files.svg)](https://travis-ci.org/jaydenseric/extract-files)
[![npm version](https://badgen.net/npm/v/extract-files)](https://npm.im/extract-files) [![Build status](https://travis-ci.org/jaydenseric/extract-files.svg?branch=master)](https://travis-ci.org/jaydenseric/extract-files)
Reversibly extracts files from a tree object.
Reversibly extracts [`File`](https://developer.mozilla.org/docs/web/api/file), [`Blob`](https://developer.mozilla.org/docs/web/api/blob) and [`ReactNativeFile`](#class-reactnativefile) instances, with [object paths](#type-objectpath), from an object tree and replaces them with `null`. [`FileList`](https://developer.mozilla.org/docs/web/api/filelist) instances are treated as [`File`](https://developer.mozilla.org/docs/web/api/file) instance arrays.
Files are extracted along with their object path to allow reassembly and are replaced with `null` in the original tree.
Files may be [`File`](https://developer.mozilla.org/en/docs/Web/API/File), [`Blob`](https://developer.mozilla.org/en/docs/Web/API/Blob) and [`ReactNativeFile`](https://github.com/jaydenseric/extract-files#react-native) instances. [`FileList`](https://developer.mozilla.org/en/docs/Web/API/FileList) instances are converted to arrays and the items are extracted as `File` instances.
## Usage

@@ -19,79 +15,150 @@

`extractFiles` accepts a tree object to extract files from, along with an optional tree path to prefix file paths:
See the [`extractFiles`](#function-extractfiles) documentation to get started.
### Reassembly
Loop and reinsert the extracted files using [`object-path`](https://npm.im/object-path):
```js
import extractFiles from 'extract-files'
import { extractFiles } from 'extract-files'
import objectPath from 'object-path'
import tree from './tree'
const files = extractFiles(tree, 'tree')
const files = extractFiles(tree)
const treePath = objectPath(tree)
files.forEach(({ path, file }) => treePath.set(path, file))
```
Extracted files are an array:
[`FileList`](https://developer.mozilla.org/docs/web/api/filelist) instances in the original tree become [`File`](https://developer.mozilla.org/docs/web/api/file) instance arrays when reassembled.
```js
[{
path: 'tree.foo',
file: /* File or Blob instance */
}, {
path: 'tree.bar.0',
file: /* File or Blob instance */
}, {
path: 'tree.bar.1',
file: /* File or Blob instance */
}]
```
## Support
`extractFiles` will return an empty array if the tree is not an object or `null`. The tree itself must not be a file.
- Node.js v6.10+
- [Browsers >1% usage](http://browserl.ist/?q=%3E1%25)
- React Native
### React Native
## API
React Native [polyfills FormData](https://github.com/facebook/react-native/blob/v0.45.1/Libraries/Network/FormData.js) 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:
### Table of contents
```js
import extractFiles, { ReactNativeFile } from 'extract-files'
- [class ReactNativeFile](#class-reactnativefile)
- [Examples](#examples)
- [function extractFiles](#function-extractfiles)
- [Examples](#examples-1)
- [type ExtractedFile](#type-extractedfile)
- [type ObjectPath](#type-objectpath)
- [See](#see)
- [Examples](#examples-2)
- [type ReactNativeFileSubstitute](#type-reactnativefilesubstitute)
- [See](#see-1)
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'
}
])
}
### class ReactNativeFile
const files = extractFiles(tree)
```
Used to mark a [React Native `File` substitute](#type-reactnativefilesubstitute) in an object tree for [`extractFiles`](#function-extractfiles). It’s too risky to assume all objects with `uri`, `type` and `name` properties are files to extract.
### Reassembly
| Parameter | Type | Description |
| :-------- | :----------------------------------------------------------- | :----------------------------------------------------------------------------------- |
| `file` | [ReactNativeFileSubstitute](#type-reactnativefilesubstitute) | A React Native [`File`](https://developer.mozilla.org/docs/web/api/file) substitute. |
`object-path` can be used to loop and reinsert the extracted files:
#### Examples
```js
import extractFiles from 'extract-files'
import objectPath from 'object-path'
import tree from './tree'
_An extractable file in React Native._
const files = extractFiles(tree)
const treePath = objectPath(tree)
> ```js
> import { ReactNativeFile } from 'extract-files'
>
> const file = new ReactNativeFile({
> uri: uriFromCameraRoll,
> name: 'a.jpg',
> type: 'image/jpeg'
> })
> ```
files.forEach(({ path, file }) => treePath.set(path, file))
```
### function extractFiles
`FileList` instances in an original tree become arrays when reassembled.
Reversibly extracts [`File`](https://developer.mozilla.org/docs/web/api/file), [`Blob`](https://developer.mozilla.org/docs/web/api/blob) and [`ReactNativeFile`](#class-reactnativefile) instances, with [object paths](#type-objectpath), from an object tree and replaces them with `null`. [`FileList`](https://developer.mozilla.org/docs/web/api/filelist) instances are treated as [`File`](https://developer.mozilla.org/docs/web/api/file) instance arrays.
## Support
| Parameter | Type | Description |
| :--------- | :----------------------------------------------------------------------------------------- | :------------------------------------------------------------------------ |
| `tree` | [Object](https://developer.mozilla.org/javascript/reference/global_objects/object) | An object tree to extract files from. The tree itself must not be a file. |
| `treePath` | [string](https://developer.mozilla.org/javascript/reference/global_objects/string)? = `''` | Optional object tree path to prefix file object tree paths. |
* Node.js v6.10+, see `package.json` `engines`.
* [Browsers >1% usage](http://browserl.ist/?q=%3E1%25), see `package.json` `browserslist`.
* React Native.
**Returns:** [Array](https://developer.mozilla.org/javascript/reference/global_objects/array)&lt;[ExtractedFile](#type-extractedfile)> — Extracted files or an empty array if the tree is not an enumerable object.
#### Examples
_Extracting files._
> The following:
>
> ```js
> 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](https://developer.mozilla.org/javascript/reference/global_objects/object)
| Property | Type | Description |
| :------- | :-------------------------------------------------------- | :--------------------------------------------------- |
| `path` | [ObjectPath](#type-objectpath) | Object path to the file in the original object tree. |
| `file` | File \| Blob \| [ReactNativeFile](#class-reactnativefile) | The extracted file. |
### type ObjectPath
String notation for the path to a node in an object tree.
**Type:** [String](https://developer.mozilla.org/javascript/reference/global_objects/string)
#### See
- [`object-path` on npm](https://npm.im/object-path).
#### Examples
_Object path is property `a`, array index `0`, object property `b`._
> a.0.b
### type ReactNativeFileSubstitute
A React Native [`File`](https://developer.mozilla.org/docs/web/api/file) substitute for when using [`FormData`](https://developer.mozilla.org/docs/web/api/formdata).
**Type:** [Object](https://developer.mozilla.org/javascript/reference/global_objects/object)
| Property | Type | Description |
| :------- | :---------------------------------------------------------------------------------- | :----------------- |
| `uri` | [String](https://developer.mozilla.org/javascript/reference/global_objects/string) | Filesystem path. |
| `name` | [String](https://developer.mozilla.org/javascript/reference/global_objects/string)? | File name. |
| `type` | [String](https://developer.mozilla.org/javascript/reference/global_objects/string)? | File content type. |
#### See
- [React Native `FormData` polyfill source](https://github.com/facebook/react-native/blob/v0.45.1/Libraries/Network/FormData.js#L34).

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc