From 8d3ce68cf84ce9e14a7389edc095a5d716d212b6 Mon Sep 17 00:00:00 2001 From: dojyorin Date: Thu, 25 Apr 2024 04:10:32 +0900 Subject: [PATCH] text method refine --- src/pure/minipack.ts | 8 +++---- src/pure/text.ts | 54 +++++++++++++++++++++--------------------- src/pure/time.ts | 4 ++-- src/pure/worker.ts | 4 ++-- test/pure/text.test.ts | 22 ++++++++--------- 5 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/pure/minipack.ts b/src/pure/minipack.ts index 7b1d236..a11b8b0 100644 --- a/src/pure/minipack.ts +++ b/src/pure/minipack.ts @@ -1,4 +1,4 @@ -import {u8Encode, u8Decode} from "./text.ts"; +import {textEncode, textDecode} from "./text.ts"; const MINIPACK_NAME = 1; const MINIPACK_BODY = 4; @@ -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; @@ -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) }); } diff --git a/src/pure/text.ts b/src/pure/text.ts index edfada3..a6f4125 100644 --- a/src/pure/text.ts +++ b/src/pure/text.ts @@ -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); } @@ -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); } @@ -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); } @@ -42,12 +42,12 @@ 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(""); } /** @@ -55,11 +55,11 @@ export function hexEncode(data:Uint8Array):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 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}`)) ?? []); } @@ -67,10 +67,10 @@ export function hexDecode(data:string):Uint8Array{ * 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, ""); } @@ -78,10 +78,10 @@ export function trimExtend(data:string):string{ * 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({ "ヴ": "ヴ", "ガ": "ガ", "ギ": "ギ", "グ": "グ", "ゲ": "ゲ", "ゴ": "ゴ", @@ -114,14 +114,14 @@ 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)); } /** @@ -129,10 +129,10 @@ export function textAdjust(data:string):string{ * 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); } @@ -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"); } \ No newline at end of file diff --git a/src/pure/time.ts b/src/pure/time.ts index b1e5be0..3997c46 100644 --- a/src/pure/time.ts +++ b/src/pure/time.ts @@ -1,4 +1,4 @@ -import {padZero} from "./text.ts"; +import {textPadZero} from "./text.ts"; /** * Wait for specified time. @@ -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())}`; } \ No newline at end of file diff --git a/src/pure/worker.ts b/src/pure/worker.ts index 1170b0a..dad7351 100644 --- a/src/pure/worker.ts +++ b/src/pure/worker.ts @@ -1,5 +1,5 @@ import {base64DataURL} from "./base64.ts"; -import {u8Encode} from "./text.ts"; +import {textEncode} from "./text.ts"; interface TaskMessage{ message: T; @@ -45,7 +45,7 @@ export function workerTask(task:TaskAction return (message, transfers)=>{ return new Promise((res, rej)=>{ - const worker = new Worker(base64DataURL(u8Encode(script), "text/javascript"), { + const worker = new Worker(base64DataURL(textEncode(script), "text/javascript"), { type: "module" }); diff --git a/test/pure/text.test.ts b/test/pure/text.test.ts index 0fc6827..17fae98 100644 --- a/test/pure/text.test.ts +++ b/test/pure/text.test.ts @@ -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([ @@ -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); } @@ -25,7 +25,7 @@ Deno.test({ Deno.test({ name: "Text: Any Text Decode", fn(){ - const decode = textDecode(sjisBin); + const decode = textDecodeAny(sjisBin); assertEquals(decode, "こんにちは"); } @@ -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); } @@ -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."); } @@ -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"); } @@ -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"); } @@ -71,7 +71,7 @@ Deno.test({ Deno.test({ name: "Text: Segment", fn(){ - const {length} = splitSegment("😄😁😆😅😂"); + const {length} = textSplit("😄😁😆😅😂"); assertEquals(length, 5); } @@ -80,7 +80,7 @@ Deno.test({ Deno.test({ name: "Text: Pad 0", fn(){ - const pad = padZero(8); + const pad = textPadZero(8); assertEquals(pad, "08"); }