Skip to content

Commit

Permalink
feat(utils): 新增 isDataUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 27, 2020
1 parent b6be4e0 commit 80b3a0c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export {
isFuture,
isLastDayOfMonth,
isLeapYear,
isMatch,
isMonday,
isPast,
isSameDay,
Expand Down Expand Up @@ -232,6 +233,7 @@ export {
enCA,
enGB,
enIN,
enNZ,
enUS,
eo,
es,
Expand All @@ -241,6 +243,7 @@ export {
fi,
fr,
frCA,
gd,
gl,
gu,
he,
Expand All @@ -256,6 +259,7 @@ export {
kk,
kn,
ko,
lb,
lt,
lv,
mk,
Expand Down
1 change: 1 addition & 0 deletions src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export {
useMountedState,
useMouse,
useMouseHovered,
useMouseWheel,
useMultiStateValidator,
useNetwork,
useNumber,
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export * from './inNodeJS'
export * from './inTaro'
export * from './inWechatWebView'
export * from './isChineseIDCardNumber'
export * from './isDataUrl'
export * from './isNumeric'
export * from './isPossibleChineseMobilePhoneNumber'
export * from './isPromiseLike'
Expand Down
46 changes: 46 additions & 0 deletions src/utils/isDataUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { isDataUrl } from './isDataUrl'

describe('isDataUrl', () => {
test('是', () => {
const valid = [
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC',
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC',
'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Crect%20fill%3D%22%2300B1FF%22%20width%3D%22100%22%20height%3D%22100%22%2F%3E%3C%2Fsvg%3E',
'data:image/svg+xml;charset=utf-8;name=bar.svg,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Crect%20fill%3D%22%2300B1FF%22%20width%3D%22100%22%20height%3D%22100%22%2F%3E%3C%2Fsvg%3E',
'data:image/png;name=foo.png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC',
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCBmaWxsPSIjMDBCMUZGIiB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIvPjwvc3ZnPg==',
'data:,Hello%2C%20World!',
'data:,Hello World!',
'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D',
'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E',
'data:,A%20brief%20note',
'data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E',
'data:audio/mp3;base64,%3Ch1%3EHello!%3C%2Fh1%3E',
'data:video/x-ms-wmv;base64,%3Ch1%3EHello!%3C%2Fh1%3E',
'data:application/vnd.ms-excel;base64,PGh0bWw%2BPC9odG1sPg%3D%3D',
'data:image/svg+xml;name=foobar%20(1).svg;charset=UTF-8,some-data',
]
for (const item of valid) {
expect(isDataUrl(item)).toBeTrue()
}
})

test('否', () => {
const invalid = [
'dataxbase64',
'data:HelloWorld',
'data:text/html;charset=,%3Ch1%3EHello!%3C%2Fh1%3E',
'data:text/plain;name=@;base64,SGVsbG8sIFdvcmxkIQ%3D%3D',
'data:text/html;charset,%3Ch1%3EHello!%3C%2Fh1%3E',
'data:base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
'',
'http://wikipedia.org',
'base64',
'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
]
for (const item of invalid) {
expect(isDataUrl(item)).toBeFalse()
}
})
})
17 changes: 17 additions & 0 deletions src/utils/isDataUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 检测传入值是否是 Data URL。
*
* @public
* @param value 要检测的值
* @returns 返回检测结果
* @example
* ```typescript
* isDataUrl('http://foo.bar') // => false
* isDataUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D') // => true
* ```
*/
export function isDataUrl(value: string) {
return isDataUrl.regex.test(value)
}

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

0 comments on commit 80b3a0c

Please sign in to comment.