From f6857a88dead44a41ce098c31c3bca4c7174e45b Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 30 May 2024 19:43:30 +1000 Subject: [PATCH] BREAKING(encoding): replace `Ascii85Options` with `EncodeAscii85Options` and `DecodeAscii85Options` (#4861) * refactor(encoding): deprecate `Ascii85Options` * tweak * tweak * tweak * tweaks --- encoding/ascii85.ts | 25 +++++++++++++++++-------- encoding/ascii85_test.ts | 1 - 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/encoding/ascii85.ts b/encoding/ascii85.ts index 821400c3e..1b89e233f 100644 --- a/encoding/ascii85.ts +++ b/encoding/ascii85.ts @@ -25,11 +25,14 @@ import { validateBinaryLike } from "./_validate_binary_like.ts"; -/** Supported ascii85 standards for {@linkcode Ascii85Options}. */ +/** + * Supported ascii85 standards for {@linkcode EncodeAscii85Options} and + * {@linkcode DecodeAscii85Options}. + */ export type Ascii85Standard = "Adobe" | "btoa" | "RFC 1924" | "Z85"; -/** Options for {@linkcode encodeAscii85} and {@linkcode decodeAscii85}. */ -export interface Ascii85Options { +/** Options for {@linkcode encodeAscii85}. */ +export interface EncodeAscii85Options { /** * Character set and delimiter (if supported and used). * @@ -43,6 +46,7 @@ export interface Ascii85Options { */ delimiter?: boolean; } + const rfc1924 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" as const; const Z85 = @@ -66,11 +70,12 @@ const Z85 = */ export function encodeAscii85( data: ArrayBuffer | Uint8Array | string, - options?: Ascii85Options, + options: EncodeAscii85Options = {}, ): string { let uint8 = validateBinaryLike(data); - const standard = options?.standard ?? "Adobe"; + const { standard = "Adobe" } = options; + let output: string[] = []; let v: number; let n = 0; @@ -129,6 +134,9 @@ export function encodeAscii85( return output.slice(0, output.length - difference).join(""); } +/** Options for {@linkcode decodeAscii85}. */ +export type DecodeAscii85Options = Omit; + /** * Decodes a ascii85-encoded string. * @@ -149,11 +157,12 @@ export function encodeAscii85( */ export function decodeAscii85( ascii85: string, - options?: Ascii85Options, + options: DecodeAscii85Options = {}, ): Uint8Array { - const encoding = options?.standard ?? "Adobe"; + const { standard = "Adobe" } = options; + // translate all encodings to most basic adobe/btoa one and decompress some special characters ("z" and "y") - switch (encoding) { + switch (standard) { case "Adobe": ascii85 = ascii85.replaceAll(/(<~|~>)/g, "").replaceAll("z", "!!!!!"); break; diff --git a/encoding/ascii85_test.ts b/encoding/ascii85_test.ts index 1e78c5bbb..9372bc590 100644 --- a/encoding/ascii85_test.ts +++ b/encoding/ascii85_test.ts @@ -174,7 +174,6 @@ for (const [standard, tests] of Object.entries(testCasesDelimiter)) { assertEquals( decodeAscii85(b85 as string, { standard: standard as Ascii85Standard, - delimiter: true, }), utf8encoder.encode(bin), );