continue on typescript

This commit is contained in:
Nolan Lawson 2018-05-21 12:56:57 -07:00
parent da31976065
commit e76eed3c6c
3 changed files with 78 additions and 72 deletions

View File

@ -4,6 +4,7 @@
"description": "Utilities for working with Blob objects in the browser", "description": "Utilities for working with Blob objects in the browser",
"main": "dist/blob-util.cjs.js", "main": "dist/blob-util.cjs.js",
"module": "dist/blob-util.es.js", "module": "dist/blob-util.es.js",
"types": "./dist/blob-util.d.ts",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/nolanlawson/blob-util.git" "url": "git://github.com/nolanlawson/blob-util.git"
@ -21,13 +22,13 @@
"url": "https://github.com/nolanlawson/blob-util/issues" "url": "https://github.com/nolanlawson/blob-util/issues"
}, },
"scripts": { "scripts": {
"lint": "standard test/*js && tslint -c .tslint.json src/*ts", "lint": "standard test/*js && tslint src/*ts",
"test": "npm run build && npm run lint && zuul --no-coverage ./test/test.js", "test": "npm run build && npm run lint && zuul --no-coverage ./test/test.js",
"test-local": "npm run build && zuul ./test/test.js --local 9000 --no-coverage", "test-local": "npm run build && zuul ./test/test.js --local 9000 --no-coverage",
"clean": "rimraf dist lib && mkdirp dist lib", "clean": "rimraf dist lib && mkdirp dist lib",
"build": "run-s clean build-ts build-js min", "build": "run-s clean build-ts build-js min",
"build-ts": "tsc src/blob-util.ts --target ES6 --module es2015 --outDir lib -d && cp lib/blob-util.d.ts dist/", "build-ts": "tsc src/blob-util.ts --target ES6 --module es2015 --outDir lib -d && cpy lib/blob-util.d.ts dist/",
"build-js": "run-p build-es build-cjs build-umd", "build-js": "run-p build-es build-cjs build-umd && rimraf lib",
"build-es": "rollup -i lib/blob-util.js -f es -o dist/blob-util.es.js", "build-es": "rollup -i lib/blob-util.js -f es -o dist/blob-util.es.js",
"build-cjs": "rollup -i lib/blob-util.js -f cjs -o dist/blob-util.cjs.js", "build-cjs": "rollup -i lib/blob-util.js -f cjs -o dist/blob-util.cjs.js",
"build-umd": "rollup -i lib/blob-util.js -f umd -n blobUtil -o dist/blob-util.js", "build-umd": "rollup -i lib/blob-util.js -f umd -n blobUtil -o dist/blob-util.js",
@ -37,6 +38,7 @@
"bundle-collapser": "^1.1.4", "bundle-collapser": "^1.1.4",
"chai": "~1.8.1", "chai": "~1.8.1",
"chai-as-promised": "~4.1.0", "chai-as-promised": "~4.1.0",
"cpy-cli": "^1.0.1",
"istanbul": "^0.2.7", "istanbul": "^0.2.7",
"mkdirp": "^0.5.0", "mkdirp": "^0.5.0",
"mocha": "~1.18", "mocha": "~1.18",

View File

@ -1,7 +1,14 @@
/* global Promise, Image, Blob, FileReader, atob, btoa */ /* global Promise, Image, Blob, FileReader, atob, btoa,
BlobBuilder, MSBlobBuilder, MozBlobBuilder, WebKitBlobBuilder, webkitURL */
/* exported createObjectURL, revokeObjectURL, binaryStringToBlob, blobToDataURL, /* exported createObjectURL, revokeObjectURL, binaryStringToBlob, blobToDataURL,
imgSrcToDataURL, imgSrcToBlob, arrayBufferToBlob, blobToArrayBuffer */ imgSrcToDataURL, imgSrcToBlob, arrayBufferToBlob, blobToArrayBuffer */
declare var BlobBuilder: any
declare var MozBlobBuilder: any
declare var MSBlobBuilder: any
declare var WebKitBlobBuilder: any
declare var webkitURL: any
function loadImage (src, crossOrigin) { function loadImage (src, crossOrigin) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var img = new Image() var img = new Image()
@ -36,10 +43,10 @@ function imgToCanvas (img) {
/** /**
* Convert a binary string to an <code>ArrayBuffer</code>. * Convert a binary string to an <code>ArrayBuffer</code>.
* @param {string} binary - binary string * @param binary - binary string
* @returns {ArrayBuffer} * @returns array buffer
*/ */
export function binaryStringToArrayBuffer (binary) { export function binaryStringToArrayBuffer (binary: string): ArrayBuffer {
var length = binary.length var length = binary.length
var buf = new ArrayBuffer(length) var buf = new ArrayBuffer(length)
var arr = new Uint8Array(buf) var arr = new Uint8Array(buf)
@ -52,10 +59,10 @@ export function binaryStringToArrayBuffer (binary) {
/** /**
* Convert an <code>ArrayBuffer</code> to a binary string. * Convert an <code>ArrayBuffer</code> to a binary string.
* @param {ArrayBuffer} buffer - array buffer * @param buffer - array buffer
* @returns {string} * @returns binary string
*/ */
export function arrayBufferToBinaryString (buffer) { export function arrayBufferToBinaryString (buffer: ArrayBuffer): string {
var binary = '' var binary = ''
var bytes = new Uint8Array(buffer) var bytes = new Uint8Array(buffer)
var length = bytes.byteLength var length = bytes.byteLength
@ -68,17 +75,16 @@ export function arrayBufferToBinaryString (buffer) {
/** /**
* Shim for * Shim for
* [new Blob()]{@link https://developer.mozilla.org/en-US/docs/Web/API/Blob.Blob} * {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob.Blob | <code>new Blob()</code>}
* to support * to support
* [older browsers that use the deprecated <code>BlobBuilder</code> API]{@link http://caniuse.com/blob}. * {@link http://caniuse.com/blob | older browsers that use the deprecated <code>BlobBuilder</code> API}.
* *
* @param {Array} parts - content of the <code>Blob</code> * @param parts - content of the <code>Blob</code>
* @param {Object} options - usually <code>{type: myContentType}</code>, * @param properties - usually <code>{type: myContentType}</code>,
* you can also pass a string for the content type * you can also pass a string for the content type
* @returns {Blob} * @returns Blob
*/ */
export function createBlob (parts, properties) { export function createBlob (parts: Array<any>, properties?: BlobPropertyBag | string): Blob {
/* global BlobBuilder,MSBlobBuilder,MozBlobBuilder,WebKitBlobBuilder */
parts = parts || [] parts = parts || []
properties = properties || {} properties = properties || {}
if (typeof properties === 'string') { if (typeof properties === 'string') {
@ -92,8 +98,8 @@ export function createBlob (parts, properties) {
} }
var Builder = typeof BlobBuilder !== 'undefined' var Builder = typeof BlobBuilder !== 'undefined'
? BlobBuilder : typeof MSBlobBuilder !== 'undefined' ? BlobBuilder : typeof MSBlobBuilder !== 'undefined'
? MSBlobBuilder : typeof MozBlobBuilder !== 'undefined' ? MSBlobBuilder : typeof MozBlobBuilder !== 'undefined'
? MozBlobBuilder : WebKitBlobBuilder ? MozBlobBuilder : WebKitBlobBuilder
var builder = new Builder() var builder = new Builder()
for (var i = 0; i < parts.length; i += 1) { for (var i = 0; i < parts.length; i += 1) {
builder.append(parts[i]) builder.append(parts[i])
@ -104,36 +110,34 @@ export function createBlob (parts, properties) {
/** /**
* Shim for * Shim for
* [URL.createObjectURL()]{@link https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL} * {@link https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL | <code>URL.createObjectURL()</code>}
* to support browsers that only have the prefixed * to support browsers that only have the prefixed
* <code>webkitURL</code> (e.g. Android <4.4). * <code>webkitURL</code> (e.g. Android <4.4).
* @param {Blob} blob * @param blob
* @returns {string} url * @returns url
*/ */
export function createObjectURL (blob) { export function createObjectURL (blob: Blob): string {
/* global webkitURL */
return (typeof URL !== 'undefined' ? URL : webkitURL).createObjectURL(blob) return (typeof URL !== 'undefined' ? URL : webkitURL).createObjectURL(blob)
} }
/** /**
* Shim for * Shim for
* [URL.revokeObjectURL()]{@link https://developer.mozilla.org/en-US/docs/Web/API/URL.revokeObjectURL} * {@link https://developer.mozilla.org/en-US/docs/Web/API/URL.revokeObjectURL | <code>URL.revokeObjectURL()</code>}
* to support browsers that only have the prefixed * to support browsers that only have the prefixed
* <code>webkitURL</code> (e.g. Android <4.4). * <code>webkitURL</code> (e.g. Android <4.4).
* @param {string} url * @param url
*/ */
export function revokeObjectURL (url) { export function revokeObjectURL (url: string): void {
/* global webkitURL */
return (typeof URL !== 'undefined' ? URL : webkitURL).revokeObjectURL(url) return (typeof URL !== 'undefined' ? URL : webkitURL).revokeObjectURL(url)
} }
/** /**
* Convert a <code>Blob</code> to a binary string. * Convert a <code>Blob</code> to a binary string.
* *
* @param {Blob} blob * @param blob
* @returns {Promise} Promise that resolves with the binary string * @returns Promise that resolves with the binary string
*/ */
export function blobToBinaryString (blob) { export function blobToBinaryString (blob: Blob): Promise<string> {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var reader = new FileReader() var reader = new FileReader()
var hasBinaryString = typeof reader.readAsBinaryString === 'function' var hasBinaryString = typeof reader.readAsBinaryString === 'function'
@ -155,31 +159,31 @@ export function blobToBinaryString (blob) {
/** /**
* Convert a base64-encoded string to a <code>Blob</code>. * Convert a base64-encoded string to a <code>Blob</code>.
* @param {string} base64 * @param base64 - base64-encoded string
* @param {string|undefined} type - the content type (optional) * @param type - the content type (optional)
* @returns {Blob} * @returns Blob
*/ */
export function base64StringToBlob (base64, type) { export function base64StringToBlob (base64: string, type?: string): Blob {
var parts = [binaryStringToArrayBuffer(atob(base64))] var parts = [binaryStringToArrayBuffer(atob(base64))]
return type ? createBlob(parts, { type: type }) : createBlob(parts) return type ? createBlob(parts, { type: type }) : createBlob(parts)
} }
/** /**
* Convert a binary string to a <code>Blob</code>. * Convert a binary string to a <code>Blob</code>.
* @param {string} binary * @param binary - binary string
* @param {string|undefined} type - the content type (optional) * @param type - the content type (optional)
* @returns {Blob} * @returns Blob
*/ */
export function binaryStringToBlob (binary, type) { export function binaryStringToBlob (binary: string, type?: string): Blob {
return base64StringToBlob(btoa(binary), type) return base64StringToBlob(btoa(binary), type)
} }
/** /**
* Convert a <code>Blob</code> to a binary string. * Convert a <code>Blob</code> to a binary string.
* @param {Blob} blob * @param blob
* @returns {Promise} Promise that resolves with the binary string * @returns Promise that resolves with the binary string
*/ */
export function blobToBase64String (blob) { export function blobToBase64String (blob: Blob): Promise<string> {
return blobToBinaryString(blob).then(btoa) return blobToBinaryString(blob).then(btoa)
} }
@ -187,10 +191,10 @@ export function blobToBase64String (blob) {
* Convert a data URL string * Convert a data URL string
* (e.g. <code>'data:image/png;base64,iVBORw0KG...'</code>) * (e.g. <code>'data:image/png;base64,iVBORw0KG...'</code>)
* to a <code>Blob</code>. * to a <code>Blob</code>.
* @param {string} dataURL * @param dataURL - dataURL-encoded string
* @returns {Blob} * @returns Blob
*/ */
export function dataURLToBlob (dataURL) { export function dataURLToBlob (dataURL: string): Blob {
var type = dataURL.match(/data:([^;]+)/)[1] var type = dataURL.match(/data:([^;]+)/)[1]
var base64 = dataURL.replace(/^[^,]+,/, '') var base64 = dataURL.replace(/^[^,]+,/, '')
@ -201,10 +205,10 @@ export function dataURLToBlob (dataURL) {
/** /**
* Convert a <code>Blob</code> to a data URL string * Convert a <code>Blob</code> to a data URL string
* (e.g. <code>'data:image/png;base64,iVBORw0KG...'</code>). * (e.g. <code>'data:image/png;base64,iVBORw0KG...'</code>).
* @param {Blob} blob * @param blob
* @returns {Promise} Promise that resolves with the data URL string * @returns Promise that resolves with the data URL string
*/ */
export function blobToDataURL (blob) { export function blobToDataURL (blob: Blob): Promise<string> {
return blobToBase64String(blob).then(function (base64String) { return blobToBase64String(blob).then(function (base64String) {
return 'data:' + blob.type + ';base64,' + base64String return 'data:' + blob.type + ';base64,' + base64String
}) })
@ -217,15 +221,15 @@ export function blobToDataURL (blob) {
* <p/>Note: this will coerce the image to the desired content type, and it * <p/>Note: this will coerce the image to the desired content type, and it
* will only paint the first frame of an animated GIF. * will only paint the first frame of an animated GIF.
* *
* @param {string} src * @param src - image src
* @param {string|undefined} type - the content type (optional, defaults to 'image/png') * @param type - the content type (optional, defaults to 'image/png')
* @param {string|undefined} crossOrigin - for CORS-enabled images, set this to * @param crossOrigin - for CORS-enabled images, set this to
* 'Anonymous' to avoid "tainted canvas" errors * 'Anonymous' to avoid "tainted canvas" errors
* @param {number|undefined} quality - a number between 0 and 1 indicating image quality * @param quality - a number between 0 and 1 indicating image quality
* if the requested type is 'image/jpeg' or 'image/webp' * if the requested type is 'image/jpeg' or 'image/webp'
* @returns {Promise} Promise that resolves with the data URL string * @returns Promise that resolves with the data URL string
*/ */
export function imgSrcToDataURL (src, type, crossOrigin, quality) { export function imgSrcToDataURL (src: string, type?: string, crossOrigin?: string, quality?: number): Promise<string> {
type = type || 'image/png' type = type || 'image/png'
return loadImage(src, crossOrigin).then(imgToCanvas).then(function (canvas) { return loadImage(src, crossOrigin).then(imgToCanvas).then(function (canvas) {
@ -235,13 +239,13 @@ export function imgSrcToDataURL (src, type, crossOrigin, quality) {
/** /**
* Convert a <code>canvas</code> to a <code>Blob</code>. * Convert a <code>canvas</code> to a <code>Blob</code>.
* @param {string} canvas * @param canvas
* @param {string|undefined} type - the content type (optional, defaults to 'image/png') * @param type - the content type (optional, defaults to 'image/png')
* @param {number|undefined} quality - a number between 0 and 1 indicating image quality * @param quality - a number between 0 and 1 indicating image quality
* if the requested type is 'image/jpeg' or 'image/webp' * if the requested type is 'image/jpeg' or 'image/webp'
* @returns {Promise} Promise that resolves with the <code>Blob</code> * @returns Promise that resolves with the <code>Blob</code>
*/ */
export function canvasToBlob (canvas, type, quality) { export function canvasToBlob (canvas: HTMLCanvasElement, type?: string, quality?: number): Promise<Blob> {
if (typeof canvas.toBlob === 'function') { if (typeof canvas.toBlob === 'function') {
return new Promise(function (resolve) { return new Promise(function (resolve) {
canvas.toBlob(resolve, type, quality) canvas.toBlob(resolve, type, quality)
@ -257,15 +261,15 @@ export function canvasToBlob (canvas, type, quality) {
* <p/>Note: this will coerce the image to the desired content type, and it * <p/>Note: this will coerce the image to the desired content type, and it
* will only paint the first frame of an animated GIF. * will only paint the first frame of an animated GIF.
* *
* @param {string} src * @param src - image src
* @param {string|undefined} type - the content type (optional, defaults to 'image/png') * @param type - the content type (optional, defaults to 'image/png')
* @param {string|undefined} crossOrigin - for CORS-enabled images, set this to * @param crossOrigin - for CORS-enabled images, set this to
* 'Anonymous' to avoid "tainted canvas" errors * 'Anonymous' to avoid "tainted canvas" errors
* @param {number|undefined} quality - a number between 0 and 1 indicating image quality * @param quality - a number between 0 and 1 indicating image quality
* if the requested type is 'image/jpeg' or 'image/webp' * if the requested type is 'image/jpeg' or 'image/webp'
* @returns {Promise} Promise that resolves with the <code>Blob</code> * @returns Promise that resolves with the <code>Blob</code>
*/ */
export function imgSrcToBlob (src, type, crossOrigin, quality) { export function imgSrcToBlob (src: string, type?: string, crossOrigin?: string, quality?: number): Promise<Blob> {
type = type || 'image/png' type = type || 'image/png'
return loadImage(src, crossOrigin).then(imgToCanvas).then(function (canvas) { return loadImage(src, crossOrigin).then(imgToCanvas).then(function (canvas) {
@ -276,20 +280,20 @@ export function imgSrcToBlob (src, type, crossOrigin, quality) {
/** /**
* Convert an <code>ArrayBuffer</code> to a <code>Blob</code>. * Convert an <code>ArrayBuffer</code> to a <code>Blob</code>.
* *
* @param {ArrayBuffer} buffer * @param buffer
* @param {string|undefined} type - the content type (optional) * @param type - the content type (optional)
* @returns {Blob} * @returns Blob
*/ */
export function arrayBufferToBlob (buffer, type) { export function arrayBufferToBlob (buffer: ArrayBuffer, type?: string): Blob {
return createBlob([buffer], type) return createBlob([buffer], type)
} }
/** /**
* Convert a <code>Blob</code> to an <code>ArrayBuffer</code>. * Convert a <code>Blob</code> to an <code>ArrayBuffer</code>.
* @param {Blob} blob * @param blob
* @returns {Promise} Promise that resolves with the <code>ArrayBuffer</code> * @returns Promise that resolves with the <code>ArrayBuffer</code>
*/ */
export function blobToArrayBuffer (blob) { export function blobToArrayBuffer (blob: Blob): Promise<ArrayBuffer> {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var reader = new FileReader() var reader = new FileReader()
reader.onloadend = function (e) { reader.onloadend = function (e) {