-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
531 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
name: Deno | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
tags: | ||
- '*' | ||
pull_request: | ||
branches: ["main"] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Setup repo | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup Deno | ||
# uses: denoland/setup-deno@v1 | ||
uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2 | ||
with: | ||
deno-version: v1.x | ||
|
||
- name: Verify formatting | ||
run: deno fmt --check | ||
|
||
- name: Run linter | ||
run: deno lint | ||
|
||
- name: Run tests | ||
run: deno test -A | ||
|
||
npm_publish: | ||
name: Publish to npm | ||
needs: test | ||
if: startsWith(github.ref, 'refs/tags/') | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Setup repo | ||
uses: actions/checkout@v3 | ||
|
||
- name: Get tag version | ||
id: get_tag_version | ||
run: echo TAG_VERSION=${GITHUB_REF/refs\/tags\//} >> $GITHUB_OUTPUT | ||
|
||
- name: Setup Deno | ||
# uses: denoland/setup-deno@v1 | ||
uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.2 | ||
with: | ||
deno-version: v1.x | ||
|
||
- name: Setup Node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: '18.x' | ||
registry-url: 'https://registry.npmjs.org' | ||
|
||
- name: npm build | ||
run: deno task build ${{steps.get_tag_version.outputs.TAG_VERSION}} | ||
|
||
- name: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
run: npm publish npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"deno.enable": true, | ||
"editor.defaultFormatter": "denoland.vscode-deno" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,41 @@ | ||
# file-reader-promise | ||
# file_reader_promise | ||
|
||
Promise-based [`FileReader`](https://developer.mozilla.org/docs/Web/API/FileReader) implementation. | ||
|
||
## Installation | ||
|
||
node | ||
|
||
```sh | ||
npm install @myrear/file_reader_promise | ||
``` | ||
|
||
deno | ||
|
||
```ts | ||
import { | ||
FileReader, | ||
readAs, | ||
} from 'https://deno.land/x/file_reader_promise/mod.ts' | ||
``` | ||
|
||
## Usage | ||
|
||
This library provides 2 modules: | ||
|
||
- [`readAs`](#readas) | ||
- [`FileReader`](#filereader): Thin wrapper class for the web standard [`FileReader`](https://developer.mozilla.org/docs/Web/API/FileReader) class | ||
|
||
### `readAs` | ||
|
||
```ts | ||
const result: string = await readAs(reader, file, 'DataURL') | ||
const result: ArrayBuffer = await readAs(reader, file, 'ArrayBuffer') | ||
``` | ||
|
||
### `FileReader` | ||
|
||
```ts | ||
const result: string = await new FileReader().readAsDataURL(file) | ||
const result: ArrayBuffer = await new FileReader().readAsArrayBuffer(file) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import './setup.ts' | ||
import { FileReader } from '../src/FileReader.ts' | ||
import { assertEquals } from 'std/assert/assert_equals.ts' | ||
import { assertInstanceOf } from 'std/assert/assert_instance_of.ts' | ||
|
||
Deno.test('read blob', async (t) => { | ||
const blob = new Blob(['foo'], { type: 'text/plain' }) | ||
|
||
await t.step('as `ArrayBuffer`', async () => { | ||
const reader = new FileReader() | ||
const result = await reader.readAsArrayBuffer(blob) | ||
|
||
assertEquals(result, reader.result) | ||
assertInstanceOf(result, ArrayBuffer) | ||
}) | ||
|
||
await t.step('as `BinaryString`', async () => { | ||
const reader = new FileReader() | ||
const result = await reader.readAsBinaryString(blob) | ||
|
||
assertEquals(result, reader.result) | ||
assertEquals(result, 'foo') | ||
}) | ||
|
||
await t.step('as `DataURL`', async () => { | ||
const reader = new FileReader() | ||
const result = await reader.readAsDataURL(blob) | ||
|
||
assertEquals(result, reader.result) | ||
assertEquals(result, 'data:text/plain;base64,Zm9v') | ||
}) | ||
|
||
await t.step('as `Text` with encoding utf-8', async () => { | ||
const reader = new FileReader() | ||
const result = await reader.readAsText(blob, 'utf-8') | ||
|
||
assertEquals(result, reader.result) | ||
assertEquals(result, 'foo') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import './setup.ts' | ||
import { assertEquals, assertInstanceOf } from 'std/assert/mod.ts' | ||
import { assertSpyCall, spy } from 'std/testing/mock.ts' | ||
import { readAs } from '../src/readAs.ts' | ||
|
||
Deno.test('read blob', async (t) => { | ||
const blob = new Blob(['foo'], { type: 'text/plain' }) | ||
|
||
await t.step('as `ArrayBuffer`', async () => { | ||
const reader = new FileReader() | ||
const readAsArrayBufferSpy = spy(reader, 'readAsArrayBuffer') | ||
const result = await readAs(reader, blob, 'ArrayBuffer') | ||
|
||
assertEquals(result, reader.result) | ||
assertInstanceOf(result, ArrayBuffer) | ||
assertSpyCall(readAsArrayBufferSpy, 0, { | ||
self: reader, | ||
args: [blob, undefined], | ||
}) | ||
}) | ||
|
||
await t.step('as `BinaryString`', async () => { | ||
const reader = new FileReader() | ||
const readAsBinaryStringSpy = spy(reader, 'readAsBinaryString') | ||
const result = await readAs(reader, blob, 'BinaryString') | ||
|
||
assertEquals(result, reader.result) | ||
assertEquals(result, 'foo') | ||
assertSpyCall(readAsBinaryStringSpy, 0, { | ||
self: reader, | ||
args: [blob, undefined], | ||
}) | ||
}) | ||
|
||
await t.step('as `DataURL`', async () => { | ||
const reader = new FileReader() | ||
const readAsDataURLSpy = spy(reader, 'readAsDataURL') | ||
const result = await readAs(reader, blob, 'DataURL') | ||
|
||
assertEquals(result, reader.result) | ||
assertEquals(result, 'data:text/plain;base64,Zm9v') | ||
assertSpyCall(readAsDataURLSpy, 0, { | ||
self: reader, | ||
args: [blob, undefined], | ||
}) | ||
}) | ||
|
||
await t.step('as `Text` with encoding utf-8', async () => { | ||
const reader = new FileReader() | ||
const readAsTextSpy = spy(reader, 'readAsText') | ||
const result = await readAs(reader, blob, 'Text', 'utf-8') | ||
|
||
assertEquals(result, reader.result) | ||
assertEquals(typeof result, 'string') | ||
assertSpyCall(readAsTextSpy, 0, { | ||
self: reader, | ||
args: [blob, 'utf-8'], | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { JSDOM } from 'jsdom' | ||
import { isNode } from 'which_runtime/mod.ts' | ||
|
||
if (isNode) { | ||
const { window: { Blob, FileReader } } = new JSDOM() | ||
globalThis.Blob = Blob | ||
globalThis.FileReader = FileReader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"tasks": { | ||
"build": "deno run -A npm_build.ts" | ||
}, | ||
"fmt": { | ||
"semiColons": false, | ||
"singleQuote": true, | ||
"exclude": ["*.md"] | ||
}, | ||
"exclude": ["npm/**/*"], | ||
"imports": { | ||
"dnt/": "https://deno.land/x/dnt@0.38.1/", | ||
"std/": "https://deno.land/std@0.199.0/", | ||
"jsdom": "https://esm.sh/jsdom@22.1.0", | ||
"which_runtime/": "https://deno.land/x/which_runtime@0.2.0/" | ||
} | ||
} |
Oops, something went wrong.