Skip to content

Commit

Permalink
first release
Browse files Browse the repository at this point in the history
  • Loading branch information
myrear committed Aug 22, 2023
1 parent 807e2c3 commit 55b2bbb
Show file tree
Hide file tree
Showing 14 changed files with 530 additions and 1 deletion.
62 changes: 62 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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: 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: cd npm && npm publish
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deno.enable": true,
"editor.defaultFormatter": "denoland.vscode-deno"
}
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
# 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)
```
40 changes: 40 additions & 0 deletions __tests__/FileReader.test.ts
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')
})
})
60 changes: 60 additions & 0 deletions __tests__/readAs.test.ts
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'],
})
})
})
8 changes: 8 additions & 0 deletions __tests__/setup.ts
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
}
17 changes: 17 additions & 0 deletions deno.jsonc
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/"
}
}
Loading

0 comments on commit 55b2bbb

Please sign in to comment.