Skip to content

Commit

Permalink
Bundle up support checks into a single object
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-andrews committed Jan 24, 2015
1 parent 0ca546c commit 84d4324
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,29 @@
return fileReaderReady(reader)
}

var blobSupport = 'FileReader' in self && 'Blob' in self && (function() {
try {
new Blob();
return true
} catch(e) {
return false
}
})();
var formDataSupport = 'FormData' in self;
var support = {
blob: 'FileReader' in self && 'Blob' in self && (function() {
try {
new Blob();
return true
} catch(e) {
return false
}
})(),
formData: 'FormData' in self
}

function Body() {
this.bodyUsed = false

if (blobSupport) {
if (support.blob) {
this._initBody = function(body) {
this._bodyInit = body
if (typeof body === 'string') {
this._bodyText = body
} else if (blobSupport && Blob.prototype.isPrototypeOf(body)) {
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
this._bodyBlob = body
} else if (formDataSupport && FormData.prototype.isPrototypeOf(body)) {
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
this._bodyFormData = body
} else if (!body) {
this._bodyText = ''
Expand Down Expand Up @@ -159,7 +161,7 @@
this._bodyInit = body
if (typeof body === 'string') {
this._bodyText = body
} else if (formDataSupport && FormData.prototype.isPrototypeOf(body)) {
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
this._bodyFormData = body
} else if (!body) {
this._bodyText = ''
Expand All @@ -174,7 +176,7 @@
}
}

if (formDataSupport) {
if (support.formData) {
this.formData = function() {
return this.text().then(decode)
}
Expand Down Expand Up @@ -279,7 +281,7 @@
}

xhr.open(self.method, self.url, true)
if ('responseType' in xhr && blobSupport) {
if ('responseType' in xhr && support.blob) {
xhr.responseType = 'blob'
}

Expand Down

0 comments on commit 84d4324

Please sign in to comment.