From d74f3e2d43455debc9ce5a0bdd0b5a3279987853 Mon Sep 17 00:00:00 2001 From: Pascal Perrenoud Date: Mon, 9 Sep 2024 03:03:03 +0200 Subject: [PATCH] Get rid of Result --- index.test.ts | 7 ++++--- index.ts | 10 +++++----- package.json | 14 ++++++-------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/index.test.ts b/index.test.ts index 0f33d54..63668a8 100644 --- a/index.test.ts +++ b/index.test.ts @@ -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() }) diff --git a/index.ts b/index.ts index 07da2bf..7b572a9 100644 --- a/index.ts +++ b/index.ts @@ -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 { +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 } diff --git a/package.json b/package.json index 96f79c6..61c4046 100644 --- a/package.json +++ b/package.json @@ -2,22 +2,20 @@ "name": "misc", "description": "Various utils", "version": "1.0.0", - "author": "Pascal Perrenoud ", - "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"] }