Skip to content

Commit

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

describe('dataUrlRegExpBuilder', () => {
test('test', () => {
expect(dataUrlRegExpBuilder.build().test('data:,Hello World!')).toBeTrue()
expect(dataUrlRegExpBuilder.build().test('http://baidu.com')).toBeFalse()
})
})
8 changes: 8 additions & 0 deletions src/regexp/dataUrlRegExpBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RegExpBuilder } from './RegExpBuilder'

const baseRegExp =
/data:(([a-z]+\/[a-z0-9-+.]+)(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s]*?)/i

export const dataUrlRegExpBuilder = 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 @@ -9,6 +9,7 @@
// @index(['./**/*.ts', '!./**/*.test.*', '!**/__*'], f => `export * from '${f.path}'`)
export * from './RegExpBuilder'
export * from './blankCharsRegExpBuilder'
export * from './dataUrlRegExpBuilder'
export * from './emailRegExpBuilder'
export * from './emojiRegExpBuilder'
// @endindex
8 changes: 5 additions & 3 deletions src/utils/isDataUrl.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { dataUrlRegExpBuilder } from '../regexp'

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

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

isDataUrl.regex = /^data:(([a-z]+\/[a-z0-9-+.]+)(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s]*?)$/i
8 changes: 6 additions & 2 deletions src/utils/parseDataUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { isDataUrl } from './isDataUrl'
import { dataUrlRegExpBuilder } from '../regexp'
import { LiteralUnion } from '../types'
import { parseUrlQueryString } from './parseUrlQueryString'

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

export interface ParseDataUrlResult {
mimeType: string
parameters: Record<
Expand All @@ -26,7 +30,7 @@ export function parseDataUrl(dataUrl: string): ParseDataUrlResult {
,
extraParameters = '',
content = '',
] = dataUrl.match(isDataUrl.regex) || []
] = dataUrl.match(regExp) || []

const parameters = parseUrlQueryString(
`${mimeType}${extraParameters}`.replace(/^[^;]*;?/, ''),
Expand Down

0 comments on commit e4dc050

Please sign in to comment.