Skip to content

Commit

Permalink
BREAKING(std/encoding/hex): reorder encode & decode arguments (#6410)
Browse files Browse the repository at this point in the history
refactor to match other src/dst methods
  • Loading branch information
marcosc90 authored Jun 21, 2020
1 parent 0a81ec6 commit f24aab8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions std/encoding/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function encodedLen(n: number): number {
* @param dst
* @param src
*/
export function encode(dst: Uint8Array, src: Uint8Array): number {
export function encode(src: Uint8Array, dst: Uint8Array): number {
const srcLength = encodedLen(src.length);
if (dst.length !== srcLength) {
throw new Error("Out of index.");
Expand All @@ -67,7 +67,7 @@ export function encode(dst: Uint8Array, src: Uint8Array): number {
*/
export function encodeToString(src: Uint8Array): string {
const dest = new Uint8Array(encodedLen(src.length));
encode(dest, src);
encode(src, dest);
return new TextDecoder().decode(dest);
}

Expand All @@ -82,8 +82,8 @@ export function encodeToString(src: Uint8Array): string {
* @param src
*/
export function decode(
dst: Uint8Array,
src: Uint8Array
src: Uint8Array,
dst: Uint8Array
): [number, Error | void] {
let i = 0;
for (; i < Math.floor(src.length / 2); i++) {
Expand Down
10 changes: 5 additions & 5 deletions std/encoding/hex_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Deno.test({
const srcStr = "abc";
const src = new TextEncoder().encode(srcStr);
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(int, 6);
}
Expand All @@ -74,7 +74,7 @@ Deno.test({
const dest = new Uint8Array(2); // out of index
assertThrows(
(): void => {
encode(dest, src);
encode(src, dest);
},
Error,
"Out of index."
Expand All @@ -84,7 +84,7 @@ Deno.test({
for (const [enc, dec] of testCases) {
const dest = new Uint8Array(encodedLen(dec.length));
const src = new Uint8Array(dec as number[]);
const n = encode(dest, src);
const n = encode(src, dest);
assertEquals(dest.length, n);
assertEquals(new TextDecoder().decode(dest), enc);
}
Expand Down Expand Up @@ -125,7 +125,7 @@ Deno.test({
for (const [enc, dec] of cases) {
const dest = new Uint8Array(decodedLen(enc.length));
const src = new TextEncoder().encode(enc as string);
const [, err] = decode(dest, src);
const [, err] = decode(src, dest);
assertEquals(err, undefined);
assertEquals(Array.from(dest), Array.from(dec as number[]));
}
Expand All @@ -148,7 +148,7 @@ Deno.test({
fn(): void {
for (const [input, output, expectedErr] of errCases) {
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(
new TextDecoder("ascii").decode(out.slice(0, n)),
output as string
Expand Down

0 comments on commit f24aab8

Please sign in to comment.