Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Apr 25, 2024
1 parent 8d3ce68 commit e4a6974
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions deps.pure_ext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @deno-types="https://deno.land/[email protected]/csv/mod.ts"
export {parse, stringify} from "https://esm.sh/gh/denoland/[email protected]/csv/mod.ts?bundle&target=esnext";
// @deno-types="https://deno.land/x/[email protected].41/index.d.ts"
export {ZipReader, ZipWriter, Uint8ArrayReader, Uint8ArrayWriter} from "https://esm.sh/gh/gildas-lormeau/[email protected].41/index.js?bundle&target=esnext";
// @deno-types="https://deno.land/x/[email protected].43/index.d.ts"
export {ZipReader, ZipWriter, Uint8ArrayReader, Uint8ArrayWriter} from "https://esm.sh/gh/gildas-lormeau/[email protected].43/index.js?bundle&target=esnext";

// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.2/package/types/index.d.ts"
export {type WorkBook as RawWorkBook, type WorkSheet as RawWorkSheet, type CellObject as RawWorkCell, set_cptable, read as xlsxRead, write as xlsxWrite, utils as xlsxUtil} from "https://cdn.sheetjs.com/xlsx-0.20.2/package/xlsx.mjs";
Expand Down
20 changes: 10 additions & 10 deletions src/pure/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ export function base64Encode(data:Uint8Array):string{
}

/**
* Convert from base64 encoded string to binary.
* Convert from binary to base64 encoded DataURL.
* Default MIME type is `application/octet-stream`.
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const encode = base64Encode(bin);
* const decode = base64Decode(encode);
* const data = base64EncodeDataURL(bin);
* ```
*/
export function base64Decode(data:string):Uint8Array{
return new Uint8Array([...atob(data)].map(v => v.charCodeAt(0)));
export function base64EncodeDataURL(data:Uint8Array, type?:string):string{
return `data:${type ?? "application/octet-stream"};base64,${base64Encode(data)}`;
}

/**
* Convert from binary to base64 encoded DataURL.
* Default MIME type is `application/octet-stream`.
* Convert from base64 encoded string to binary.
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const data = base64DataURL(bin);
* const encode = base64Encode(bin);
* const decode = base64Decode(encode);
* ```
*/
export function base64DataURL(data:Uint8Array, type?:string):string{
return `data:${type ?? "application/octet-stream"};base64,${base64Encode(data)}`;
export function base64Decode(data:string):Uint8Array{
return new Uint8Array([...atob(data)].map(v => v.charCodeAt(0)));
}
16 changes: 8 additions & 8 deletions src/pure/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export function textDecodeAny(data:Uint8Array, codec?:string):string{
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const encode = textEncodeHex(bin);
* const decode = textDecodeHex(encode);
* const encode = textHexEncode(bin);
* const decode = textHexDecode(encode);
* ```
*/
export function textEncodeHex(data:Uint8Array):string{
export function textHexEncode(data:Uint8Array):string{
return [...data].map(v => textPadZero(v, 2, 16)).join("");
}

Expand All @@ -55,11 +55,11 @@ export function textEncodeHex(data:Uint8Array):string{
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const encode = textEncodeHex(bin);
* const decode = textDecodeHex(encode);
* const encode = textHexEncode(bin);
* const decode = textHexDecode(encode);
* ```
*/
export function textDecodeHex(data:string):Uint8Array{
export function textHexDecode(data:string):Uint8Array{
return new Uint8Array(data.match(/[0-9a-fA-F]{2}/g)?.map(v => Number(`0x${v}`)) ?? []);
}

Expand Down Expand Up @@ -129,10 +129,10 @@ export function textGetReady(data:string):string{
* Useful for calculate number of characters with string contains emoji.
* @example
* ```ts
* const characters = textSplit("πŸ˜€πŸ˜ƒπŸ˜„πŸ˜πŸ˜†πŸ˜…πŸ˜‚πŸ€£");
* const characters = textSplitBySegment("πŸ˜€πŸ˜ƒπŸ˜„πŸ˜πŸ˜†πŸ˜…πŸ˜‚πŸ€£");
* ```
*/
export function textSplit(data:string):string[]{
export function textSplitBySegment(data:string):string[]{
return [...new Intl.Segmenter().segment(data)].map(({segment}) => segment);
}

Expand Down
4 changes: 2 additions & 2 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, base64Decode, base64DataURL} from "../../src/pure/base64.ts";
import {base64Encode, base64Decode, base64EncodeDataURL} from "../../src/pure/base64.ts";

const sample1 = new Uint8Array([
0x58, 0x0D, 0xC7, 0x64, 0x21, 0x42, 0x27, 0x76,
Expand Down Expand Up @@ -31,7 +31,7 @@ Deno.test({
Deno.test({
name: "Base64: DataURL",
fn(){
const encode = base64DataURL(sample1);
const encode = base64EncodeDataURL(sample1);

assertEquals(encode, `data:application/octet-stream;base64,${sample2}`);
}
Expand Down
8 changes: 4 additions & 4 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, textEncodeHex, textDecodeHex, textPurgeSuperfluous, textFixWidth, textGetReady, textSplit, textPadZero} from "../../src/pure/text.ts";
import {textEncode, textDecode, textDecodeAny, 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 Down Expand Up @@ -34,8 +34,8 @@ Deno.test({
Deno.test({
name: "Text: HEX Encode and Decode",
fn(){
const encode = textEncodeHex(sampleBin);
const decode = textDecodeHex(encode);
const encode = textHexEncode(sampleBin);
const decode = textHexDecode(encode);

assertEquals(decode, sampleBin);
}
Expand Down Expand Up @@ -71,7 +71,7 @@ Deno.test({
Deno.test({
name: "Text: Segment",
fn(){
const {length} = textSplit("πŸ˜„πŸ˜πŸ˜†πŸ˜…πŸ˜‚");
const {length} = textSplitBySegment("πŸ˜„πŸ˜πŸ˜†πŸ˜…πŸ˜‚");

assertEquals(length, 5);
}
Expand Down

0 comments on commit e4a6974

Please sign in to comment.