mirror of
https://github.com/denoland/deno.git
synced 2024-10-29 08:58:01 -04:00
feat(std/encoding): add base64 (#5811)
This commit is contained in:
parent
2bbe475dbb
commit
f6e3160356
2 changed files with 88 additions and 0 deletions
41
std/encoding/base64.ts
Normal file
41
std/encoding/base64.ts
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts given data with base64 encoding
|
||||||
|
* @param data input to encode
|
||||||
|
*/
|
||||||
|
export function encode(data: string | ArrayBuffer): string {
|
||||||
|
if (typeof data === "string") {
|
||||||
|
return window.btoa(data);
|
||||||
|
} else {
|
||||||
|
const d = new Uint8Array(data);
|
||||||
|
let dataString = "";
|
||||||
|
for (let i = 0; i < d.length; ++i) {
|
||||||
|
dataString += String.fromCharCode(d[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return window.btoa(dataString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts given base64 encoded data back to original
|
||||||
|
* @param data input to decode
|
||||||
|
*/
|
||||||
|
export function decode(data: string): ArrayBuffer {
|
||||||
|
const binaryString = decodeString(data);
|
||||||
|
const binary = new Uint8Array(binaryString.length);
|
||||||
|
for (let i = 0; i < binary.length; ++i) {
|
||||||
|
binary[i] = binaryString.charCodeAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return binary.buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data assuming the output is in string type
|
||||||
|
* @param data input to decode
|
||||||
|
*/
|
||||||
|
export function decodeString(data: string): string {
|
||||||
|
return window.atob(data);
|
||||||
|
}
|
47
std/encoding/base64_test.ts
Normal file
47
std/encoding/base64_test.ts
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
const { test } = Deno;
|
||||||
|
import { assertEquals } from "../testing/asserts.ts";
|
||||||
|
import { encode, decode, decodeString } from "./base64.ts";
|
||||||
|
|
||||||
|
const testsetString = [
|
||||||
|
["", ""],
|
||||||
|
["f", "Zg=="],
|
||||||
|
["fo", "Zm8="],
|
||||||
|
["foo", "Zm9v"],
|
||||||
|
["foob", "Zm9vYg=="],
|
||||||
|
["fooba", "Zm9vYmE="],
|
||||||
|
["foobar", "Zm9vYmFy"],
|
||||||
|
];
|
||||||
|
|
||||||
|
const testsetBinary = [
|
||||||
|
[new TextEncoder().encode("\x00"), "AA=="],
|
||||||
|
[new TextEncoder().encode("\x00\x00"), "AAA="],
|
||||||
|
[new TextEncoder().encode("\x00\x00\x00"), "AAAA"],
|
||||||
|
[new TextEncoder().encode("\x00\x00\x00\x00"), "AAAAAA=="],
|
||||||
|
];
|
||||||
|
|
||||||
|
test("[encoding/base64] testBase64EncodeString", () => {
|
||||||
|
for (const [input, output] of testsetString) {
|
||||||
|
assertEquals(encode(input), output);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("[encoding/base64] testBase64DecodeString", () => {
|
||||||
|
for (const [input, output] of testsetString) {
|
||||||
|
assertEquals(decodeString(output), input);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("[encoding/base64] testBase64EncodeBinary", () => {
|
||||||
|
for (const [input, output] of testsetBinary) {
|
||||||
|
assertEquals(encode(input), output);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("[encoding/base64] testBase64DecodeBinary", () => {
|
||||||
|
for (const [input, output] of testsetBinary) {
|
||||||
|
const outputBinary = new Uint8Array(decode(output as string));
|
||||||
|
assertEquals(outputBinary, input as Uint8Array);
|
||||||
|
}
|
||||||
|
});
|
Loading…
Reference in a new issue