mirror of
https://github.com/denoland/std.git
synced 2024-11-21 12:40:03 +00:00
BREAKING(encoding): replace Ascii85Options
with EncodeAscii85Options
and DecodeAscii85Options
(#4861)
* refactor(encoding): deprecate `Ascii85Options` * tweak * tweak * tweak * tweaks
This commit is contained in:
parent
c82e4e854f
commit
f6857a88de
@ -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<EncodeAscii85Options, "delimiter">;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
@ -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),
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user