+4
-3
@@ -15,13 +15,14 @@ test('b642a and a2b64 are reciprocal', () => {
|
||||
const from_str = "Testing value !"
|
||||
const from = btoa(from_str)
|
||||
|
||||
let buff = b642a(from).expect("Should encode valid b64")
|
||||
let to = a2b64(buff)
|
||||
let buff = b642a(from)
|
||||
expect(buff).not.toBeNull()
|
||||
let to = a2b64(buff!)
|
||||
|
||||
expect(to).toBe(from)
|
||||
})
|
||||
|
||||
|
||||
test("b642a doesn't accept invalid b64", () => {
|
||||
b642a("pa'0ac'i !!").expect_err("Shouldn't unpack invalid b64")
|
||||
expect(b642a("pa'0ac'i !!")).toBeNull()
|
||||
})
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import {Result} from 'rust'
|
||||
import logger from 'log'
|
||||
|
||||
const log = logger('misc')
|
||||
@@ -10,7 +9,7 @@ export function a2str(buf: Uint8Array) : string {
|
||||
export function str2a(str: string) : Uint8Array {
|
||||
const buf = new Uint8Array(str.length)
|
||||
|
||||
for (let i= 0, strLen = str.length; i < strLen; i++) {
|
||||
for (let i = 0;i < str.length;i++) {
|
||||
buf[i] = str.charCodeAt(i)
|
||||
}
|
||||
|
||||
@@ -18,17 +17,18 @@ export function str2a(str: string) : Uint8Array {
|
||||
}
|
||||
|
||||
export function a2b64(buf: Uint8Array) : string {
|
||||
log.trace('a2b64')
|
||||
return btoa(a2str(buf))
|
||||
}
|
||||
export function b642a(b64: string) : Result<Uint8Array> {
|
||||
export function b642a(b64: string) : Uint8Array | null {
|
||||
log.trace('b642a')
|
||||
|
||||
try {
|
||||
return Result.ok(str2a(atob(b64)))
|
||||
return str2a(atob(b64))
|
||||
} catch (e) {
|
||||
log.warn('b64 decode failed')
|
||||
log.debug(`error : ${e}`)
|
||||
}
|
||||
|
||||
return Result.error([])
|
||||
return null
|
||||
}
|
||||
|
||||
+6
-8
@@ -2,22 +2,20 @@
|
||||
"name": "misc",
|
||||
"description": "Various utils",
|
||||
"version": "1.0.0",
|
||||
|
||||
"author": "Pascal Perrenoud <pascal@pband.ch>",
|
||||
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"files": ["index.ts"],
|
||||
|
||||
"scripts": {
|
||||
"test": "bun test"
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"log": "git+https://git.pband.ch/typescript/log.git",
|
||||
"rust": "git+https://git.pband.ch/typescript/rust.git"
|
||||
"log": "git+https://git.pband.ch/typescript/log.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "^1.0.11"
|
||||
}
|
||||
},
|
||||
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"files": ["index.ts"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user