Skip to content

Commit

Permalink
feat: add readFile
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 31, 2020
1 parent 36ce38b commit 8704e3c
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export * from './utils/indent'
export * from './utils/isChineseIDCardNumber'
export * from './utils/isPossibleChineseMobilePhoneNumber'
export * from './utils/isUrl'
export * from './utils/readFile'
export * from './utils/wait'
// @endindex
49 changes: 49 additions & 0 deletions src/utils/readFile.test.ts
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,
)
})
})
91 changes: 91 additions & 0 deletions src/utils/readFile.ts
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 }
}

0 comments on commit 8704e3c

Please sign in to comment.