Skip to content

Commit

Permalink
purge.
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed May 23, 2024
1 parent da5cfb7 commit eceae35
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 58 deletions.
13 changes: 0 additions & 13 deletions src/pure/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ export function base64Encode(data:Uint8Array):string{
return btoa(Array.from(data, v => String.fromCharCode(v)).join(""));
}

/**
* Convert from binary to base64 encoded DataURL.
* Default MIME type is `application/octet-stream`.
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const data = base64EncodeDataURL(bin);
* ```
*/
export function base64EncodeDataURL(data:Uint8Array, type?:string):string{
return `data:${type ?? "application/octet-stream"};base64,${base64Encode(data)}`;
}

/**
* Convert from base64 encoded string to binary.
* @example
Expand Down
14 changes: 7 additions & 7 deletions src/pure/minipack.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {textEncode, textDecode} from "./text.ts";

const MINIPACK_NAME = 1;
const MINIPACK_BODY = 4;
const NAME_SIZE = 1;
const BODY_SIZE = 4;

/**
* Simple name and body pair.
Expand All @@ -25,17 +25,17 @@ export interface DataMap {
* ```
*/
export function minipackEncode(files:DataMap[]):Uint8Array{
const archive = new Uint8Array(files.reduce((size, {name, body}) => size + MINIPACK_NAME + MINIPACK_BODY + textEncode(name).byteLength + body.byteLength, 0));
const archive = new Uint8Array(files.reduce((size, {name, body}) => size + NAME_SIZE + BODY_SIZE + textEncode(name).byteLength + body.byteLength, 0));

let i = 0;
for(const {name, body} of files){
const u8name = textEncode(name);

new DataView(archive.buffer, i).setUint8(0, u8name.byteLength);
i += MINIPACK_NAME;
i += NAME_SIZE;

new DataView(archive.buffer, i).setUint32(0, body.byteLength);
i += MINIPACK_BODY;
i += BODY_SIZE;

archive.set(u8name, i);
i += u8name.byteLength;
Expand Down Expand Up @@ -65,10 +65,10 @@ export function minipackDecode(archive:Uint8Array):DataMap[]{

for(let i = 0; i < archive.byteLength;){
const ns = new DataView(archive.buffer, i).getUint8(0);
i += MINIPACK_NAME;
i += NAME_SIZE;

const bs = new DataView(archive.buffer, i).getUint32(0);
i += MINIPACK_BODY;
i += BODY_SIZE;

files.push({
name: textDecode(archive.subarray(i, i += ns)),
Expand Down
19 changes: 3 additions & 16 deletions src/pure/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,16 @@ export function textEncode(data:string):Uint8Array{
}

/**
* Convert from UTF-8 binary to string.
* Convert from encoded binary to string.
* @example
* ```ts
* const text = "HelloWorld!";
* const encode = textEncode(text);
* const decode = textDecode(encode);
* ```
*/
export function textDecode(data:Uint8Array):string{
return new TextDecoder().decode(data);
}

/**
* Convert from any encoded binary to string.
* Default codec is SHIFT-JIS.
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const decode = textDecodeAny(bin);
* ```
*/
export function textDecodeAny(data:Uint8Array, codec?:string):string{
return new TextDecoder(codec ?? "shift-jis").decode(data);
export function textDecode(data:Uint8Array, codec?:string):string{
return new TextDecoder(codec).decode(data);
}

/**
Expand Down
11 changes: 1 addition & 10 deletions test/pure/base64.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals} from "../../deps.test.ts";
import {base64Encode, base64EncodeDataURL, base64Decode} from "../../src/pure/base64.ts";
import {base64Encode, base64Decode} from "../../src/pure/base64.ts";

const sample1 = new Uint8Array([
0x58, 0x0D, 0xC7, 0x64, 0x21, 0x42, 0x27, 0x76,
Expand All @@ -19,15 +19,6 @@ Deno.test({
}
});

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

assertEquals(encode, `data:application/octet-stream;base64,${sample2}`);
}
});

Deno.test({
name: "Base64: Decode",
fn(){
Expand Down
13 changes: 1 addition & 12 deletions test/pure/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals} from "../../deps.test.ts";
import {textEncode, textDecode, textDecodeAny, textHexEncode, textHexDecode, textPurgeSuperfluous, textFixWidth, textGetReady, textSplitBySegment, textPadZero} from "../../src/pure/text.ts";
import {textEncode, textDecode, textHexEncode, textHexDecode, textPurgeSuperfluous, textFixWidth, textGetReady, textSplitBySegment, textPadZero} from "../../src/pure/text.ts";

const sampleText = " Lorem ipsum\r dolor sit \r\r amet. ";
const sampleBin = new Uint8Array([
Expand All @@ -10,8 +10,6 @@ const sampleBin = new Uint8Array([
0x65, 0x74, 0x2E, 0x20
]);

const sjisBin = new Uint8Array([0x82, 0xB1, 0x82, 0xF1, 0x82, 0xC9, 0x82, 0xBF, 0x82, 0xCD]);

Deno.test({
name: "Text: UTF-8 Encode and Decode",
fn(){
Expand All @@ -22,15 +20,6 @@ Deno.test({
}
});

Deno.test({
name: "Text: Any Text Decode",
fn(){
const decode = textDecodeAny(sjisBin);

assertEquals(decode, "こんにちは");
}
});

Deno.test({
name: "Text: HEX Encode and Decode",
fn(){
Expand Down

0 comments on commit eceae35

Please sign in to comment.