Standardjs format

This commit is contained in:
Jimmy Wärting
2019-04-17 19:47:12 +02:00
parent 20316adffb
commit a8d8821e81
+143 -150
View File
@@ -8,8 +8,7 @@
* See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md * See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md
*/ */
;(function(){ ;(function () {
var global = typeof window === 'object' var global = typeof window === 'object'
? window : typeof self === 'object' ? window : typeof self === 'object'
? self : this ? self : this
@@ -17,9 +16,9 @@
var BlobBuilder = global.BlobBuilder var BlobBuilder = global.BlobBuilder
|| global.WebKitBlobBuilder || global.WebKitBlobBuilder
|| global.MSBlobBuilder || global.MSBlobBuilder
|| global.MozBlobBuilder; || global.MozBlobBuilder
global.URL = global.URL || global.webkitURL || function(href, a) { global.URL = global.URL || global.webkitURL || function (href, a) {
a = document.createElement('a') a = document.createElement('a')
a.href = href a.href = href
return a return a
@@ -34,7 +33,7 @@
var arrayBufferSupported = !!global.ArrayBuffer var arrayBufferSupported = !!global.ArrayBuffer
var blobBuilderSupported = BlobBuilder var blobBuilderSupported = BlobBuilder
&& BlobBuilder.prototype.append && BlobBuilder.prototype.append
&& BlobBuilder.prototype.getBlob; && BlobBuilder.prototype.getBlob
try { try {
// Check if Blob constructor is supported // Check if Blob constructor is supported
@@ -42,151 +41,149 @@
// Check if Blob constructor supports ArrayBufferViews // Check if Blob constructor supports ArrayBufferViews
// Fails in Safari 6, so we need to map to ArrayBuffers there. // Fails in Safari 6, so we need to map to ArrayBuffers there.
blobSupportsArrayBufferView = new Blob([new Uint8Array([1,2])]).size === 2 blobSupportsArrayBufferView = new Blob([new Uint8Array([1, 2])]).size === 2
} catch(e) {} } catch (e) {}
/** /**
* Helper function that maps ArrayBufferViews to ArrayBuffers * Helper function that maps ArrayBufferViews to ArrayBuffers
* Used by BlobBuilder constructor and old browsers that didn't * Used by BlobBuilder constructor and old browsers that didn't
* support it in the Blob constructor. * support it in the Blob constructor.
*/ */
function mapArrayBufferViews(ary) { function mapArrayBufferViews (ary) {
return ary.map(function(chunk) { return ary.map(function (chunk) {
if (chunk.buffer instanceof ArrayBuffer) { if (chunk.buffer instanceof ArrayBuffer) {
var buf = chunk.buffer; var buf = chunk.buffer
// if this is a subarray, make a copy so we only // if this is a subarray, make a copy so we only
// include the subarray region from the underlying buffer // include the subarray region from the underlying buffer
if (chunk.byteLength !== buf.byteLength) { if (chunk.byteLength !== buf.byteLength) {
var copy = new Uint8Array(chunk.byteLength); var copy = new Uint8Array(chunk.byteLength)
copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength)); copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength))
buf = copy.buffer; buf = copy.buffer
} }
return buf; return buf
} }
return chunk; return chunk
}); })
} }
function BlobBuilderConstructor(ary, options) { function BlobBuilderConstructor (ary, options) {
options = options || {}; options = options || {}
var bb = new BlobBuilder(); var bb = new BlobBuilder()
mapArrayBufferViews(ary).forEach(function(part) { mapArrayBufferViews(ary).forEach(function (part) {
bb.append(part); bb.append(part)
}); })
return options.type ? bb.getBlob(options.type) : bb.getBlob(); return options.type ? bb.getBlob(options.type) : bb.getBlob()
}; }
function BlobConstructor(ary, options) { function BlobConstructor (ary, options) {
return new origBlob(mapArrayBufferViews(ary), options || {}); return new origBlob(mapArrayBufferViews(ary), options || {})
}; }
if (global.Blob) { if (global.Blob) {
BlobBuilderConstructor.prototype = Blob.prototype; BlobBuilderConstructor.prototype = Blob.prototype
BlobConstructor.prototype = Blob.prototype; BlobConstructor.prototype = Blob.prototype
} }
function FakeBlobBuilder() { function FakeBlobBuilder () {
function toUTF8Array(str) { function toUTF8Array (str) {
var utf8 = []; var utf8 = []
for (var i=0; i < str.length; i++) { for (var i = 0; i < str.length; i++) {
var charcode = str.charCodeAt(i); var charcode = str.charCodeAt(i)
if (charcode < 0x80) utf8.push(charcode); if (charcode < 0x80) utf8.push(charcode)
else if (charcode < 0x800) { else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6), utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f)); 0x80 | (charcode & 0x3f))
} } else if (charcode < 0xd800 || charcode >= 0xe000) {
else if (charcode < 0xd800 || charcode >= 0xe000) { utf8.push(0xe0 | (charcode >> 12),
utf8.push(0xe0 | (charcode >> 12), 0x80 | ((charcode >> 6) & 0x3f),
0x80 | ((charcode>>6) & 0x3f), 0x80 | (charcode & 0x3f))
0x80 | (charcode & 0x3f));
} }
// surrogate pair // surrogate pair
else { else {
i++; i++
// UTF-16 encodes 0x10000-0x10FFFF by // UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the // subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves // 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff)<<10) charcode = 0x10000 + (((charcode & 0x3ff) << 10)
| (str.charCodeAt(i) & 0x3ff)); | (str.charCodeAt(i) & 0x3ff))
utf8.push(0xf0 | (charcode >>18), utf8.push(0xf0 | (charcode >> 18),
0x80 | ((charcode>>12) & 0x3f), 0x80 | ((charcode >> 12) & 0x3f),
0x80 | ((charcode>>6) & 0x3f), 0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f)); 0x80 | (charcode & 0x3f))
} }
} }
return utf8; return utf8
} }
function fromUtf8Array(array) { function fromUtf8Array (array) {
var out, i, len, c; var out, i, len, c
var char2, char3; var char2, char3
out = ""; out = ''
len = array.length; len = array.length
i = 0; i = 0
while (i < len) { while (i < len) {
c = array[i++]; c = array[i++]
switch (c >> 4) switch (c >> 4) {
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx // 0xxxxxxx
out += String.fromCharCode(c); out += String.fromCharCode(c)
break; break
case 12: case 13: case 12: case 13:
// 110x xxxx 10xx xxxx // 110x xxxx 10xx xxxx
char2 = array[i++]; char2 = array[i++]
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F))
break; break
case 14: case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx // 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++]; char2 = array[i++]
char3 = array[i++]; char3 = array[i++]
out += String.fromCharCode(((c & 0x0F) << 12) | out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) | ((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0)); ((char3 & 0x3F) << 0))
break; break
} }
} }
return out; return out
} }
function isDataView(obj) { function isDataView (obj) {
return obj && DataView.prototype.isPrototypeOf(obj) return obj && DataView.prototype.isPrototypeOf(obj)
} }
function bufferClone(buf) { function bufferClone (buf) {
var view = new Array(buf.byteLength) var view = new Array(buf.byteLength)
var array = new Uint8Array(buf) var array = new Uint8Array(buf)
var i = view.length var i = view.length
while(i--) { while (i--) {
view[i] = array[i] view[i] = array[i]
} }
return view return view
} }
function encodeByteArray(input) { function encodeByteArray (input) {
var byteToCharMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var byteToCharMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
var output = []; var output = []
for (var i = 0; i < input.length; i += 3) { for (var i = 0; i < input.length; i += 3) {
var byte1 = input[i]; var byte1 = input[i]
var haveByte2 = i + 1 < input.length; var haveByte2 = i + 1 < input.length
var byte2 = haveByte2 ? input[i + 1] : 0; var byte2 = haveByte2 ? input[i + 1] : 0
var haveByte3 = i + 2 < input.length; var haveByte3 = i + 2 < input.length
var byte3 = haveByte3 ? input[i + 2] : 0; var byte3 = haveByte3 ? input[i + 2] : 0
var outByte1 = byte1 >> 2; var outByte1 = byte1 >> 2
var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4); var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4)
var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6); var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6)
var outByte4 = byte3 & 0x3F; var outByte4 = byte3 & 0x3F
if (!haveByte3) { if (!haveByte3) {
outByte4 = 64; outByte4 = 64
if (!haveByte2) { if (!haveByte2) {
outByte3 = 64; outByte3 = 64
} }
} }
@@ -199,11 +196,11 @@
} }
var create = Object.create || function (a) { var create = Object.create || function (a) {
function c() {} function c () {}
c.prototype = a; c.prototype = a
return new c return new c()
} }
if (arrayBufferSupported) { if (arrayBufferSupported) {
var viewClasses = [ var viewClasses = [
'[object Int8Array]', '[object Int8Array]',
@@ -217,17 +214,15 @@
'[object Float64Array]' '[object Float64Array]'
] ]
var isArrayBufferView = ArrayBuffer.isView || function(obj) { var isArrayBufferView = ArrayBuffer.isView || function (obj) {
return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1 return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
} }
} }
/********************************************************/ /********************************************************/
/* Blob constructor */ /* Blob constructor */
/********************************************************/ /********************************************************/
function Blob(chunks, opts) { function Blob (chunks, opts) {
chunks = chunks || [] chunks = chunks || []
for (var i = 0, len = chunks.length; i < len; i++) { for (var i = 0, len = chunks.length; i < len; i++) {
var chunk = chunks[i] var chunk = chunks[i]
@@ -249,54 +244,52 @@
this.type = opts ? opts.type || '' : '' this.type = opts ? opts.type || '' : ''
} }
Blob.prototype.slice = function(start, end, type) { Blob.prototype.slice = function (start, end, type) {
var slice = this._buffer.slice(start || 0, end || this._buffer.length) var slice = this._buffer.slice(start || 0, end || this._buffer.length)
return new Blob([slice], {type: type}) return new Blob([slice], {type: type})
} }
Blob.prototype.toString = function() { Blob.prototype.toString = function () {
return '[object Blob]' return '[object Blob]'
} }
/********************************************************/ /********************************************************/
/* File constructor */ /* File constructor */
/********************************************************/ /********************************************************/
function File(chunks, name, opts) { function File (chunks, name, opts) {
opts = opts || {} opts = opts || {}
var a = Blob.call(this, chunks, opts) || this var a = Blob.call(this, chunks, opts) || this
a.name = name a.name = name
a.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date a.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date()
a.lastModified = +a.lastModifiedDate a.lastModified = +a.lastModifiedDate
return a return a
} }
File.prototype = create(Blob.prototype); File.prototype = create(Blob.prototype)
File.prototype.constructor = File; File.prototype.constructor = File
if (Object.setPrototypeOf) if (Object.setPrototypeOf) {
Object.setPrototypeOf(File, Blob); Object.setPrototypeOf(File, Blob)
else { } else {
try {File.__proto__ = Blob} catch (e) {} try { File.__proto__ = Blob } catch (e) {}
} }
File.prototype.toString = function() { File.prototype.toString = function () {
return '[object File]' return '[object File]'
} }
/********************************************************/ /********************************************************/
/* FileReader constructor */ /* FileReader constructor */
/********************************************************/ /********************************************************/
function FileReader() { function FileReader () {
if (!(this instanceof FileReader)) if (!(this instanceof FileReader)) {
throw new TypeError("Failed to construct 'FileReader': Please use the 'new' operator, this DOM object constructor cannot be called as a function.") throw new TypeError("Failed to construct 'FileReader': Please use the 'new' operator, this DOM object constructor cannot be called as a function.")
}
var delegate = document.createDocumentFragment() var delegate = document.createDocumentFragment()
this.addEventListener = delegate.addEventListener this.addEventListener = delegate.addEventListener
this.dispatchEvent = function(evt) { this.dispatchEvent = function (evt) {
var local = this['on' + evt.type] var local = this['on' + evt.type]
if (typeof local === 'function') local(evt) if (typeof local === 'function') local(evt)
delegate.dispatchEvent(evt) delegate.dispatchEvent(evt)
@@ -304,13 +297,14 @@
this.removeEventListener = delegate.removeEventListener this.removeEventListener = delegate.removeEventListener
} }
function _read(fr, blob, kind) { function _read (fr, blob, kind) {
if (!(blob instanceof Blob)) if (!(blob instanceof Blob)) {
throw new TypeError("Failed to execute '" + kind + "' on 'FileReader': parameter 1 is not of type 'Blob'.") throw new TypeError("Failed to execute '" + kind + "' on 'FileReader': parameter 1 is not of type 'Blob'.")
}
fr.result = '' fr.result = ''
setTimeout(function(){ setTimeout(function () {
this.readyState = FileReader.LOADING this.readyState = FileReader.LOADING
fr.dispatchEvent(new Event('load')) fr.dispatchEvent(new Event('load'))
fr.dispatchEvent(new Event('loadend')) fr.dispatchEvent(new Event('loadend'))
@@ -328,34 +322,33 @@
FileReader.prototype.onloadstart = null FileReader.prototype.onloadstart = null
FileReader.prototype.onprogress = null FileReader.prototype.onprogress = null
FileReader.prototype.readAsDataURL = function(blob) { FileReader.prototype.readAsDataURL = function (blob) {
_read(this, blob, 'readAsDataURL') _read(this, blob, 'readAsDataURL')
this.result = 'data:' + blob.type + ';base64,' + encodeByteArray(blob._buffer) this.result = 'data:' + blob.type + ';base64,' + encodeByteArray(blob._buffer)
} }
FileReader.prototype.readAsText = function(blob) { FileReader.prototype.readAsText = function (blob) {
_read(this, blob, 'readAsText') _read(this, blob, 'readAsText')
this.result = fromUtf8Array(blob._buffer) this.result = fromUtf8Array(blob._buffer)
} }
FileReader.prototype.readAsArrayBuffer = function(blob) { FileReader.prototype.readAsArrayBuffer = function (blob) {
_read(this, blob, 'readAsText') _read(this, blob, 'readAsText')
this.result = blob._buffer.slice() this.result = blob._buffer.slice()
} }
FileReader.prototype.abort = function() {} FileReader.prototype.abort = function () {}
/********************************************************/ /********************************************************/
/* URL */ /* URL */
/********************************************************/ /********************************************************/
URL.createObjectURL = function(blob) { URL.createObjectURL = function (blob) {
return blob instanceof Blob return blob instanceof Blob
? 'data:' + blob.type + ';base64,' + encodeByteArray(blob._buffer) ? 'data:' + blob.type + ';base64,' + encodeByteArray(blob._buffer)
: createObjectURL.call(URL, blob) : createObjectURL.call(URL, blob)
} }
URL.revokeObjectURL = function(url) { URL.revokeObjectURL = function (url) {
revokeObjectURL && revokeObjectURL.call(URL, url) revokeObjectURL && revokeObjectURL.call(URL, url)
} }
@@ -364,7 +357,7 @@
/********************************************************/ /********************************************************/
var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send
if (_send) { if (_send) {
XMLHttpRequest.prototype.send = function(data) { XMLHttpRequest.prototype.send = function (data) {
if (data instanceof Blob) { if (data instanceof Blob) {
this.setRequestHeader('Content-Type', data.type) this.setRequestHeader('Content-Type', data.type)
_send.call(this, fromUtf8Array(data._buffer)) _send.call(this, fromUtf8Array(data._buffer))
@@ -373,7 +366,7 @@
} }
} }
} }
global.FileReader = FileReader global.FileReader = FileReader
global.File = File global.File = File
global.Blob = Blob global.Blob = Blob
@@ -385,18 +378,18 @@
FileReader.prototype[strTag] = 'FileReader' FileReader.prototype[strTag] = 'FileReader'
} }
function fixFileAndXHR() { function fixFileAndXHR () {
var isIE = !!global.ActiveXObject || ( var isIE = !!global.ActiveXObject || (
'-ms-scroll-limit' in document.documentElement.style && '-ms-scroll-limit' in document.documentElement.style &&
'-ms-ime-align' in document.documentElement.style '-ms-ime-align' in document.documentElement.style
) )
// Monkey patched // Monkey patched
// IE don't set Content-Type header on XHR whose body is a typed Blob // IE don't set Content-Type header on XHR whose body is a typed Blob
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/6047383 // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/6047383
var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send
if (isIE && _send) { if (isIE && _send) {
XMLHttpRequest.prototype.send = function(data) { XMLHttpRequest.prototype.send = function (data) {
if (data instanceof Blob) { if (data instanceof Blob) {
this.setRequestHeader('Content-Type', data.type) this.setRequestHeader('Content-Type', data.type)
_send.call(this, data) _send.call(this, data)
@@ -408,34 +401,35 @@
try { try {
new File([], '') new File([], '')
} catch(e) { } catch (e) {
try { try {
var klass = new Function('class File extends Blob {' + var klass = new Function('class File extends Blob {' +
'constructor(chunks, name, opts) {' + 'constructor(chunks, name, opts) {' +
'opts = opts || {};' + 'opts = opts || {};' +
'super(chunks, opts || {});' + 'super(chunks, opts || {});' +
'this.name = name;' + 'this.name = name;' +
'this.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date;' + 'this.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date();' +
'this.lastModified = +this.lastModifiedDate;' + 'this.lastModified = +this.lastModifiedDate;' +
'}};' + '}};' +
'return new File([], ""), File' 'return new File([], ""), File'
)() )()
global.File = klass global.File = klass
} catch(e) { } catch (e) {
var klass = function(b, d, c) { var klass = function (b, d, c) {
var blob = new Blob(b, c) var blob = new Blob(b, c)
var t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date var t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date()
blob.name = d blob.name = d
blob.lastModifiedDate = t blob.lastModifiedDate = t
blob.lastModified = +t blob.lastModified = +t
blob.toString = function() { blob.toString = function () {
return '[object File]' return '[object File]'
} }
if (strTag) if (strTag) {
blob[strTag] = 'File' blob[strTag] = 'File'
}
return blob return blob
} }
global.File = klass global.File = klass
@@ -448,9 +442,8 @@
global.Blob = blobSupportsArrayBufferView ? global.Blob : BlobConstructor global.Blob = blobSupportsArrayBufferView ? global.Blob : BlobConstructor
} else if (blobBuilderSupported) { } else if (blobBuilderSupported) {
fixFileAndXHR() fixFileAndXHR()
global.Blob = BlobBuilderConstructor; global.Blob = BlobBuilderConstructor
} else { } else {
FakeBlobBuilder() FakeBlobBuilder()
} }
})()
})();