Skip to content

Commit 8fb5627

Browse files
committed
first release
1 parent 807e2c3 commit 8fb5627

File tree

14 files changed

+531
-1
lines changed

14 files changed

+531
-1
lines changed

.github/workflows/deno.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Deno
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
tags:
7+
- '*'
8+
pull_request:
9+
branches: ["main"]
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Setup repo
17+
uses: actions/checkout@v3
18+
19+
- name: Setup Deno
20+
# uses: denoland/setup-deno@v1
21+
uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2
22+
with:
23+
deno-version: v1.x
24+
25+
- name: Verify formatting
26+
run: deno fmt --check
27+
28+
- name: Run linter
29+
run: deno lint
30+
31+
- name: Run tests
32+
run: deno test -A
33+
34+
npm_publish:
35+
name: Publish to npm
36+
needs: test
37+
if: startsWith(github.ref, 'refs/tags/')
38+
runs-on: ubuntu-latest
39+
steps:
40+
- name: Setup repo
41+
uses: actions/checkout@v3
42+
43+
- name: Get tag version
44+
id: get_tag_version
45+
run: echo TAG_VERSION=${GITHUB_REF/refs\/tags\//} >> $GITHUB_OUTPUT
46+
47+
- name: Setup Deno
48+
# uses: denoland/setup-deno@v1
49+
uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2
50+
with:
51+
deno-version: v1.x
52+
53+
- name: Setup Node
54+
uses: actions/setup-node@v3
55+
with:
56+
node-version: '18.x'
57+
registry-url: 'https://registry.npmjs.org'
58+
59+
- name: npm build
60+
run: deno task build ${{steps.get_tag_version.outputs.TAG_VERSION}}
61+
62+
- name: npm publish
63+
env:
64+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
65+
run: npm publish --access=public ./npm

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npm

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"deno.enable": true,
3+
"editor.defaultFormatter": "denoland.vscode-deno"
4+
}

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1-
# file-reader-promise
1+
# file_reader_promise
2+
3+
Promise-based [`FileReader`](https://developer.mozilla.org/docs/Web/API/FileReader) implementation.
4+
5+
## Installation
6+
7+
node
8+
9+
```sh
10+
npm install @myrear/file_reader_promise
11+
```
12+
13+
deno
14+
15+
```ts
16+
import {
17+
FileReader,
18+
readAs,
19+
} from 'https://deno.land/x/file_reader_promise/mod.ts'
20+
```
21+
22+
## Usage
23+
24+
This library provides 2 modules:
25+
26+
- [`readAs`](#readas)
27+
- [`FileReader`](#filereader): Thin wrapper class for the web standard [`FileReader`](https://developer.mozilla.org/docs/Web/API/FileReader) class
28+
29+
### `readAs`
30+
31+
```ts
32+
const result: string = await readAs(reader, file, 'DataURL')
33+
const result: ArrayBuffer = await readAs(reader, file, 'ArrayBuffer')
34+
```
35+
36+
### `FileReader`
37+
38+
```ts
39+
const result: string = await new FileReader().readAsDataURL(file)
40+
const result: ArrayBuffer = await new FileReader().readAsArrayBuffer(file)
41+
```

__tests__/FileReader.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import './setup.ts'
2+
import { FileReader } from '../src/FileReader.ts'
3+
import { assertEquals } from 'std/assert/assert_equals.ts'
4+
import { assertInstanceOf } from 'std/assert/assert_instance_of.ts'
5+
6+
Deno.test('read blob', async (t) => {
7+
const blob = new Blob(['foo'], { type: 'text/plain' })
8+
9+
await t.step('as `ArrayBuffer`', async () => {
10+
const reader = new FileReader()
11+
const result = await reader.readAsArrayBuffer(blob)
12+
13+
assertEquals(result, reader.result)
14+
assertInstanceOf(result, ArrayBuffer)
15+
})
16+
17+
await t.step('as `BinaryString`', async () => {
18+
const reader = new FileReader()
19+
const result = await reader.readAsBinaryString(blob)
20+
21+
assertEquals(result, reader.result)
22+
assertEquals(result, 'foo')
23+
})
24+
25+
await t.step('as `DataURL`', async () => {
26+
const reader = new FileReader()
27+
const result = await reader.readAsDataURL(blob)
28+
29+
assertEquals(result, reader.result)
30+
assertEquals(result, 'data:text/plain;base64,Zm9v')
31+
})
32+
33+
await t.step('as `Text` with encoding utf-8', async () => {
34+
const reader = new FileReader()
35+
const result = await reader.readAsText(blob, 'utf-8')
36+
37+
assertEquals(result, reader.result)
38+
assertEquals(result, 'foo')
39+
})
40+
})

__tests__/readAs.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import './setup.ts'
2+
import { assertEquals, assertInstanceOf } from 'std/assert/mod.ts'
3+
import { assertSpyCall, spy } from 'std/testing/mock.ts'
4+
import { readAs } from '../src/readAs.ts'
5+
6+
Deno.test('read blob', async (t) => {
7+
const blob = new Blob(['foo'], { type: 'text/plain' })
8+
9+
await t.step('as `ArrayBuffer`', async () => {
10+
const reader = new FileReader()
11+
const readAsArrayBufferSpy = spy(reader, 'readAsArrayBuffer')
12+
const result = await readAs(reader, blob, 'ArrayBuffer')
13+
14+
assertEquals(result, reader.result)
15+
assertInstanceOf(result, ArrayBuffer)
16+
assertSpyCall(readAsArrayBufferSpy, 0, {
17+
self: reader,
18+
args: [blob, undefined],
19+
})
20+
})
21+
22+
await t.step('as `BinaryString`', async () => {
23+
const reader = new FileReader()
24+
const readAsBinaryStringSpy = spy(reader, 'readAsBinaryString')
25+
const result = await readAs(reader, blob, 'BinaryString')
26+
27+
assertEquals(result, reader.result)
28+
assertEquals(result, 'foo')
29+
assertSpyCall(readAsBinaryStringSpy, 0, {
30+
self: reader,
31+
args: [blob, undefined],
32+
})
33+
})
34+
35+
await t.step('as `DataURL`', async () => {
36+
const reader = new FileReader()
37+
const readAsDataURLSpy = spy(reader, 'readAsDataURL')
38+
const result = await readAs(reader, blob, 'DataURL')
39+
40+
assertEquals(result, reader.result)
41+
assertEquals(result, 'data:text/plain;base64,Zm9v')
42+
assertSpyCall(readAsDataURLSpy, 0, {
43+
self: reader,
44+
args: [blob, undefined],
45+
})
46+
})
47+
48+
await t.step('as `Text` with encoding utf-8', async () => {
49+
const reader = new FileReader()
50+
const readAsTextSpy = spy(reader, 'readAsText')
51+
const result = await readAs(reader, blob, 'Text', 'utf-8')
52+
53+
assertEquals(result, reader.result)
54+
assertEquals(typeof result, 'string')
55+
assertSpyCall(readAsTextSpy, 0, {
56+
self: reader,
57+
args: [blob, 'utf-8'],
58+
})
59+
})
60+
})

__tests__/setup.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { JSDOM } from 'jsdom'
2+
import { isNode } from 'which_runtime/mod.ts'
3+
4+
if (isNode) {
5+
const { window: { Blob, FileReader } } = new JSDOM()
6+
globalThis.Blob = Blob
7+
globalThis.FileReader = FileReader
8+
}

deno.jsonc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"tasks": {
3+
"build": "deno run -A npm_build.ts"
4+
},
5+
"fmt": {
6+
"semiColons": false,
7+
"singleQuote": true,
8+
"exclude": ["*.md"]
9+
},
10+
"exclude": ["npm/**/*"],
11+
"imports": {
12+
"dnt/": "https://deno.land/x/dnt@0.38.1/",
13+
"std/": "https://deno.land/std@0.199.0/",
14+
"jsdom": "https://esm.sh/jsdom@22.1.0",
15+
"which_runtime/": "https://deno.land/x/which_runtime@0.2.0/"
16+
}
17+
}

0 commit comments

Comments
 (0)