Skip to content

Commit

Permalink
feat: download
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Nov 17, 2024
1 parent 79c9324 commit 130d65a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/items/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const utilItems = [
{ text: 'mitt', link: '/util/mitt' },
{ text: 'copyText', link: '/util/copy-text' },
{ text: 'download', link: '/util/download' },
{ text: 'storage', link: '/util/storage' },
{ text: 'classes', link: '/util/classes' },
{ text: 'createNamespaceFn', link: '/util/create-namespace-fn' },
Expand Down
20 changes: 20 additions & 0 deletions docs/util/download.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# download

Trigger browser download, supporting downloading via file `url`, `Blob`, `File`.

### Usage

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

download('/hello.txt', 'hello.txt')
download(new Blob(['hello']), 'hello.txt')
download(new File(['helle'], 'hello.txt', { type: 'text/plain' }), 'hello.txt')
```

### Arguments

| Arg | Type | Defaults |
| ---------- | ------------------------ | -------- |
| `value` | `string \| Blob \| File` | |
| `filename` | `string` | |
20 changes: 20 additions & 0 deletions docs/zh/util/download.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# download

触发浏览器下载,支持通过 `文件地址``Blob``File`

### 使用

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

download('/hello.txt', 'hello.txt')
download(new Blob(['hello']), 'hello.txt')
download(new File(['helle'], 'hello.txt', { type: 'text/plain' }), 'hello.txt')
```

### 参数

| 参数 | 类型 | 默认值 |
| ---------- | ------------------------ | ------ |
| `value` | `string \| Blob \| File` | |
| `filename` | `string` | |
13 changes: 13 additions & 0 deletions src/util/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isString } from '../general'

export function download(val: string | Blob | File, filename: string = 'file') {
const a = document.createElement('a')
a.style.display = 'none'
a.href = isString(val) ? val : URL.createObjectURL(val)
a.download = filename

document.body.appendChild(a)
a.click()
URL.revokeObjectURL(a.href)
document.body.removeChild(a)
}
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './raf'
export * from './requestAnimationFrame'
export * from './storage'
export * from './tryParseJSON'
export * from './download'
24 changes: 24 additions & 0 deletions tests/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
tryParseJSON,
prettyJSONObject,
copyText,
download,
} from '../src'

it('requestAnimationFrame', () => {
Expand Down Expand Up @@ -263,3 +264,26 @@ describe('copyText', () => {
execCommandMock.mockRestore()
})
})

it('download', () => {
let href = ''

Reflect.defineProperty(HTMLAnchorElement.prototype, 'href', {
set(v) {
href = v
},
get() {
return href
},
})

URL.createObjectURL = vi.fn(() => 'mock')
URL.revokeObjectURL = vi.fn()
download(new Blob(['hello']), 'test.txt')
expect(href).toBe('mock')
expect(URL.createObjectURL).toHaveBeenCalled()
expect(URL.revokeObjectURL).toHaveBeenCalled()

download('/a.jpg', 'test.txt')
expect(href).toBe('/a.jpg')
})

0 comments on commit 130d65a

Please sign in to comment.