0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

BREAKING(std/encoding/hex): reorder encode & decode arguments (#6410)

refactor to match other src/dst methods
This commit is contained in:
Marcos Casagrande 2020-06-21 06:13:54 +02:00 committed by GitHub
parent 0a81ec6b1e
commit f24aab81c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 9 deletions

View file

@ -48,7 +48,7 @@ export function encodedLen(n: number): number {
* @param dst * @param dst
* @param src * @param src
*/ */
export function encode(dst: Uint8Array, src: Uint8Array): number { export function encode(src: Uint8Array, dst: Uint8Array): number {
const srcLength = encodedLen(src.length); const srcLength = encodedLen(src.length);
if (dst.length !== srcLength) { if (dst.length !== srcLength) {
throw new Error("Out of index."); throw new Error("Out of index.");
@ -67,7 +67,7 @@ export function encode(dst: Uint8Array, src: Uint8Array): number {
*/ */
export function encodeToString(src: Uint8Array): string { export function encodeToString(src: Uint8Array): string {
const dest = new Uint8Array(encodedLen(src.length)); const dest = new Uint8Array(encodedLen(src.length));
encode(dest, src); encode(src, dest);
return new TextDecoder().decode(dest); return new TextDecoder().decode(dest);
} }
@ -82,8 +82,8 @@ export function encodeToString(src: Uint8Array): string {
* @param src * @param src
*/ */
export function decode( export function decode(
dst: Uint8Array, src: Uint8Array,
src: Uint8Array dst: Uint8Array
): [number, Error | void] { ): [number, Error | void] {
let i = 0; let i = 0;
for (; i < Math.floor(src.length / 2); i++) { for (; i < Math.floor(src.length / 2); i++) {

View file

@ -63,7 +63,7 @@ Deno.test({
const srcStr = "abc"; const srcStr = "abc";
const src = new TextEncoder().encode(srcStr); const src = new TextEncoder().encode(srcStr);
const dest = new Uint8Array(encodedLen(src.length)); const dest = new Uint8Array(encodedLen(src.length));
const int = encode(dest, src); const int = encode(src, dest);
assertEquals(src, new Uint8Array([97, 98, 99])); assertEquals(src, new Uint8Array([97, 98, 99]));
assertEquals(int, 6); assertEquals(int, 6);
} }
@ -74,7 +74,7 @@ Deno.test({
const dest = new Uint8Array(2); // out of index const dest = new Uint8Array(2); // out of index
assertThrows( assertThrows(
(): void => { (): void => {
encode(dest, src); encode(src, dest);
}, },
Error, Error,
"Out of index." "Out of index."
@ -84,7 +84,7 @@ Deno.test({
for (const [enc, dec] of testCases) { for (const [enc, dec] of testCases) {
const dest = new Uint8Array(encodedLen(dec.length)); const dest = new Uint8Array(encodedLen(dec.length));
const src = new Uint8Array(dec as number[]); const src = new Uint8Array(dec as number[]);
const n = encode(dest, src); const n = encode(src, dest);
assertEquals(dest.length, n); assertEquals(dest.length, n);
assertEquals(new TextDecoder().decode(dest), enc); assertEquals(new TextDecoder().decode(dest), enc);
} }
@ -125,7 +125,7 @@ Deno.test({
for (const [enc, dec] of cases) { for (const [enc, dec] of cases) {
const dest = new Uint8Array(decodedLen(enc.length)); const dest = new Uint8Array(decodedLen(enc.length));
const src = new TextEncoder().encode(enc as string); const src = new TextEncoder().encode(enc as string);
const [, err] = decode(dest, src); const [, err] = decode(src, dest);
assertEquals(err, undefined); assertEquals(err, undefined);
assertEquals(Array.from(dest), Array.from(dec as number[])); assertEquals(Array.from(dest), Array.from(dec as number[]));
} }
@ -148,7 +148,7 @@ Deno.test({
fn(): void { fn(): void {
for (const [input, output, expectedErr] of errCases) { for (const [input, output, expectedErr] of errCases) {
const out = new Uint8Array((input as string).length + 10); const out = new Uint8Array((input as string).length + 10);
const [n, err] = decode(out, new TextEncoder().encode(input as string)); const [n, err] = decode(new TextEncoder().encode(input as string), out);
assertEquals( assertEquals(
new TextDecoder("ascii").decode(out.slice(0, n)), new TextDecoder("ascii").decode(out.slice(0, n)),
output as string output as string