Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: parseCidSafe in lex

+49 -12
+32 -11
lex/data/cid.ts
··· 20 20 toString(): string; 21 21 } 22 22 23 + export interface CidParseOptions { 24 + strict?: boolean; 25 + } 26 + 23 27 export function asCid(value: unknown): Cid | null { 24 28 return CID.asCID(value) as Cid | null; 25 29 } 26 30 27 - export function parseCid(input: string): Cid { 28 - return CID.parse(input) as Cid; 31 + export function parseCid(input: string, options?: CidParseOptions): Cid { 32 + const cid = CID.parse(input) as Cid; 33 + if (!isCid(cid, options)) { 34 + throw new Error(`Invalid CID string`); 35 + } 36 + return cid; 37 + } 38 + 39 + export function parseCidSafe( 40 + input: string, 41 + options?: CidParseOptions, 42 + ): Cid | null { 43 + try { 44 + return parseCid(input, options); 45 + } catch { 46 + return null; 47 + } 29 48 } 30 49 31 50 export function decodeCid(bytes: Uint8Array): Cid { ··· 38 57 39 58 export function isCid( 40 59 value: unknown, 41 - options?: { strict?: boolean }, 60 + options?: CidParseOptions, 42 61 ): value is Cid { 43 62 const cid = asCid(value); 44 63 if (!cid) return false; ··· 54 73 return true; 55 74 } 56 75 57 - export function validateCidString(input: string): boolean { 58 - try { 59 - return parseCid(input).toString() === input; 60 - } catch { 61 - return false; 62 - } 76 + export function validateCidString( 77 + input: string, 78 + options?: CidParseOptions, 79 + ): boolean { 80 + return parseCidSafe(input, options)?.toString() === input; 63 81 } 64 82 65 - export function ensureValidCidString(input: string): void { 66 - if (!validateCidString(input)) { 83 + export function ensureValidCidString( 84 + input: string, 85 + options?: CidParseOptions, 86 + ): void { 87 + if (!validateCidString(input, options)) { 67 88 throw new Error(`Invalid CID string`); 68 89 } 69 90 }
+1 -1
lex/deno.json
··· 1 1 { 2 2 "name": "@atp/lex", 3 - "version": "0.1.0-alpha.6", 3 + "version": "0.1.0-alpha.7", 4 4 "exports": { 5 5 ".": "./mod.ts", 6 6 "./cbor": "./cbor/mod.ts",
+1
lex/mod.ts
··· 2 2 3 3 export { l }; 4 4 export * from "./external.ts"; 5 + export { type CidParseOptions, parseCidSafe } from "./data/cid.ts"; 5 6 export * from "./json/mod.ts"; 6 7 7 8 if (import.meta.main) {
+15
lex/tests/cid_test.ts
··· 1 + import { parseCidSafe } from "@atp/lex"; 2 + import { parseCidSafe as parseCidSafeFromData } from "@atp/lex/data"; 3 + import { assertEquals } from "@std/assert"; 4 + 5 + const VALID_CID = "bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a"; 6 + 7 + Deno.test("safe CID parsers return parsed CID for valid input", () => { 8 + assertEquals(parseCidSafe(VALID_CID)?.toString(), VALID_CID); 9 + assertEquals(parseCidSafeFromData(VALID_CID)?.toString(), VALID_CID); 10 + }); 11 + 12 + Deno.test("safe CID parsers return null for invalid input", () => { 13 + assertEquals(parseCidSafe("not-a-cid"), null); 14 + assertEquals(parseCidSafeFromData("not-a-cid"), null); 15 + });