feat: finished 1.0.0
This commit is contained in:
@@ -1,18 +1,15 @@
|
||||
import { Codec } from '@/utils/codecs'
|
||||
import '@/utils/iconv'
|
||||
|
||||
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)
|
||||
const byteArray = iconv.encode(text, codec.origin)
|
||||
convertedText = iconv.decode(byteArray, codec.target)
|
||||
text = convertedText
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to convert from ${codec.origin} to ${codec.target}: ${error}`
|
||||
)
|
||||
console.error(`Failed to convert from ${codec.origin} to ${codec.target}: ${error}`)
|
||||
return null
|
||||
}
|
||||
return convertedText
|
||||
|
||||
Reference in New Issue
Block a user