Skip to content

Commit

Permalink
feat(utils): 新增 isBlobUrl 检测传入值是否是 Blob URL
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 8, 2020
1 parent 70e4727 commit 5654306
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export * from './inMiniProgram'
export * from './inNodeJS'
export * from './inTaro'
export * from './inWechatWebView'
export * from './isBlobUrl'
export * from './isChineseIDCardNumber'
export * from './isDataUrl'
export * from './isNumeric'
Expand Down
13 changes: 13 additions & 0 deletions src/utils/isBlobUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isBlobUrl } from './isBlobUrl'

describe('isBlobUrl', () => {
test('表现正常', () => {
expect(
isBlobUrl(
'blob:https://example.org/9115d58c-bcda-ff47-86e5-083e9a215304',
),
).toBeTrue()
expect(isBlobUrl('blob:foo')).toBeFalse()
expect(isBlobUrl('http://foo.bar')).toBeFalse()
})
})
17 changes: 17 additions & 0 deletions src/utils/isBlobUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 检测传入值是否是 Blob URL,也称 Object URL。
*
* @public
* @param value 要检测的值
* @returns 返回检测结果
* @example
* ```typescript
* isBlobUrl('http://foo.bar') // => false
* isBlobUrl('blob:https://example.org/9115d58c-bcda-ff47-86e5-083e9a215304') // => true
* ```
*/
export function isBlobUrl(value: string) {
return isBlobUrl.regex.test(value)
}

isBlobUrl.regex = /^blob:.+\/[\w-]{36,}(?:#.+)?$/
3 changes: 2 additions & 1 deletion src/utils/loadCss.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isBlobUrl } from './isBlobUrl'
import { isDataUrl } from './isDataUrl'
import { isUrl } from './isUrl'
import { loadResource, LoadResourceUrlType } from './loadResource'
Expand Down Expand Up @@ -29,7 +30,7 @@ export interface LoadCssResult {
export function loadCss(urlOrContent: string): Promise<LoadCssResult> {
return (urlOrContent in cache
? Promise.resolve(cache[urlOrContent])
: isUrl(urlOrContent) || isDataUrl(urlOrContent)
: isUrl(urlOrContent) || isDataUrl(urlOrContent) || isBlobUrl(urlOrContent)
? loadResource({
type: LoadResourceUrlType.css,
path: urlOrContent,
Expand Down

0 comments on commit 5654306

Please sign in to comment.