Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function isArray (value) {
return Array.isArray(value)
}

function isFile (value) {
return value instanceof File
function isBlob (value) {
return value instanceof Blob
}

function isDate (value) {
Expand All @@ -31,7 +31,7 @@ function objectToFormData (obj, fd, pre) {

objectToFormData(value, fd, key)
})
} else if (isObject(obj) && !isFile(obj) && !isDate(obj)) {
} else if (isObject(obj) && !isBlob(obj) && !isDate(obj)) {
Object.keys(obj).forEach(function (prop) {
var value = obj[prop]

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"standard": {
"globals": [
"FormData",
"File"
"File",
"Blob"
]
}
}
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ global.FormData = class FormData {
}
}
global.File = global.window.File
global.Blob = global.window.Blob

test('undefined', t => {
const formData = objectToFormData({
Expand Down Expand Up @@ -123,6 +124,21 @@ test('File', t => {
t.is(formData.get('foo'), foo)
})

test('Blob', t => {
const foo = new Blob([], {})
const formData = objectToFormData({
foo
})

t.true(formData.append.calledOnce)
t.deepEqual(formData.append.getCall(0).args, [
'foo',
foo
])

t.true(formData.get('foo') instanceof File)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be instanceof Blob? So we don't check for an instance of a sub-prototype?

Copy link
Author

@cretueusebiu cretueusebiu Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you append a Blob instance to FormData and call FormData.get() you'll get an instance of File instead of Blob.

Try this in your console:

const foo = new Blob([], {})
const data = new FormData()
data.append('foo', foo)
data.get('foo') // <- will return a File instance

})

test('Date', t => {
const foo = new Date()
const formData = objectToFormData({
Expand Down