init: repo

This commit is contained in:
2024-03-14 15:44:56 +08:00
commit d1005bcd13
42 changed files with 5829 additions and 0 deletions

20
src/utils/converter.ts Normal file
View File

@@ -0,0 +1,20 @@
import { Codec } from '@/utils/codecs'
export class EncodingConverter {
async convert(codec: Codec, text: string): Promise<string | null> {
let convertedText: string | null = null
try {
const byteArray = new TextEncoder().encode(text)
const decodedText = new TextDecoder(codec.origin).decode(byteArray)
const byteArray2 = new TextEncoder().encode(decodedText)
convertedText = new TextDecoder(codec.target).decode(byteArray2)
text = convertedText
} catch (error) {
console.error(
`Failed to convert from ${codec.origin} to ${codec.target}: ${error}`
)
return null
}
return convertedText
}
}