BREAKING(encoding): replace Ascii85Options with EncodeAscii85Options and DecodeAscii85Options (#4861)

* refactor(encoding): deprecate `Ascii85Options`

* tweak

* tweak

* tweak

* tweaks
This commit is contained in:
Asher Gomez 2024-05-30 19:43:30 +10:00 committed by GitHub
parent c82e4e854f
commit f6857a88de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 9 deletions

View File

@ -25,11 +25,14 @@
import { validateBinaryLike } from "./_validate_binary_like.ts"; 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"; export type Ascii85Standard = "Adobe" | "btoa" | "RFC 1924" | "Z85";
/** Options for {@linkcode encodeAscii85} and {@linkcode decodeAscii85}. */ /** Options for {@linkcode encodeAscii85}. */
export interface Ascii85Options { export interface EncodeAscii85Options {
/** /**
* Character set and delimiter (if supported and used). * Character set and delimiter (if supported and used).
* *
@ -43,6 +46,7 @@ export interface Ascii85Options {
*/ */
delimiter?: boolean; delimiter?: boolean;
} }
const rfc1924 = const rfc1924 =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" as const; "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" as const;
const Z85 = const Z85 =
@ -66,11 +70,12 @@ const Z85 =
*/ */
export function encodeAscii85( export function encodeAscii85(
data: ArrayBuffer | Uint8Array | string, data: ArrayBuffer | Uint8Array | string,
options?: Ascii85Options, options: EncodeAscii85Options = {},
): string { ): string {
let uint8 = validateBinaryLike(data); let uint8 = validateBinaryLike(data);
const standard = options?.standard ?? "Adobe"; const { standard = "Adobe" } = options;
let output: string[] = []; let output: string[] = [];
let v: number; let v: number;
let n = 0; let n = 0;
@ -129,6 +134,9 @@ export function encodeAscii85(
return output.slice(0, output.length - difference).join(""); return output.slice(0, output.length - difference).join("");
} }
/** Options for {@linkcode decodeAscii85}. */
export type DecodeAscii85Options = Omit<EncodeAscii85Options, "delimiter">;
/** /**
* Decodes a ascii85-encoded string. * Decodes a ascii85-encoded string.
* *
@ -149,11 +157,12 @@ export function encodeAscii85(
*/ */
export function decodeAscii85( export function decodeAscii85(
ascii85: string, ascii85: string,
options?: Ascii85Options, options: DecodeAscii85Options = {},
): Uint8Array { ): 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") // translate all encodings to most basic adobe/btoa one and decompress some special characters ("z" and "y")
switch (encoding) { switch (standard) {
case "Adobe": case "Adobe":
ascii85 = ascii85.replaceAll(/(<~|~>)/g, "").replaceAll("z", "!!!!!"); ascii85 = ascii85.replaceAll(/(<~|~>)/g, "").replaceAll("z", "!!!!!");
break; break;

View File

@ -174,7 +174,6 @@ for (const [standard, tests] of Object.entries(testCasesDelimiter)) {
assertEquals( assertEquals(
decodeAscii85(b85 as string, { decodeAscii85(b85 as string, {
standard: standard as Ascii85Standard, standard: standard as Ascii85Standard,
delimiter: true,
}), }),
utf8encoder.encode(bin), utf8encoder.encode(bin),
); );