diff --git a/src/pure/base64.ts b/src/pure/base64.ts index 465a54d..4153418 100644 --- a/src/pure/base64.ts +++ b/src/pure/base64.ts @@ -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 diff --git a/src/pure/minipack.ts b/src/pure/minipack.ts index d933aa3..9b0a479 100644 --- a/src/pure/minipack.ts +++ b/src/pure/minipack.ts @@ -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. @@ -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; @@ -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)), diff --git a/src/pure/text.ts b/src/pure/text.ts index b566720..907b146 100644 --- a/src/pure/text.ts +++ b/src/pure/text.ts @@ -12,7 +12,7 @@ export function textEncode(data:string):Uint8Array{ } /** -* Convert from UTF-8 binary to string. +* Convert from encoded binary to string. * @example * ```ts * const text = "HelloWorld!"; @@ -20,21 +20,8 @@ export function textEncode(data:string):Uint8Array{ * 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); } /** diff --git a/test/pure/base64.test.ts b/test/pure/base64.test.ts index 0ccbce3..63a765f 100644 --- a/test/pure/base64.test.ts +++ b/test/pure/base64.test.ts @@ -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, @@ -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(){ diff --git a/test/pure/text.test.ts b/test/pure/text.test.ts index 5477c7d..3edd9d9 100644 --- a/test/pure/text.test.ts +++ b/test/pure/text.test.ts @@ -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([ @@ -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(){ @@ -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(){