Skip to content

Commit

Permalink
Merge pull request #72 from dojyorin/dev
Browse files Browse the repository at this point in the history
feat: BASE64 DataURL.
  • Loading branch information
dojyorin authored Aug 4, 2023
2 parents 5e583b3 + 5abf4cf commit 02cbf2b
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 47 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# **Deno Simple Utility**
![actions:test](https://github.com/dojyorin/deno_simple_utility/actions/workflows/test.yaml/badge.svg)
![actions:release](https://github.com/dojyorin/deno_simple_utility/actions/workflows/release.yaml/badge.svg)
![shields:license](https://img.shields.io/github/license/dojyorin/deno_simple_utility)
![shields:release](https://img.shields.io/github/release/dojyorin/deno_simple_utility)

Useful snippet collection.

Expand Down
6 changes: 3 additions & 3 deletions deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {assertEquals} from "https://deno.land/std@0.196.0/assert/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.196.0/path/mod.ts";
export {exists} from "https://deno.land/std@0.196.0/fs/mod.ts";
export {assertEquals} from "https://deno.land/std@0.197.0/assert/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.197.0/path/mod.ts";
export {exists} from "https://deno.land/std@0.197.0/fs/mod.ts";
6 changes: 3 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {dirname, fromFileUrl} from "https://deno.land/std@0.196.0/path/mod.ts";
export {Logger, handlers} from "https://deno.land/std@0.196.0/log/mod.ts";
export {format} from "https://deno.land/std@0.196.0/datetime/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.197.0/path/mod.ts";
export {Logger, handlers} from "https://deno.land/std@0.197.0/log/mod.ts";
export {format} from "https://deno.land/std@0.197.0/datetime/mod.ts";
1 change: 0 additions & 1 deletion mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import "./test/blob.test.ts";
import "./test/crypto.test.ts";
import "./test/deep.test.ts";
import "./test/deflate.test.ts";
import "./test/evaluate.test.ts";
import "./test/fetch.test.ts";
import "./test/import.test.ts";
import "./test/json.deno.test.ts";
Expand Down
1 change: 0 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from "./src/blob.ts";
export * from "./src/crypto.ts";
export * from "./src/deep.ts";
export * from "./src/deflate.ts";
export * from "./src/evaluate.ts";
export * from "./src/fetch.ts";
export * from "./src/import.ts";
export * from "./src/minipack.ts";
Expand Down
1 change: 0 additions & 1 deletion mod.universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from "./src/blob.ts";
export * from "./src/crypto.ts";
export * from "./src/deep.ts";
export * from "./src/deflate.ts";
export * from "./src/evaluate.ts";
export * from "./src/fetch.ts";
export * from "./src/import.ts";
export * from "./src/minipack.ts";
Expand Down
13 changes: 13 additions & 0 deletions src/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ export function base64Encode(data:Uint8Array):string{
*/
export function base64Decode(data:string):Uint8Array{
return new Uint8Array([...atob(data)].map(s => s.charCodeAt(0)));
}

/**
* Convert from binary to base64 encoded DataURL.
* Default MIME type is `application/octet-stream`.
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const url = base64DataURL(bin);
* ```
*/
export function base64DataURL(data:Uint8Array, mime?:string):string{
return `data:${mime ?? "application/octet-stream"};base64,${base64Encode(data)}`;
}
21 changes: 0 additions & 21 deletions src/evaluate.ts

This file was deleted.

31 changes: 25 additions & 6 deletions test/base64.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
import {assertEquals} from "../deps.test.ts";
import {base64Encode, base64Decode} from "../src/base64.ts";
import {base64Encode, base64Decode, base64DataURL} from "../src/base64.ts";

const sample = new Uint8Array([
const sample1 = new Uint8Array([
0x58, 0x0D, 0xC7, 0x64, 0x21, 0x42, 0x27, 0x76,
0x2E, 0xA6, 0xFE, 0x9E, 0x58, 0xA3, 0x93, 0x9A,
0x9A, 0x07, 0x57, 0xAE, 0x4E, 0x5B, 0x2F, 0xC7,
0x7A, 0xEF, 0xD7, 0xAF, 0xF5, 0x1F, 0x2A, 0x3A
]);

const sample2 = "WA3HZCFCJ3Yupv6eWKOTmpoHV65OWy/Heu/Xr/UfKjo=";

Deno.test({
name: "Base64: Encode",
fn(){
const encode = base64Encode(sample1);

assertEquals(encode, sample2);
}
});

Deno.test({
name: "Base64: Decode",
fn(){
const decode = base64Decode(sample2);

assertEquals(decode, sample1);
}
});

Deno.test({
name: "Base64: Encode and Decode",
name: "Base64: DataURL",
fn(){
const encode = base64Encode(sample);
const decode = base64Decode(encode);
const encode = base64DataURL(sample1);

assertEquals(decode, sample);
assertEquals(encode, `data:application/octet-stream;base64,${sample2}`);
}
});
11 changes: 0 additions & 11 deletions test/evaluate.test.ts

This file was deleted.

0 comments on commit 02cbf2b

Please sign in to comment.