Skip to content

Commit

Permalink
feat(regexp): 新增 blobUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 27, 2023
1 parent bfe3f07 commit d1e1ba5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/regexp/blobUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { blobUrlRegExpBuilder } from './blobUrl'

describe('blobUrlRegExpBuilder', () => {
test('test', () => {
expect(
blobUrlRegExpBuilder
.build()
.test('blob:http://baidu.com/8c8f76be-2f7a-4d11-8efd-e714c6457fe9'),
).toBeTrue()
expect(blobUrlRegExpBuilder.build().test('data:,Hello World!')).toBeFalse()
})
})
7 changes: 7 additions & 0 deletions src/regexp/blobUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RegExpBuilder } from './RegExpBuilder'

const baseRegExp = /blob:.+\/[\w-]{36,}(?:#.+)?/

export const blobUrlRegExpBuilder = new RegExpBuilder({
baseRegExp: baseRegExp,
})
1 change: 1 addition & 0 deletions src/regexp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// @index(['./**/*.ts', '!./**/*.test.*', '!**/__*'], f => `export * from '${f.path}'`)
export * from './blankChars'
export * from './blobUrl'
export * from './dataUrl'
export * from './email'
export * from './emoji'
Expand Down
8 changes: 5 additions & 3 deletions src/utils/isBlobUrl.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { blobUrlRegExpBuilder } from '../regexp'

const regExp = blobUrlRegExpBuilder.build({ exact: true })

/**
* 检测传入值是否是 Blob URL,也称 Object URL。
*
Expand All @@ -11,7 +15,5 @@
* ```
*/
export function isBlobUrl(value: string) {
return isBlobUrl.regex.test(value)
return regExp.test(value)
}

isBlobUrl.regex = /^blob:.+\/[\w-]{36,}(?:#.+)?$/

0 comments on commit d1e1ba5

Please sign in to comment.