diff --git a/package.json b/package.json index 50ab87d..92ddf39 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Utilities for working with Blob objects in the browser", "main": "dist/blob-util.cjs.js", "module": "dist/blob-util.es.js", + "types": "./dist/blob-util.d.ts", "repository": { "type": "git", "url": "git://github.com/nolanlawson/blob-util.git" @@ -21,13 +22,13 @@ "url": "https://github.com/nolanlawson/blob-util/issues" }, "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-local": "npm run build && zuul ./test/test.js --local 9000 --no-coverage", "clean": "rimraf dist lib && mkdirp dist lib", "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-js": "run-p build-es build-cjs build-umd", + "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 && rimraf lib", "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-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", "chai": "~1.8.1", "chai-as-promised": "~4.1.0", + "cpy-cli": "^1.0.1", "istanbul": "^0.2.7", "mkdirp": "^0.5.0", "mocha": "~1.18", diff --git a/src/blob-util.ts b/src/blob-util.ts index 38093c0..7c0d696 100644 --- a/src/blob-util.ts +++ b/src/blob-util.ts @@ -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, 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) { return new Promise(function (resolve, reject) { var img = new Image() @@ -36,10 +43,10 @@ function imgToCanvas (img) { /** * Convert a binary string to an ArrayBuffer. - * @param {string} binary - binary string - * @returns {ArrayBuffer} + * @param binary - binary string + * @returns array buffer */ -export function binaryStringToArrayBuffer (binary) { +export function binaryStringToArrayBuffer (binary: string): ArrayBuffer { var length = binary.length var buf = new ArrayBuffer(length) var arr = new Uint8Array(buf) @@ -52,10 +59,10 @@ export function binaryStringToArrayBuffer (binary) { /** * Convert an ArrayBuffer to a binary string. - * @param {ArrayBuffer} buffer - array buffer - * @returns {string} + * @param buffer - array buffer + * @returns binary string */ -export function arrayBufferToBinaryString (buffer) { +export function arrayBufferToBinaryString (buffer: ArrayBuffer): string { var binary = '' var bytes = new Uint8Array(buffer) var length = bytes.byteLength @@ -68,17 +75,16 @@ export function arrayBufferToBinaryString (buffer) { /** * 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 | new Blob()} * to support - * [older browsers that use the deprecated BlobBuilder API]{@link http://caniuse.com/blob}. + * {@link http://caniuse.com/blob | older browsers that use the deprecated BlobBuilder API}. * - * @param {Array} parts - content of the Blob - * @param {Object} options - usually {type: myContentType}, + * @param parts - content of the Blob + * @param properties - usually {type: myContentType}, * you can also pass a string for the content type - * @returns {Blob} + * @returns Blob */ -export function createBlob (parts, properties) { - /* global BlobBuilder,MSBlobBuilder,MozBlobBuilder,WebKitBlobBuilder */ +export function createBlob (parts: Array, properties?: BlobPropertyBag | string): Blob { parts = parts || [] properties = properties || {} if (typeof properties === 'string') { @@ -92,8 +98,8 @@ export function createBlob (parts, properties) { } var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder : typeof MSBlobBuilder !== 'undefined' - ? MSBlobBuilder : typeof MozBlobBuilder !== 'undefined' - ? MozBlobBuilder : WebKitBlobBuilder + ? MSBlobBuilder : typeof MozBlobBuilder !== 'undefined' + ? MozBlobBuilder : WebKitBlobBuilder var builder = new Builder() for (var i = 0; i < parts.length; i += 1) { builder.append(parts[i]) @@ -104,36 +110,34 @@ export function createBlob (parts, properties) { /** * 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 | URL.createObjectURL()} * to support browsers that only have the prefixed * webkitURL (e.g. Android <4.4). - * @param {Blob} blob - * @returns {string} url + * @param blob + * @returns url */ -export function createObjectURL (blob) { - /* global webkitURL */ +export function createObjectURL (blob: Blob): string { return (typeof URL !== 'undefined' ? URL : webkitURL).createObjectURL(blob) } /** * 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 | URL.revokeObjectURL()} * to support browsers that only have the prefixed * webkitURL (e.g. Android <4.4). - * @param {string} url + * @param url */ -export function revokeObjectURL (url) { - /* global webkitURL */ +export function revokeObjectURL (url: string): void { return (typeof URL !== 'undefined' ? URL : webkitURL).revokeObjectURL(url) } /** * Convert a Blob to a binary string. * - * @param {Blob} blob - * @returns {Promise} Promise that resolves with the binary string + * @param blob + * @returns Promise that resolves with the binary string */ -export function blobToBinaryString (blob) { +export function blobToBinaryString (blob: Blob): Promise { return new Promise(function (resolve, reject) { var reader = new FileReader() var hasBinaryString = typeof reader.readAsBinaryString === 'function' @@ -155,31 +159,31 @@ export function blobToBinaryString (blob) { /** * Convert a base64-encoded string to a Blob. - * @param {string} base64 - * @param {string|undefined} type - the content type (optional) - * @returns {Blob} + * @param base64 - base64-encoded string + * @param type - the content type (optional) + * @returns Blob */ -export function base64StringToBlob (base64, type) { +export function base64StringToBlob (base64: string, type?: string): Blob { var parts = [binaryStringToArrayBuffer(atob(base64))] return type ? createBlob(parts, { type: type }) : createBlob(parts) } /** * Convert a binary string to a Blob. - * @param {string} binary - * @param {string|undefined} type - the content type (optional) - * @returns {Blob} + * @param binary - binary string + * @param type - the content type (optional) + * @returns Blob */ -export function binaryStringToBlob (binary, type) { +export function binaryStringToBlob (binary: string, type?: string): Blob { return base64StringToBlob(btoa(binary), type) } /** * Convert a Blob to a binary string. - * @param {Blob} blob - * @returns {Promise} Promise that resolves with the binary string + * @param blob + * @returns Promise that resolves with the binary string */ -export function blobToBase64String (blob) { +export function blobToBase64String (blob: Blob): Promise { return blobToBinaryString(blob).then(btoa) } @@ -187,10 +191,10 @@ export function blobToBase64String (blob) { * Convert a data URL string * (e.g. 'data:image/png;base64,iVBORw0KG...') * to a Blob. - * @param {string} dataURL - * @returns {Blob} + * @param dataURL - dataURL-encoded string + * @returns Blob */ -export function dataURLToBlob (dataURL) { +export function dataURLToBlob (dataURL: string): Blob { var type = dataURL.match(/data:([^;]+)/)[1] var base64 = dataURL.replace(/^[^,]+,/, '') @@ -201,10 +205,10 @@ export function dataURLToBlob (dataURL) { /** * Convert a Blob to a data URL string * (e.g. 'data:image/png;base64,iVBORw0KG...'). - * @param {Blob} blob - * @returns {Promise} Promise that resolves with the data URL string + * @param blob + * @returns Promise that resolves with the data URL string */ -export function blobToDataURL (blob) { +export function blobToDataURL (blob: Blob): Promise { return blobToBase64String(blob).then(function (base64String) { return 'data:' + blob.type + ';base64,' + base64String }) @@ -217,15 +221,15 @@ export function blobToDataURL (blob) { *

Note: this will coerce the image to the desired content type, and it * will only paint the first frame of an animated GIF. * - * @param {string} src - * @param {string|undefined} type - the content type (optional, defaults to 'image/png') - * @param {string|undefined} crossOrigin - for CORS-enabled images, set this to + * @param src - image src + * @param type - the content type (optional, defaults to 'image/png') + * @param crossOrigin - for CORS-enabled images, set this to * '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' - * @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 { type = type || 'image/png' return loadImage(src, crossOrigin).then(imgToCanvas).then(function (canvas) { @@ -235,13 +239,13 @@ export function imgSrcToDataURL (src, type, crossOrigin, quality) { /** * Convert a canvas to a Blob. - * @param {string} canvas - * @param {string|undefined} type - the content type (optional, defaults to 'image/png') - * @param {number|undefined} quality - a number between 0 and 1 indicating image quality + * @param canvas + * @param type - the content type (optional, defaults to 'image/png') + * @param quality - a number between 0 and 1 indicating image quality * if the requested type is 'image/jpeg' or 'image/webp' - * @returns {Promise} Promise that resolves with the Blob + * @returns Promise that resolves with the Blob */ -export function canvasToBlob (canvas, type, quality) { +export function canvasToBlob (canvas: HTMLCanvasElement, type?: string, quality?: number): Promise { if (typeof canvas.toBlob === 'function') { return new Promise(function (resolve) { canvas.toBlob(resolve, type, quality) @@ -257,15 +261,15 @@ export function canvasToBlob (canvas, type, quality) { *

Note: this will coerce the image to the desired content type, and it * will only paint the first frame of an animated GIF. * - * @param {string} src - * @param {string|undefined} type - the content type (optional, defaults to 'image/png') - * @param {string|undefined} crossOrigin - for CORS-enabled images, set this to + * @param src - image src + * @param type - the content type (optional, defaults to 'image/png') + * @param crossOrigin - for CORS-enabled images, set this to * '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' - * @returns {Promise} Promise that resolves with the Blob + * @returns Promise that resolves with the Blob */ -export function imgSrcToBlob (src, type, crossOrigin, quality) { +export function imgSrcToBlob (src: string, type?: string, crossOrigin?: string, quality?: number): Promise { type = type || 'image/png' return loadImage(src, crossOrigin).then(imgToCanvas).then(function (canvas) { @@ -276,20 +280,20 @@ export function imgSrcToBlob (src, type, crossOrigin, quality) { /** * Convert an ArrayBuffer to a Blob. * - * @param {ArrayBuffer} buffer - * @param {string|undefined} type - the content type (optional) - * @returns {Blob} + * @param buffer + * @param type - the content type (optional) + * @returns Blob */ -export function arrayBufferToBlob (buffer, type) { +export function arrayBufferToBlob (buffer: ArrayBuffer, type?: string): Blob { return createBlob([buffer], type) } /** * Convert a Blob to an ArrayBuffer. - * @param {Blob} blob - * @returns {Promise} Promise that resolves with the ArrayBuffer + * @param blob + * @returns Promise that resolves with the ArrayBuffer */ -export function blobToArrayBuffer (blob) { +export function blobToArrayBuffer (blob: Blob): Promise { return new Promise(function (resolve, reject) { var reader = new FileReader() reader.onloadend = function (e) { diff --git a/.tslint.json b/tslint.json similarity index 100% rename from .tslint.json rename to tslint.json