Skip to content

Commit

Permalink
text method refine
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Apr 24, 2024
1 parent 4953324 commit 8d3ce68
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
8 changes: 4 additions & 4 deletions src/pure/minipack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {u8Encode, u8Decode} from "./text.ts";
import {textEncode, textDecode} from "./text.ts";

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

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

new DataView(archive.buffer, i).setUint8(0, u8name.byteLength);
i += MINIPACK_NAME;
Expand Down Expand Up @@ -71,7 +71,7 @@ export function minipackDecode(archive:Uint8Array):DataMap[]{
i += MINIPACK_BODY;

files.push({
name: u8Decode(archive.subarray(i, i += ns)),
name: textDecode(archive.subarray(i, i += ns)),
body: archive.slice(i, i += bs)
});
}
Expand Down
54 changes: 27 additions & 27 deletions src/pure/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* @example
* ```ts
* const text = "HelloWorld!";
* const encode = u8Encode(text);
* const decode = u8Decode(encode);
* const encode = textEncode(text);
* const decode = textDecode(encode);
* ```
*/
export function u8Encode(data:string):Uint8Array{
export function textEncode(data:string):Uint8Array{
return new TextEncoder().encode(data);
}

Expand All @@ -16,11 +16,11 @@ export function u8Encode(data:string):Uint8Array{
* @example
* ```ts
* const text = "HelloWorld!";
* const encode = u8Encode(text);
* const decode = u8Decode(encode);
* const encode = textEncode(text);
* const decode = textDecode(encode);
* ```
*/
export function u8Decode(data:Uint8Array):string{
export function textDecode(data:Uint8Array):string{
return new TextDecoder().decode(data);
}

Expand All @@ -30,10 +30,10 @@ export function u8Decode(data:Uint8Array):string{
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const decode = textDecode(bin);
* const decode = textDecodeAny(bin);
* ```
*/
export function textDecode(data:Uint8Array, codec?:string):string{
export function textDecodeAny(data:Uint8Array, codec?:string):string{
return new TextDecoder(codec ?? "shift-jis").decode(data);
}

Expand All @@ -42,46 +42,46 @@ export function textDecode(data:Uint8Array, codec?:string):string{
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const encode = hexEncode(bin);
* const decode = hexDecode(encode);
* const encode = textEncodeHex(bin);
* const decode = textDecodeHex(encode);
* ```
*/
export function hexEncode(data:Uint8Array):string{
return [...data].map(v => padZero(v, 2, 16)).join("");
export function textEncodeHex(data:Uint8Array):string{
return [...data].map(v => textPadZero(v, 2, 16)).join("");
}

/**
* Convert from hex string to binary.
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const encode = hexEncode(bin);
* const decode = hexDecode(encode);
* const encode = textEncodeHex(bin);
* const decode = textDecodeHex(encode);
* ```
*/
export function hexDecode(data:string):Uint8Array{
export function textDecodeHex(data:string):Uint8Array{
return new Uint8Array(data.match(/[0-9a-fA-F]{2}/g)?.map(v => Number(`0x${v}`)) ?? []);
}

/**
* Trim head and tail blank, remove CR and consecutive space (tab, LF) to single space (tab, LF).
* @example
* ```ts
* const format = trimExtend(" Lorem ipsum\r dolor sit \r\r amet. ");
* const format = textPurgeSuperfluous(" Lorem ipsum\r dolor sit \r\r amet. ");
* ```
*/
export function trimExtend(data:string):string{
export function textPurgeSuperfluous(data:string):string{
return data.trim().replace(/\r/g, "").replace(/ +/g, " ").replace(/\t+/g, "\t").replace(/\n+/g, "\n").replace(/^ /mg, "").replace(/ $/mg, "");
}

/**
* Convert half-width string (ex: Japanese Kana) to full-width and full-width alphanumeric symbols to half-width.
* @example
* ```ts
* const format = fixWidth("1+1=2");
* const format = textFixWidth("1+1=2");
* ```
*/
export function fixWidth(data:string):string{
export function textFixWidth(data:string):string{
return Object.entries({
"ヴ": "ヴ",
"ガ": "ガ", "ギ": "ギ", "グ": "グ", "ゲ": "ゲ", "ゴ": "ゴ",
Expand Down Expand Up @@ -114,25 +114,25 @@ export function fixWidth(data:string):string{
}

/**
* Clean up text with `fixWidth()` and `trimExtend()`.
* Clean up text with `textFixWidth()` and `textPurgeSuperfluous()`.
* @example
* ```ts
* const format = textAdjust("1 + 1 = 2 ");
* const format = textGetReady("1 + 1 = 2 ");
* ```
*/
export function textAdjust(data:string):string{
return trimExtend(fixWidth(data));
export function textGetReady(data:string):string{
return textPurgeSuperfluous(textFixWidth(data));
}

/**
* Accurately recognize string that contain character above `0x010000` and array them one by character.
* Useful for calculate number of characters with string contains emoji.
* @example
* ```ts
* const characters = splitSegment("😀😃😄😁😆😅😂🤣");
* const characters = textSplit("😀😃😄😁😆😅😂🤣");
* ```
*/
export function splitSegment(data:string):string[]{
export function textSplit(data:string):string[]{
return [...new Intl.Segmenter().segment(data)].map(({segment}) => segment);
}

Expand All @@ -141,9 +141,9 @@ export function splitSegment(data:string):string[]{
* Output is 2 digits by default.
* @example
* ```ts
* const pad = padZero(8);
* const pad = textPadZero(8);
* ```
*/
export function padZero(data:number, digit?:number, radix?:number):string{
export function textPadZero(data:number, digit?:number, radix?:number):string{
return data.toString(radix).toUpperCase().padStart(digit ?? 2, "0");
}
4 changes: 2 additions & 2 deletions src/pure/time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {padZero} from "./text.ts";
import {textPadZero} from "./text.ts";

/**
* Wait for specified time.
Expand Down Expand Up @@ -68,5 +68,5 @@ export function timeSerial(date?:Date, split?:boolean):string{
const ss = split ? "/" : "";
const sc = split ? ":" : "";

return `${d.getFullYear()}${ss}${padZero(d.getMonth() + 1)}${ss}${padZero(d.getDate())}${split ? " " : ""}${padZero(d.getHours())}${sc}${padZero(d.getMinutes())}${sc}${padZero(d.getSeconds())}`;
return `${d.getFullYear()}${ss}${textPadZero(d.getMonth() + 1)}${ss}${textPadZero(d.getDate())}${split ? " " : ""}${textPadZero(d.getHours())}${sc}${textPadZero(d.getMinutes())}${sc}${textPadZero(d.getSeconds())}`;
}
4 changes: 2 additions & 2 deletions src/pure/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {base64DataURL} from "./base64.ts";
import {u8Encode} from "./text.ts";
import {textEncode} from "./text.ts";

interface TaskMessage<T extends unknown>{
message: T;
Expand Down Expand Up @@ -45,7 +45,7 @@ export function workerTask<T extends unknown, K extends unknown>(task:TaskAction

return (message, transfers)=>{
return new Promise<K>((res, rej)=>{
const worker = new Worker(base64DataURL(u8Encode(script), "text/javascript"), {
const worker = new Worker(base64DataURL(textEncode(script), "text/javascript"), {
type: "module"
});

Expand Down
22 changes: 11 additions & 11 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 {u8Encode, u8Decode, textDecode, hexEncode, hexDecode, trimExtend, fixWidth, textAdjust, splitSegment, padZero} from "../../src/pure/text.ts";
import {textEncode, textDecode, textDecodeAny, textEncodeHex, textDecodeHex, textPurgeSuperfluous, textFixWidth, textGetReady, textSplit, textPadZero} from "../../src/pure/text.ts";

const sampleText = " Lorem ipsum\r dolor sit \r\r amet. ";
const sampleBin = new Uint8Array([
Expand All @@ -15,8 +15,8 @@ const sjisBin = new Uint8Array([0x82, 0xB1, 0x82, 0xF1, 0x82, 0xC9, 0x82, 0xBF,
Deno.test({
name: "Text: UTF-8 Encode and Decode",
fn(){
const encode = u8Encode(sampleText);
const decode = u8Decode(encode);
const encode = textEncode(sampleText);
const decode = textDecode(encode);

assertEquals(decode, sampleText);
}
Expand All @@ -25,7 +25,7 @@ Deno.test({
Deno.test({
name: "Text: Any Text Decode",
fn(){
const decode = textDecode(sjisBin);
const decode = textDecodeAny(sjisBin);

assertEquals(decode, "こんにちは");
}
Expand All @@ -34,8 +34,8 @@ Deno.test({
Deno.test({
name: "Text: HEX Encode and Decode",
fn(){
const encode = hexEncode(sampleBin);
const decode = hexDecode(encode);
const encode = textEncodeHex(sampleBin);
const decode = textDecodeHex(encode);

assertEquals(decode, sampleBin);
}
Expand All @@ -44,7 +44,7 @@ Deno.test({
Deno.test({
name: "Text: Trim",
fn(){
const result = trimExtend(sampleText);
const result = textPurgeSuperfluous(sampleText);

assertEquals(result, "Lorem ipsum dolor sit amet.");
}
Expand All @@ -53,7 +53,7 @@ Deno.test({
Deno.test({
name: "Text: Fix Width",
fn(){
const result = fixWidth("1+1=2");
const result = textFixWidth("1+1=2");

assertEquals(result, "1+1=2");
}
Expand All @@ -62,7 +62,7 @@ Deno.test({
Deno.test({
name: "Text: Clean Up",
fn(){
const result = textAdjust("1 + 1 = 2 ");
const result = textGetReady("1 + 1 = 2 ");

assertEquals(result, "1 + 1 = 2");
}
Expand All @@ -71,7 +71,7 @@ Deno.test({
Deno.test({
name: "Text: Segment",
fn(){
const {length} = splitSegment("😄😁😆😅😂");
const {length} = textSplit("😄😁😆😅😂");

assertEquals(length, 5);
}
Expand All @@ -80,7 +80,7 @@ Deno.test({
Deno.test({
name: "Text: Pad 0",
fn(){
const pad = padZero(8);
const pad = textPadZero(8);

assertEquals(pad, "08");
}
Expand Down

0 comments on commit 8d3ce68

Please sign in to comment.