From cde473540a6612a080d999fcf4827f2daa5f6d1c Mon Sep 17 00:00:00 2001 From: Pascal Perrenoud Date: Tue, 14 May 2024 11:31:56 +0200 Subject: [PATCH] Implement misc and tests --- .gitignore | 6 ++++++ index.test.ts | 27 +++++++++++++++++++++++++++ index.ts | 26 ++++++++++++++++++++++++++ package.json | 16 ++++++++++++++++ tsconfig.json | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 index.test.ts create mode 100644 index.ts create mode 100644 package.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f39fcb6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea/ +._* +.DS_Store +bun.lockb +package-lock.json +node_modules/ diff --git a/index.test.ts b/index.test.ts new file mode 100644 index 0000000..0f33d54 --- /dev/null +++ b/index.test.ts @@ -0,0 +1,27 @@ +import {a2b64, a2str, b642a, str2a} from './index' + +import {test, expect} from 'bun:test' + +test('str2a and a2str are reciprocal', () => { + const from = "Testing value !" + + let buff = str2a(from) + let to = a2str(buff) + + expect(to).toBe(from) +}) + +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) + + expect(to).toBe(from) +}) + + +test("b642a doesn't accept invalid b64", () => { + b642a("pa'0ac'i !!").expect_err("Shouldn't unpack invalid b64") +}) diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..c6690b2 --- /dev/null +++ b/index.ts @@ -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 { + try { + return Result.ok(str2a(atob(b64))) + } catch (e) {} + + return Result.error([]) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..45c0c3e --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "misc", + "description": "Various utils", + "module": "index.ts", + "type": "module", + "files": ["index.ts"], + "dependencies": { + "result": "git+git@git.pband.ch/typescript/result" + }, + "devDependencies": { + "@types/bun": "^1.0.11" + }, + "peerDependencies": { + "typescript": "^5.0.0" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..84911b8 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,35 @@ +{ + "compilerOptions": { + "baseUrl": ".", + + // Enable latest features + "lib": [ + "ESNext", + "dom" + ], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "allowJs": false, + "checkJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + }, + "include": [ + "index.ts", + ] +}