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 4.1.0 to 5.0.0

19

changelog.md
# extract-files changelog
## 5.0.0
### Major
- `extractFiles` has a new return signature and no longer mutates the original input, fixing [#8](https://github.com/jaydenseric/extract-files/issues/8). It returns an object with `clone`, a clone of the original input value with files recursively replaced with `null`, and `files`, a `Map` instance keying each extracted file to an array of object paths.
### Minor
- Files can now be used as the root value passed to `extractFiles`.
### Patch
- Updated dev dependencies.
- Updated package description.
- Moved JSDoc type definitions into the index file.
- Manually composed package exports instead of relying on `*`.
- Improved tests.
- Added a paragraph explaining the GraphQL use case to the readme.
## 4.1.0

@@ -4,0 +23,0 @@

71

lib/extractFiles.js

@@ -6,40 +6,53 @@ 'use strict'

var _isObject = require('./isObject')
var _ReactNativeFile = require('./ReactNativeFile')
function extractFiles(tree, treePath) {
if (treePath === void 0) {
treePath = ''
function extractFiles(value, path) {
if (path === void 0) {
path = ''
}
var files = []
var clone
var files = new Map()
var recurse = function recurse(node, nodePath) {
Object.keys(node).forEach(function(key) {
if (!(0, _isObject.isObject)(node[key])) return
var path = '' + nodePath + key
function addFile(paths, file) {
var storedPaths = files.get(file)
if (storedPaths) storedPaths.push.apply(storedPaths, paths)
else files.set(file, paths)
}
if (
(typeof File !== 'undefined' && node[key] instanceof File) ||
(typeof Blob !== 'undefined' && node[key] instanceof Blob) ||
node[key] instanceof _ReactNativeFile.ReactNativeFile
) {
files.push({
path: path,
file: node[key]
})
node[key] = null
return
if (
(typeof File !== 'undefined' && value instanceof File) ||
(typeof Blob !== 'undefined' && value instanceof Blob) ||
value instanceof _ReactNativeFile.ReactNativeFile
) {
clone = null
addFile([path], value)
} else {
var prefix = path ? path + '.' : ''
if (typeof FileList !== 'undefined' && value instanceof FileList)
clone = Array.prototype.map.call(value, function(file, i) {
addFile(['' + prefix + i], file)
return null
})
else if (Array.isArray(value))
clone = value.map(function(child, i) {
var result = extractFiles(child, '' + prefix + i)
result.files.forEach(addFile)
return result.clone
})
else if (value && typeof value == 'object') {
clone = {}
for (var i in value) {
var result = extractFiles(value[i], '' + prefix + i)
result.files.forEach(addFile)
clone[i] = result.clone
}
} else clone = value
}
if (typeof FileList !== 'undefined' && node[key] instanceof FileList)
node[key] = Array.prototype.slice.call(node[key])
recurse(node[key], path + '.')
})
return {
clone: clone,
files: files
}
if ((0, _isObject.isObject)(tree))
recurse(tree, treePath === '' ? treePath : treePath + '.')
return files
}
'use strict'
exports.__esModule = true
exports.ReactNativeFile = exports.extractFiles = void 0
var _extractFiles = require('./extractFiles')
Object.keys(_extractFiles).forEach(function(key) {
if (key === 'default' || key === '__esModule') return
exports[key] = _extractFiles[key]
})
exports.extractFiles = _extractFiles.extractFiles
var _ReactNativeFile = require('./ReactNativeFile')
Object.keys(_ReactNativeFile).forEach(function(key) {
if (key === 'default' || key === '__esModule') return
exports[key] = _ReactNativeFile[key]
})
exports.ReactNativeFile = _ReactNativeFile.ReactNativeFile
{
"name": "extract-files",
"version": "4.1.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.",
"version": "5.0.0",
"description": "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.",
"license": "MIT",

@@ -37,20 +37,21 @@ "author": {

"devDependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.2.1",
"@babel/preset-env": "^7.2.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.8.0",
"eslint-config-env": "^1.2.1",
"eslint-config-prettier": "^3.1.0",
"eslint": "^5.10.0",
"eslint-config-env": "^2.0.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-import-order-alphabetical": "0.0.1",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-prettier": "^3.0.0",
"husky": "^1.1.2",
"husky": "^1.2.1",
"if-ver": "^1.0.6",
"jsdoc-md": "^1.6.0",
"lint-staged": "^8.0.4",
"prettier": "^1.14.3",
"jsdoc-md": "^1.7.0",
"lint-staged": "^8.1.0",
"prettier": "^1.15.3",
"size-limit": "^0.21.0",
"tap": "^12.0.1"
"tap": "^12.1.1"
},

@@ -57,0 +58,0 @@ "scripts": {

@@ -5,4 +5,6 @@ # extract-files

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.
Clones a value, recursively extracting [`File`](https://developer.mozilla.org/docs/web/api/file), [`Blob`](https://developer.mozilla.org/docs/web/api/blob) and [`ReactNativeFile`](#class-reactnativefile) instances with their [object paths](#type-objectpath), replacing 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.
Used by [GraphQL multipart request spec client implementations](https://github.com/jaydenseric/graphql-multipart-request-spec#implementations) such as [`graphql-react`](https://npm.im/graphql-react) and [`apollo-upload-client`](https://npm.im/apollo-upload-client).
## Usage

@@ -18,19 +20,2 @@

### Reassembly
Loop and reinsert the extracted files using [`object-path`](https://npm.im/object-path):
```js
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`](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.
## Support

@@ -50,3 +35,4 @@

- [Examples](#examples-1)
- [type ExtractedFile](#type-extractedfile)
- [type ExtractableFile](#type-extractablefile)
- [type ExtractFilesResult](#type-extractfilesresult)
- [type ObjectPath](#type-objectpath)

@@ -83,16 +69,16 @@ - [See](#see)

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.
Clones a value, recursively extracting [`File`](https://developer.mozilla.org/docs/web/api/file), [`Blob`](https://developer.mozilla.org/docs/web/api/blob) and [`ReactNativeFile`](#class-reactnativefile) instances with their [object paths](#type-objectpath), replacing 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.
| Parameter | Type | Description |
| :--------- | :-------------------------------------- | :------------------------------------------------------------------------ |
| `tree` | [Object](https://mdn.io/object) | An object tree to extract files from. The tree itself must not be a file. |
| `treePath` | [string](https://mdn.io/string)? = `''` | Optional object tree path to prefix file object tree paths. |
| Parameter | Type | Description |
| :-------- | :------------------------------------- | :------------------------------------------------------ |
| `value` | \* | Value (typically an object tree) to extract files from. |
| `path` | [ObjectPath](#type-objectpath)? = `''` | Prefix for object paths for extracted files. |
**Returns:** [Array](https://mdn.io/array)<[ExtractedFile](#type-extractedfile)> — Extracted files or an empty array if the tree is not an enumerable object.
**Returns:** [ExtractFilesResult](#type-extractfilesresult) — Result.
#### Examples
_Extracting files._
_Extract files from an object._
> The following:
> For the following:
>

@@ -102,37 +88,46 @@ > ```js

>
> console.log(
> extractFiles(
> {
> a: new File(['a'], 'a.txt', { type: 'text/plain' }),
> b: [
> {
> c: new File(['b'], 'b.txt', { type: 'text/plain' })
> }
> ]
> },
> 'prefix'
> )
> )
> 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')
> ```
>
> Logs:
> `value` remains the same.
>
> [{
> path: 'prefix.a',
> file: [object File]
> }, {
> path: 'prefix.b.0.c',
> file: [object File]
> }]
> `clone` is:
>
> ```js
> {
> a: null,
> b: [null, null]
> }
> ```
>
> `files` is a [`Map`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map) instance containing:
>
> | Key | Value |
> | :------ | :--------------------------- |
> | `file1` | `['prefix.a', 'prefix.b.0']` |
> | `file2` | `['prefix.b.1']` |
### type ExtractedFile
### type ExtractableFile
An extracted file.
An extractable file.
**Type:** File | Blob | [ReactNativeFile](#class-reactnativefile)
### type ExtractFilesResult
What [`extractFiles`](#function-extractfiles) returns.
**Type:** [Object](https://mdn.io/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. |
| Property | Type | Description |
| :------- | :----------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------- |
| `clone` | \* | Clone of the original input value with files recursively replaced with `null`. |
| `files` | Map<[ExtractableFile](#type-extractablefile), [Array](https://mdn.io/array)<[ObjectPath](#type-objectpath)>> | Extracted files and their locations within the original value. |

@@ -139,0 +134,0 @@ ### type ObjectPath

Sorry, the diff of this file is not supported yet

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