mirror of
https://github.com/Warky-Devs/artemis-kit.git
synced 2026-02-01 23:44:27 +00:00
Test cases and minor function fixes
This commit is contained in:
10
src/base64/Base64ToBlob.test.ts
Normal file
10
src/base64/Base64ToBlob.test.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { base64ToBlob } from './Base64ToBlob'
|
||||
|
||||
describe('base64ToBlob', () => {
|
||||
it('should convert a base64 string to a Blob object', () => {
|
||||
const base64 = 'SGVsbG8sIHdvcmxkIQ=='
|
||||
const blob = base64ToBlob(base64)
|
||||
expect(blob).toBeInstanceOf(Blob)
|
||||
})
|
||||
})
|
||||
11
src/base64/BlobToBase64.test.ts
Normal file
11
src/base64/BlobToBase64.test.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { blobToBase64 } from './BlobToBase64'
|
||||
|
||||
describe('blobToBase64', () => {
|
||||
it('should convert a Blob object to a base64 encoded string', async () => {
|
||||
const content = 'Hello, world!'
|
||||
const blob = new Blob([content], { type: 'text/plain' })
|
||||
const base64 = await blobToBase64(blob)
|
||||
expect(base64).toBe('SGVsbG8sIHdvcmxkIQ==')
|
||||
})
|
||||
})
|
||||
22
src/base64/BlobToString.test.ts
Normal file
22
src/base64/BlobToString.test.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { BlobToString } from './BlobToString'
|
||||
|
||||
describe('blobToString', () => {
|
||||
it('should convert a Blob object to a string', async () => {
|
||||
const content = 'Hello, world!'
|
||||
const blob = new Blob([content], { type: 'text/plain' })
|
||||
const text = await BlobToString(blob)
|
||||
expect(text).toBe(content)
|
||||
})
|
||||
|
||||
it('should return the input if it is already a string', async () => {
|
||||
const content = 'Hello, world!'
|
||||
const text = await BlobToString(content)
|
||||
expect(text).toBe(content)
|
||||
})
|
||||
|
||||
it('should return an empty string if the Blob is null', async () => {
|
||||
const text = await BlobToString(null)
|
||||
expect(text).toBe('')
|
||||
})
|
||||
})
|
||||
@@ -3,12 +3,21 @@
|
||||
* @param blob - The Blob object to convert
|
||||
* @returns Promise that resolves with the text
|
||||
*/
|
||||
async function blobToString(blob: Blob | string): Promise<string> {
|
||||
if (!blob) return ''
|
||||
if (typeof blob === 'string') {
|
||||
return blob
|
||||
}
|
||||
return await blob.text()
|
||||
function BlobToString(blob: Blob | string): Promise<string> {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
if (!blob) return resolve('')
|
||||
if (typeof blob === 'string') {
|
||||
return resolve(blob)
|
||||
}
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
const text = reader.result as string
|
||||
resolve(text)
|
||||
}
|
||||
reader.onerror = reject
|
||||
reader.readAsText(blob)
|
||||
})
|
||||
}
|
||||
|
||||
export { blobToString }
|
||||
export { BlobToString }
|
||||
|
||||
11
src/base64/FileToBase64.test.ts
Normal file
11
src/base64/FileToBase64.test.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { FileToBase64 } from './FileToBase64'
|
||||
|
||||
describe('FileToBase64', () => {
|
||||
it('should convert a File object to a base64 encoded string', async () => {
|
||||
const content = 'Hello, world!'
|
||||
const file = new File([content], 'hello.txt', { type: 'text/plain' })
|
||||
const base64 = await FileToBase64(file)
|
||||
expect(base64).toBe('SGVsbG8sIHdvcmxkIQ==')
|
||||
})
|
||||
})
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
/**
|
||||
* Converts a File object to a base64 encoded string.
|
||||
* @param file - The File object to convert
|
||||
@@ -7,12 +8,13 @@ function FileToBase64(file: File): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => {
|
||||
const d = reader.result?.toString()
|
||||
resolve(btoa(d ?? ''))
|
||||
const dataUrl = (reader.result ?? '') as string
|
||||
const base64 = dataUrl?.split?.(',')?.[1]
|
||||
resolve(base64)
|
||||
}
|
||||
reader.onerror = reject
|
||||
reader.readAsArrayBuffer(file)
|
||||
reader.readAsDataURL(file)
|
||||
})
|
||||
}
|
||||
|
||||
export { FileToBase64 }
|
||||
export { FileToBase64 }
|
||||
20
src/base64/FileToBlob.test.ts
Normal file
20
src/base64/FileToBlob.test.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { FileToBlob } from './FileToBlob'
|
||||
import { BlobToString } from './BlobToString'
|
||||
|
||||
|
||||
// FILE: src/base64/FileToBlob.test.ts
|
||||
|
||||
describe('FileToBlob', () => {
|
||||
it('should convert a File object to a Blob object', async () => {
|
||||
const content = 'Hello, world!'
|
||||
const file = new File([content], 'hello.txt', { type: 'text/plain' })
|
||||
const blob = await FileToBlob(file)
|
||||
|
||||
expect(blob).toBeInstanceOf(Blob)
|
||||
const text = await BlobToString(blob)
|
||||
expect(text).toBe(content)
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
@@ -7,11 +7,13 @@ function FileToBlob(file: File): Promise<Blob> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => {
|
||||
resolve(new Blob([reader.result as ArrayBuffer]))
|
||||
const arrayBuffer = reader.result as ArrayBuffer
|
||||
resolve(new Blob([arrayBuffer]))
|
||||
}
|
||||
reader.onerror = reject
|
||||
reader.readAsArrayBuffer(file)
|
||||
})
|
||||
}
|
||||
|
||||
export { FileToBlob }
|
||||
|
||||
export { FileToBlob }
|
||||
10
src/base64/base64-decode-unicode.test.ts
Normal file
10
src/base64/base64-decode-unicode.test.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { b64DecodeUnicode } from './base64-decode-unicode'
|
||||
|
||||
describe('b64DecodeUnicode', () => {
|
||||
it('should decode a base64 encoded Unicode string', () => {
|
||||
const encoded = '4pyTIMOgIGxhIG1vZGU='
|
||||
const decoded = b64DecodeUnicode(encoded)
|
||||
expect(decoded).toBe('✓ à la mode')
|
||||
})
|
||||
})
|
||||
10
src/base64/base64-encode-unicode.test.ts
Normal file
10
src/base64/base64-encode-unicode.test.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { b64EncodeUnicode } from './base64-encode-unicode'
|
||||
|
||||
describe('b64EncodeUnicode', () => {
|
||||
it('should encode a Unicode string to base64', () => {
|
||||
const str = '✓ à la mode'
|
||||
const encoded = b64EncodeUnicode(str)
|
||||
expect(encoded).toBe('4pyTIMOgIGxhIG1vZGU=')
|
||||
})
|
||||
})
|
||||
@@ -4,3 +4,4 @@ export { FileToBase64 } from './FileToBase64'
|
||||
export { FileToBlob } from './FileToBlob'
|
||||
export { b64DecodeUnicode } from './base64-decode-unicode'
|
||||
export { b64EncodeUnicode } from './base64-encode-unicode'
|
||||
export {BlobToString} from './BlobToString'
|
||||
|
||||
Reference in New Issue
Block a user