mirror of
https://github.com/Warky-Devs/artemis-kit.git
synced 2026-02-01 23:44:27 +00:00
Initial commit
This commit is contained in:
16
src/base64/Base64ToBlob.ts
Normal file
16
src/base64/Base64ToBlob.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Converts a base64 string to a Blob object.
|
||||
* @param base64 - The base64 encoded string to convert
|
||||
* @returns A Blob containing the decoded binary data
|
||||
*/
|
||||
export function base64ToBlob(base64: string): Blob {
|
||||
const byteString = atob(base64)
|
||||
const arrayBuffer = new ArrayBuffer(byteString.length)
|
||||
const uint8Array = new Uint8Array(arrayBuffer)
|
||||
|
||||
for (let i = 0; i < byteString.length; i++) {
|
||||
uint8Array[i] = byteString.charCodeAt(i)
|
||||
}
|
||||
|
||||
return new Blob([arrayBuffer])
|
||||
}
|
||||
19
src/base64/BlobToBase64.ts
Normal file
19
src/base64/BlobToBase64.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Converts a Blob object to a base64 encoded string.
|
||||
* @param blob - The Blob object to convert
|
||||
* @returns Promise that resolves with the base64 encoded string
|
||||
*/
|
||||
function blobToBase64(blob: Blob): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => {
|
||||
const dataUrl = (reader.result ?? '') as string
|
||||
const base64 = dataUrl?.split?.(',')?.[1]
|
||||
resolve(base64)
|
||||
}
|
||||
reader.onerror = reject
|
||||
reader.readAsDataURL(blob)
|
||||
})
|
||||
}
|
||||
|
||||
export { blobToBase64 }
|
||||
14
src/base64/BlobToString.ts
Normal file
14
src/base64/BlobToString.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Converts a Blob object to a string. Defaults to a blank string if the Blob is null.
|
||||
* @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()
|
||||
}
|
||||
|
||||
export { blobToString }
|
||||
18
src/base64/FileToBase64.ts
Normal file
18
src/base64/FileToBase64.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Converts a File object to a base64 encoded string.
|
||||
* @param file - The File object to convert
|
||||
* @returns Promise that resolves with the base64 encoded string
|
||||
*/
|
||||
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 ?? ''))
|
||||
}
|
||||
reader.onerror = reject
|
||||
reader.readAsArrayBuffer(file)
|
||||
})
|
||||
}
|
||||
|
||||
export { FileToBase64 }
|
||||
17
src/base64/FileToBlob.ts
Normal file
17
src/base64/FileToBlob.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Converts a File object to a Blob object.
|
||||
* @param file - The File object to convert
|
||||
* @returns Promise that resolves with the Blob
|
||||
*/
|
||||
function FileToBlob(file: File): Promise<Blob> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => {
|
||||
resolve(new Blob([reader.result as ArrayBuffer]))
|
||||
}
|
||||
reader.onerror = reject
|
||||
reader.readAsArrayBuffer(file)
|
||||
})
|
||||
}
|
||||
|
||||
export { FileToBlob }
|
||||
20
src/base64/base64-decode-unicode.ts
Normal file
20
src/base64/base64-decode-unicode.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Decodes a base64 string that contains Unicode characters.
|
||||
* This function handles Unicode characters correctly by:
|
||||
* 1. Converting base64 to binary
|
||||
* 2. Converting each byte to a hex representation
|
||||
* 3. Converting hex-encoded UTF-8 sequences back to Unicode characters
|
||||
*
|
||||
* @param str - The base64 encoded string to decode
|
||||
* @returns The decoded Unicode string
|
||||
*/
|
||||
export function b64DecodeUnicode(str: string): any {
|
||||
return decodeURIComponent(
|
||||
Array.prototype.map
|
||||
.call(
|
||||
atob(str),
|
||||
(c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`
|
||||
)
|
||||
.join('')
|
||||
)
|
||||
}
|
||||
17
src/base64/base64-encode-unicode.ts
Normal file
17
src/base64/base64-encode-unicode.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Encodes a Unicode string to base64.
|
||||
* This function handles Unicode characters correctly by:
|
||||
* 1. Converting Unicode to UTF-8 percent-encoding
|
||||
* 2. Converting percent-encoded bytes to binary
|
||||
* 3. Converting binary to base64
|
||||
*
|
||||
* @param str - The Unicode string to encode
|
||||
* @returns The base64 encoded string
|
||||
*/
|
||||
export function b64EncodeUnicode(str: any): string {
|
||||
return btoa(
|
||||
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_match, p1) =>
|
||||
String.fromCharCode(Number.parseInt(p1, 16))
|
||||
)
|
||||
)
|
||||
}
|
||||
6
src/base64/index.ts
Normal file
6
src/base64/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export { base64ToBlob } from './Base64ToBlob'
|
||||
export { blobToBase64 } from './BlobToBase64'
|
||||
export { FileToBase64 } from './FileToBase64'
|
||||
export { FileToBlob } from './FileToBlob'
|
||||
export { b64DecodeUnicode } from './base64-decode-unicode'
|
||||
export { b64EncodeUnicode } from './base64-encode-unicode'
|
||||
Reference in New Issue
Block a user