Skip to content

Commit

Permalink
feat: 新增函数
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Jan 18, 2024
1 parent e6f41b5 commit 0413d5e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
38 changes: 36 additions & 2 deletions src/is/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, it } from 'vitest'
import { describe, expect, it } from 'vitest'
import {
isServer,
isEdge,
Expand All @@ -20,7 +20,9 @@ import {
isMap,
isClient,
isUrl,
isEmail
isEmail,
isImgPath,
isEmptyVal
} from '../index'

it('isServer', () => {
Expand Down Expand Up @@ -190,3 +192,35 @@ it('isEmail', () => {
expect(isEmail('[email protected]')).toBeTruthy()
expect(isEmail('502431556@qq')).toBeFalsy()
})

describe('isImgPath', () => {
it('should return true for valid image URLs', () => {
expect(isImgPath('http://www.baidu.com/1.png')).toBe(true)
expect(isImgPath('https://www.example.com/image.jpg')).toBe(true)
expect(
isImgPath(
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
)
).toBe(true)
})

it('should return false for invalid image URLs', () => {
expect(isImgPath('http://www.baidu.com')).toBe(false)
expect(isImgPath('https://www.example.com/image.txt')).toBe(false)
expect(isImgPath('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D')).toBe(false)
})
})

describe('isEmptyVal', () => {
it('should return true for empty values', () => {
expect(isEmptyVal('')).toBe(true)
expect(isEmptyVal(null)).toBe(true)
expect(isEmptyVal(undefined)).toBe(true)
})

it('should return false for non-empty values', () => {
expect(isEmptyVal('Hello')).toBe(false)
expect(isEmptyVal(0)).toBe(false)
expect(isEmptyVal(false)).toBe(false)
})
})
28 changes: 28 additions & 0 deletions src/is/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,31 @@ export const isEmail = (val: string): boolean => {
const reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/g
return reg.test(val)
}

/**
* 判断是否是合法的图片
* @category Is
* @param val 需要验证的值
* @example
* ``` typescript
* isImgPath('http://www.baidu.com/1.png')
* ```
*/
export const isImgPath = (val: string): boolean => {
return /(https?:\/\/.*\.(png|jpg|jpeg|gif|svg|webp|ico))|(data:image\/\w+;base64,[\w+/=]+)/i.test(
val
)
}

/**
* 判断是否是空值
* @category Is
* @param val 需要验证的值
* @example
* ``` typescript
* isEmptyVal('')
* ```
*/
export const isEmptyVal = (val: any): boolean => {
return val === '' || val === null || val === undefined
}
12 changes: 11 additions & 1 deletion src/time/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { formatTime, timestamp } from '../index'
import { formatTime, sleep, timestamp } from '../index'

describe('formatTime', () => {
it('should format time correctly', () => {
Expand Down Expand Up @@ -29,3 +29,13 @@ describe('timestamp', () => {
expect(result).toBeLessThanOrEqual(after)
})
})

describe('sleep', () => {
it('should pause execution for the specified time', async () => {
const startTime = Date.now()
await sleep(1000)
const endTime = Date.now()
const elapsedTime = endTime - startTime
expect(elapsedTime).toBeGreaterThanOrEqual(1000)
})
})
10 changes: 10 additions & 0 deletions src/time/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ export function formatTime(time: Date | number | string | null | undefined, fmt:
* ```
*/
export const timestamp = () => +Date.now()

/**
* 等待指定时间
* @param time 等待时间
* @example
* ``` typescript
* await sleep(1000)
* ```
*/
export const sleep = (time: number) => new Promise((resolve) => setTimeout(resolve, time))

0 comments on commit 0413d5e

Please sign in to comment.