-
Notifications
You must be signed in to change notification settings - Fork 22
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
3 changed files
with
141 additions
and
0 deletions.
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
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,49 @@ | ||
import { base64Encode } from './base64' | ||
import { readFile } from './readFile' | ||
import { TextDecoder, TextEncoder } from 'util' | ||
|
||
describe(readFile.name, () => { | ||
test('text should work', async () => { | ||
const expectedText = 'vtils' | ||
const actualText = await readFile(new File([expectedText], 'x')).text() | ||
expect(actualText).toBe(expectedText) | ||
}) | ||
|
||
test('json should work', async () => { | ||
const expectedJson = { a: 1 } | ||
const actualJson = await readFile( | ||
new File([JSON.stringify(expectedJson)], 'x'), | ||
).json<typeof expectedJson>() | ||
expect(actualJson).toEqual(expectedJson) | ||
}) | ||
|
||
test('dataUrl should work', async () => { | ||
const expectedType = 'text/plain' | ||
const expectedData = 'hello' | ||
const expectedDataUrl = `data:${expectedType};base64,${base64Encode( | ||
expectedData, | ||
)}` | ||
const actualDataUrl = await readFile( | ||
new File([expectedData], 'x', { type: expectedType }), | ||
).dataUrl() | ||
expect(actualDataUrl).toBe(expectedDataUrl) | ||
}) | ||
|
||
test('base64 should work', async () => { | ||
const expectedData = 'hello' | ||
const expectedBase64 = base64Encode(expectedData) | ||
const actualBase64 = await readFile(new File([expectedData], 'x')).base64() | ||
expect(actualBase64).toBe(expectedBase64) | ||
}) | ||
|
||
test('arrayBuffer should work', async () => { | ||
const expectedData = 'hello' | ||
const expectedArrayBuffer = new TextEncoder().encode(expectedData) | ||
const actualArrayBuffer = await readFile( | ||
new File([expectedArrayBuffer], 'x'), | ||
).arrayBuffer() | ||
expect(new TextDecoder('utf-8').decode(actualArrayBuffer)).toBe( | ||
expectedData, | ||
) | ||
}) | ||
}) |
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,91 @@ | ||
export interface ReadFileResult { | ||
/** | ||
* Returns the text contents. | ||
*/ | ||
text(): Promise<string> | ||
|
||
/** | ||
* Returns the JSON contents. | ||
*/ | ||
json<T>(): Promise<T> | ||
|
||
/** | ||
* Returns the dataURL contents. | ||
*/ | ||
dataUrl(): Promise<string> | ||
|
||
/** | ||
* Returns the base64 contents. | ||
*/ | ||
base64(): Promise<string> | ||
|
||
/** | ||
* Returns the ArrayBuffer contents. | ||
*/ | ||
arrayBuffer(): Promise<ArrayBuffer> | ||
} | ||
|
||
/** | ||
* Reads the contents of the given file. | ||
* | ||
* @param file The given file. | ||
* @returns Returns the contents getters. | ||
*/ | ||
export function readFile(file: File): ReadFileResult { | ||
const text: ReadFileResult['text'] = () => { | ||
return new Promise((resolve, reject) => { | ||
const fileReader = new FileReader() | ||
fileReader.onload = () => { | ||
resolve(fileReader.result as string) | ||
} | ||
fileReader.onerror = () => { | ||
reject(fileReader.error) | ||
} | ||
fileReader.readAsText(file) | ||
}) | ||
} | ||
|
||
const json: ReadFileResult['json'] = () => { | ||
return text().then(data => { | ||
try { | ||
return JSON.parse(data) | ||
} catch (err) { | ||
return Promise.reject(err) | ||
} | ||
}) | ||
} | ||
|
||
const dataUrl: ReadFileResult['dataUrl'] = () => { | ||
return new Promise((resolve, reject) => { | ||
const fileReader = new FileReader() | ||
fileReader.onload = () => { | ||
resolve(fileReader.result as string) | ||
} | ||
fileReader.onerror = () => { | ||
reject(fileReader.error) | ||
} | ||
fileReader.readAsDataURL(file) | ||
}) | ||
} | ||
|
||
const base64: ReadFileResult['base64'] = () => { | ||
return dataUrl().then(url => { | ||
return url.split(';base64,')[1] | ||
}) | ||
} | ||
|
||
const arrayBuffer: ReadFileResult['arrayBuffer'] = () => { | ||
return new Promise((resolve, reject) => { | ||
const fileReader = new FileReader() | ||
fileReader.onload = () => { | ||
resolve(fileReader.result as ArrayBuffer) | ||
} | ||
fileReader.onerror = () => { | ||
reject(fileReader.error) | ||
} | ||
fileReader.readAsArrayBuffer(file) | ||
}) | ||
} | ||
|
||
return { text, json, dataUrl, base64, arrayBuffer } | ||
} |