-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert blob package to TypeScript #61541
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
type BlobPart = BufferSource | Blob | string; | ||
|
||
/** | ||
* @type {Record<string, File|undefined>} | ||
*/ | ||
const cache = {}; | ||
const cache: Record< string, File | undefined > = {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the cache type should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, we can remove all of the JSDoc type annotations. |
||
|
||
/** | ||
* Create a blob URL from a file. | ||
|
@@ -10,7 +12,7 @@ const cache = {}; | |
* | ||
* @return {string} The blob URL. | ||
*/ | ||
export function createBlobURL( file ) { | ||
export function createBlobURL( file: File ): string { | ||
const url = window.URL.createObjectURL( file ); | ||
|
||
cache[ url ] = file; | ||
|
@@ -27,7 +29,7 @@ export function createBlobURL( file ) { | |
* | ||
* @return {File|undefined} The file for the blob URL. | ||
*/ | ||
export function getBlobByURL( url ) { | ||
export function getBlobByURL( url: string ): File | undefined { | ||
return cache[ url ]; | ||
} | ||
|
||
|
@@ -40,7 +42,7 @@ export function getBlobByURL( url ) { | |
* | ||
* @return {string|undefined} The blob type. | ||
*/ | ||
export function getBlobTypeByURL( url ) { | ||
export function getBlobTypeByURL( url: string ): string | undefined { | ||
return getBlobByURL( url )?.type.split( '/' )[ 0 ]; // 0: media type , 1: file extension eg ( type: 'image/jpeg' ). | ||
} | ||
|
||
|
@@ -49,7 +51,7 @@ export function getBlobTypeByURL( url ) { | |
* | ||
* @param {string} url The blob URL. | ||
*/ | ||
export function revokeBlobURL( url ) { | ||
export function revokeBlobURL( url: string ): void { | ||
if ( cache[ url ] ) { | ||
window.URL.revokeObjectURL( url ); | ||
} | ||
|
@@ -64,7 +66,7 @@ export function revokeBlobURL( url ) { | |
* | ||
* @return {boolean} Is the url a blob url? | ||
*/ | ||
export function isBlobURL( url ) { | ||
export function isBlobURL( url: string | undefined ): boolean { | ||
if ( ! url || ! url.indexOf ) { | ||
return false; | ||
} | ||
|
@@ -94,7 +96,11 @@ export function isBlobURL( url ) { | |
* @param {BlobPart} content File content (BufferSource | Blob | string). | ||
* @param {string} contentType (Optional) File mime type. Default is `''`. | ||
*/ | ||
export function downloadBlob( filename, content, contentType = '' ) { | ||
export function downloadBlob( | ||
filename: string, | ||
content: BlobPart, | ||
contentType: string = '' | ||
): void { | ||
if ( ! filename || ! content ) { | ||
return; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BlobPart
should be a native built-in type, after all, it's used in theBlob
constructor. You don't need to define it here.