Skip to content

Commit

Permalink
add fix width.
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Jul 25, 2023
1 parent 80e99c8 commit 2c4577c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
52 changes: 52 additions & 0 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,58 @@ export function trimExtend(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 Japanese kana to full-width and full-width alphanumeric symbols to half-width.
* @example
* ```ts
* const text = "1+1=2";
* const formated = fixWidth(text);
* ```
*/
export function fixWidth(data:string){
return Object.entries({
"ヴ": "ヴ",
"ガ": "ガ", "ギ": "ギ", "グ": "グ", "ゲ": "ゲ", "ゴ": "ゴ",
"ザ": "ザ", "ジ": "ジ", "ズ": "ズ", "ゼ": "ゼ", "ゾ": "ゾ",
"ダ": "ダ", "ヂ": "ヂ", "ヅ": "ヅ", "デ": "デ", "ド": "ド",
"バ": "バ", "ビ": "ビ", "ブ": "ブ", "ベ": "ベ", "ボ": "ボ",
"パ": "パ", "ピ": "ピ", "プ": "プ", "ペ": "ペ", "ポ": "ポ",
"ア": "ア", "イ": "イ", "ウ": "ウ", "エ": "エ", "オ": "オ",
"カ": "カ", "キ": "キ", "ク": "ク", "ケ": "ケ", "コ": "コ",
"サ": "サ", "シ": "シ", "ス": "ス", "セ": "セ", "ソ": "ソ",
"タ": "タ", "チ": "チ", "ツ": "ツ", "テ": "テ", "ト": "ト",
"ナ": "ナ", "ニ": "ニ", "ヌ": "ヌ", "ネ": "ネ", "ノ": "ノ",
"ハ": "ハ", "ヒ": "ヒ", "フ": "フ", "ヘ": "ヘ", "ホ": "ホ",
"マ": "マ", "ミ": "ミ", "ム": "ム", "メ": "メ", "モ": "モ",
"ヤ": "ヤ", "ユ": "ユ", "ヨ": "ヨ",
"ラ": "ラ", "リ": "リ", "ル": "ル", "レ": "レ", "ロ": "ロ",
"ワ": "ワ", "ヲ": "ヲ", "ン": "ン",
"ァ": "ァ", "ィ": "ィ", "ゥ": "ゥ", "ェ": "ェ", "ォ": "ォ",
"ッ": "ッ",
"ャ": "ャ", "ュ": "ュ", "ョ": "ョ",
"、": "、", "。": "。", "・": "・", "ー": "ー", "「": "「", "」": "」",
"A": "A", "B": "B", "C": "C", "D": "D", "E": "E", "F": "F", "G": "G", "H": "H", "I": "I", "J": "J", "K": "K", "L": "L", "M": "M",
"N": "N", "O": "O", "P": "P", "Q": "Q", "R": "R", "S": "S", "T": "T", "U": "U", "V": "V", "W": "W", "X": "X", "Y": "Y", "Z": "Z",
"a": "a", "b": "b", "c": "c", "d": "d", "e": "e", "f": "f", "g": "g", "h": "h", "i": "i", "j": "j", "k": "k", "l": "l", "m": "m",
"n": "n", "o": "o", "p": "p", "q": "q", "r": "r", "s": "s", "t": "t", "u": "u", "v": "v", "w": "w", "x": "x", "y": "y", "z": "z",
"0": "0", "1": "1", "2": "2", "3": "3", "4": "4", "5": "5", "6": "6", "7": "7", "8": "8", "9": "9",
"!": "!", """: "\"", "#": "#", "$": "$", "%": "%", "&": "&", "'": "'", "(": "(", ")": ")", "*": "*", "+": "+", ",": ",", "-": "-", ".": ".", "/": "/", ":": ":",
";": ";", "<": "<", "=": "=", ">": ">", "?": "?", "@": "@", "[": "[", "\": "\\", "]": "]", "^": "^", "_": "_", "`": "`", "{": "{", "|": "|", "}": "}", "~": "~", " ": " "
}).reduce((text, [k, v]) => text.replace(new RegExp(k, "g"), v), data);
}

/**
* Clean up text with `fixWidth()` and `trimExtend()`.
* @example
* ```ts
* const text = "1 + 1 = 2 ";
* const formated = cleanText(text);
* ```
*/
export function cleanText(data:string){
return trimExtend(fixWidth(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.
Expand Down
4 changes: 2 additions & 2 deletions test/platform.deno.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {isWin} from "../src/platform.deno.ts";
Deno.test({
ignore: Deno.build.os !== "windows",
name: "Platform: Windows",
async fn(){
fn(){
assertEquals(isWin(), true);
}
});

Deno.test({
ignore: Deno.build.os === "windows",
name: "Platform: Posix",
async fn(){
fn(){
assertEquals(isWin(), false);
}
});
28 changes: 23 additions & 5 deletions test/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals} from "../deps.test.ts";
import {utfEncode, utfDecode, hexEncode, hexDecode, trimExtend, accurateSegment} from "../src/text.ts";
import {utfEncode, utfDecode, hexEncode, hexDecode, trimExtend, fixWidth, cleanText, accurateSegment} from "../src/text.ts";

const sampleText = " Lorem ipsum\r dolor sit \r\r amet. ";
const sampleBin = new Uint8Array([
Expand All @@ -12,7 +12,7 @@ const sampleBin = new Uint8Array([

Deno.test({
name: "Text: UTF8 Encode and Decode",
async fn(){
fn(){
const encode = utfEncode(sampleText);
const decode = utfDecode(encode);

Expand All @@ -22,7 +22,7 @@ Deno.test({

Deno.test({
name: "Text: HEX Encode and Decode",
async fn(){
fn(){
const encode = hexEncode(sampleBin);
const decode = hexDecode(encode);

Expand All @@ -32,16 +32,34 @@ Deno.test({

Deno.test({
name: "Text: Trim",
async fn(){
fn(){
const result = trimExtend(sampleText);

assertEquals(result, "Lorem ipsum dolor sit amet.");
}
});

Deno.test({
name: "Text: Fix Width",
fn(){
const result = fixWidth("1+1=2");

assertEquals(result, "1+1=2");
}
});

Deno.test({
name: "Text: Clean Up",
fn(){
const result = cleanText("1 + 1 = 2 ");

assertEquals(result, "1 + 1 = 2");
}
});

Deno.test({
name: "Text: Segment",
async fn(){
fn(){
const {length} = accurateSegment("😄😁😆😅😂");

assertEquals(length, 5);
Expand Down
4 changes: 2 additions & 2 deletions test/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const sample = new Date(2000, 0, 1, 0, 0, 0, 0);

Deno.test({
name: "Date: Encode and Decode",
async fn(){
fn(){
const encode = unixtimeEncode(sample);
const decode = unixtimeDecode(encode);

Expand All @@ -15,7 +15,7 @@ Deno.test({

Deno.test({
name: "Date: Parse",
async fn(){
fn(){
const result = unixtimeParse(sample.toISOString());

assertEquals(result, 946684800);
Expand Down

0 comments on commit 2c4577c

Please sign in to comment.