Implement misc and tests
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import {Result} from 'result'
|
||||
|
||||
export function a2str(buf: Uint8Array) : string {
|
||||
// @ts-ignore
|
||||
return String.fromCharCode.apply(null, buf)
|
||||
}
|
||||
export function str2a(str: string) : Uint8Array {
|
||||
const buf = new Uint8Array(str.length)
|
||||
|
||||
for (let i= 0, strLen = str.length; i < strLen; i++) {
|
||||
buf[i] = str.charCodeAt(i)
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
export function a2b64(buf: Uint8Array) : string {
|
||||
return btoa(a2str(buf))
|
||||
}
|
||||
export function b642a(b64: string) : Result<Uint8Array> {
|
||||
try {
|
||||
return Result.ok(str2a(atob(b64)))
|
||||
} catch (e) {}
|
||||
|
||||
return Result.error([])
|
||||
}
|
||||
Reference in New Issue
Block a user