Skip to content

Commit

Permalink
feat: isFile isBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Nov 10, 2024
1 parent 069e01f commit 66c159c
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
- 🛠️   Provide utilities frequently used in daily development
- 🛠️   Utilities implementation is very lightweight
- 🛠️   Written based on ts, providing complete ts types
- 💪   Make sure more than 90% unit test coverage, providing stability assurance
- 💪   Make sure more than 99% unit test coverage, providing stability assurance

### Installation

Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
- 🛠️   提供日常开发中经常使用的实用工具
- 🛠️   工具实现非常轻量
- 🛠️   使用 ts 编写,提供完善的类型支持
- 💪   确保 90% 以上单元测试覆盖率,提供稳定性保证
- 💪   确保 99% 以上单元测试覆盖率,提供稳定性保证

### 安装

Expand Down
2 changes: 2 additions & 0 deletions docs/.vitepress/items/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const generalItems = [
{ text: 'isSymbol', link: '/general/is-symbol' },
{ text: 'isWeakMap', link: '/general/is-weak-map' },
{ text: 'isWeakSet', link: '/general/is-weak-set' },
{ text: 'isBlob', link: '/general/is-blob' },
{ text: 'isFile', link: '/general/is-file' },
{ text: 'isArrayBuffer', link: '/general/is-array-buffer' },
{ text: 'isTypedArray', link: '/general/is-typed-array' },
{ text: 'isDataView', link: '/general/is-data-view' },
Expand Down
23 changes: 23 additions & 0 deletions docs/general/is-blob.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# isBlob

Determine whether the input value is a `Blob`.

### Usage

```ts
import { isBlob } from 'rattail'

isBlob(new Blob(['Hello, World!'], { type: 'text/plain' })) // return true
```

### Arguments

| Arg | Type | Defaults |
| ------- | :---: | -------: |
| `value` | `any` | |

### Return

| Type |
| :-------: |
| `boolean` |
24 changes: 24 additions & 0 deletions docs/general/is-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# isFile

Determine whether the input value is a `File`.

### Usage

```ts
import { isFile } from 'rattail'

isFile(new File(['Hello, world!'], 'hello.txt', { type: 'text/plain' }))
// return true
```

### Arguments

| Arg | Type | Defaults |
| ------- | :---: | -------: |
| `value` | `any` | |

### Return

| Type |
| :-------: |
| `boolean` |
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ features:
- title: General
details: Provide utilities frequently used in daily development
- title: Tests Coverage
details: Make sure more than 90% unit test coverage, providing stability assurance
details: Make sure more than 99% unit test coverage, providing stability assurance
- title: Lightweight
details: Utilities implementation is very lightweight
- title: Ts-friendly
Expand Down
23 changes: 23 additions & 0 deletions docs/zh/general/is-blob.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# isBlob

判断输入值是否是 `Blob`

### 使用

```ts
import { isBlob } from 'rattail'

isBlob(new Blob(['Hello, World!'], { type: 'text/plain' })) // return true
```

### 参数列表

| 参数 | 类型 | 默认值 |
| ------- | :---: | -----: |
| `value` | `any` | |

### 返回值

| 类型 |
| :-------: |
| `boolean` |
24 changes: 24 additions & 0 deletions docs/zh/general/is-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# isFile

判断输入值是否是 `File`

### 使用

```ts
import { isFile } from 'rattail'

isFile(new File(['Hello, world!'], 'hello.txt', { type: 'text/plain' }))
// return true
```

### 参数列表

| 参数 | 类型 | 默认值 |
| ------- | :---: | -----: |
| `value` | `any` | |

### 返回值

| 类型 |
| :-------: |
| `boolean` |
2 changes: 1 addition & 1 deletion docs/zh/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ features:
- title: 通用
details: 提供日常开发中经常使用的实用工具
- title: 测试覆盖
details: 确保 90% 以上单元测试覆盖率,提供稳定性保证
details: 确保 99% 以上单元测试覆盖率,提供稳定性保证
- title: 轻量
details: 工具实现非常轻量
- title: Ts 友好
Expand Down
2 changes: 2 additions & 0 deletions src/general/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ export * from './isWindow'
export * from './supportTouch'
export * from './toRawType'
export * from './toTypeString'
export * from './isFile'
export * from './isBlob'
5 changes: 5 additions & 0 deletions src/general/isBlob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { toRawType } from './toRawType'

export function isBlob(val: unknown): val is Blob {
return toRawType(val) === 'Blob'
}
5 changes: 5 additions & 0 deletions src/general/isFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { toRawType } from './toRawType'

export function isFile(val: unknown): val is File {
return toRawType(val) === 'File'
}
16 changes: 14 additions & 2 deletions tests/general.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { it, expect } from 'vitest'
import { it, expect, vi } from 'vitest'

Check warning on line 1 in tests/general.spec.ts

View workflow job for this annotation

GitHub Actions / test

'vi' is defined but never used
import {
isNonEmptyArray,
isString,
Expand All @@ -20,6 +20,8 @@ import {
isNullish,
isWindow,
isTruthy,
isFile,
isBlob,
toRawType,
hasOwn,
supportTouch,
Expand Down Expand Up @@ -257,6 +259,16 @@ it('isWindow', () => {
expect(isWindow(undefined)).toBe(false)
})

it('isFile', () => {
expect(isFile(new File([], 'test.txt'))).toBe(true)
expect(isFile(new Blob([]))).toBe(false)
})

it('isBlob', () => {
expect(isBlob(new Blob([]))).toBe(true)
expect(isBlob(new File([], 'test.txt'))).toBe(false)
})

it('hasOwn', () => {
expect(hasOwn({ foo: 1 }, 'foo')).toBe(true)
expect(hasOwn({ foo: 1 }, 'bar')).toBe(false)
Expand All @@ -276,7 +288,7 @@ it('inMobile', () => {
})

it('getGlobalThis', () => {
expect(getGlobalThis()).toBe(global)
expect(getGlobalThis()).toBe(globalThis)
expect(getGlobalThis()).toBe(window)
expect(getGlobalThis()).toBe(self)
})
Expand Down
4 changes: 4 additions & 0 deletions tests/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ it('sumHash', () => {
const a: Record<string, any> = { a: '123', self: undefined }
a.self = a
expect(sumHash(a)).toBe('76b6e174')

const b: any = { a: '123' }
b.valueOf = undefined
expect(sumHash(b)).toBe('4d3182a6')
})

0 comments on commit 66c159c

Please sign in to comment.