Skip to content

Commit

Permalink
Merge pull request #65 from dojyorin/dev
Browse files Browse the repository at this point in the history
feat: import assert and fix width.
  • Loading branch information
dojyorin authored Jul 25, 2023
2 parents aca34a1 + 2c4577c commit 09ea5fa
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 45 deletions.
8 changes: 4 additions & 4 deletions deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {assertEquals} from "https://deno.land/std@0.194.0/testing/asserts.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.194.0/path/mod.ts";
export {serve} from "https://deno.land/std@0.194.0/http/mod.ts";
export {exists} from "https://deno.land/std@0.194.0/fs/mod.ts";
export {assertEquals} from "https://deno.land/std@0.195.0/testing/asserts.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.195.0/path/mod.ts";
export {serve} from "https://deno.land/std@0.195.0/http/mod.ts";
export {exists} from "https://deno.land/std@0.195.0/fs/mod.ts";
8 changes: 4 additions & 4 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {dirname, fromFileUrl} from "https://deno.land/std@0.194.0/path/mod.ts";
export {Logger} from "https://deno.land/std@0.194.0/log/mod.ts";
export {ConsoleHandler, FileHandler} from "https://deno.land/std@0.194.0/log/handlers.ts";
export {format as formatDate} from "https://deno.land/std@0.194.0/datetime/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.195.0/path/mod.ts";
export {Logger} from "https://deno.land/std@0.195.0/log/mod.ts";
export {ConsoleHandler, FileHandler} from "https://deno.land/std@0.195.0/log/handlers.ts";
export {format as formatDate} from "https://deno.land/std@0.195.0/datetime/mod.ts";
1 change: 1 addition & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "./test/crypto.test.ts";
import "./test/deep.test.ts";
import "./test/deflate.test.ts";
import "./test/fetch.test.ts";
import "./test/import.test.ts";
import "./test/json.deno.test.ts";
import "./test/log.deno.test.ts";
import "./test/minipack.test.ts";
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./src/crypto.ts";
export * from "./src/deep.ts";
export * from "./src/deflate.ts";
export * from "./src/fetch.ts";
export * from "./src/import.ts";
export * from "./src/minipack.ts";
export * from "./src/text.ts";
export * from "./src/time.ts";
Expand Down
1 change: 1 addition & 0 deletions mod.universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./src/crypto.ts";
export * from "./src/deep.ts";
export * from "./src/deflate.ts";
export * from "./src/fetch.ts";
export * from "./src/import.ts";
export * from "./src/minipack.ts";
export * from "./src/text.ts";
export * from "./src/time.ts";
15 changes: 15 additions & 0 deletions src/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Define import assertion on dynamic import.
* Default to JSON.
* @example
* ```ts
* const {default: json} = await import("./data.json", importAssert());
* ```
*/
export function importAssert(type?:string):ImportCallOptions{
return {
assert: {
type: type ?? "json"
}
};
}
9 changes: 5 additions & 4 deletions src/json.deno.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {type Opt} from "./deep.ts";
import {mainPath} from "./path.deno.ts";

/**
Expand All @@ -7,7 +8,7 @@ import {mainPath} from "./path.deno.ts";
* const object = await jsonRead("./resource.json");
* ```
*/
export async function jsonRead<T extends unknown>(path:string):Promise<T>{
export async function jsonRead<T extends Opt<T>>(path:string):Promise<T>{
return JSON.parse(await Deno.readTextFile(path));
}

Expand All @@ -20,7 +21,7 @@ export async function jsonRead<T extends unknown>(path:string):Promise<T>{
* });
* ```
*/
export async function jsonWrite<T extends unknown>(path:string, data:T):Promise<void>{
export async function jsonWrite<T extends Opt<T>>(path:string, data:T):Promise<void>{
await Deno.writeTextFile(path, JSON.stringify(data, undefined, 4));
}

Expand All @@ -34,7 +35,7 @@ export async function jsonWrite<T extends unknown>(path:string, data:T):Promise<
* const resource = await jsonLoad("./resource.json", dresource);
* ```
*/
export async function jsonLoad<T extends unknown>(path:string, def:T):Promise<T>{
export async function jsonLoad<T extends Opt<T>>(path:string, def:T):Promise<T>{
try{
return await jsonRead<T>(path);
}
Expand All @@ -59,6 +60,6 @@ export async function jsonLoad<T extends unknown>(path:string, def:T):Promise<T>
* const config = await configLoad(dconfig);
* ```
*/
export async function configLoad<T extends unknown>(def:T):Promise<T>{
export async function configLoad<T extends Opt<T>>(def:T):Promise<T>{
return await jsonLoad(`${mainPath()}/config.json`, def);
}
20 changes: 12 additions & 8 deletions src/log.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@ function logRecord(date:Date, level:string, message:string){

/**
* Instantiate logger with general configuration.
* Output to console and file.
* Log file default save path is `${Deno.mainModule}/operation.log`.
* Output to console and also to file if `name` is defined.
* Log file default save path is `${Deno.mainModule}/${name}.log`.
* @example
* ```ts
* const log = logEntry();
* ```
*/
export function logEntry(name?:string):Logger{
const title = name ?? "operation";
const level = "INFO";

const log = new Logger(title, level, {
const log = new Logger("operation", level, {
handlers: [
new ConsoleHandler(level, {
formatter({datetime, levelName, msg}){
return logRecord(datetime, levelName, msg);
}
}),
})
]
});

if(name){
log.handlers.push(...[
new FileHandler(level, {
filename: `${mainPath()}/${title}.log`,
filename: `${mainPath()}/${name}.log`,
formatter({datetime, levelName, msg}){
return logRecord(datetime, levelName, msg);
}
})
]
});
]);
}

for(const h of log.handlers){
h.setup();
Expand Down
12 changes: 6 additions & 6 deletions src/path.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {isWin} from "./platform.deno.ts";
* Useful for converting from Windows path to UNIX path.
* @example
* ```ts
* const path = slashUnix("C:\\file");
* const path = slashU("C:\\file");
* ```
*/
export function slashUnix(path:string):string{
export function slashU(path:string):string{
return path.replace(/\\/g, "/");
}

Expand All @@ -18,10 +18,10 @@ export function slashUnix(path:string):string{
* Useful for converting from UNIX path to Windows path.
* @example
* ```ts
* const path = slashWin("C:/file");
* const path = slashW("C:/file");
* ```
*/
export function slashWin(path:string):string{
export function slashW(path:string):string{
return path.replace(/\//g, "\\");
}

Expand Down Expand Up @@ -60,7 +60,7 @@ export function dataPath():string{
export function homePath():string{
const {HOME, USERPROFILE} = Deno.env.toObject();

return isWin() ? slashUnix(USERPROFILE) : HOME;
return isWin() ? slashU(USERPROFILE) : HOME;
}

/**
Expand All @@ -73,5 +73,5 @@ export function homePath():string{
export function mainPath():string{
const path = fromFileUrl(dirname(Deno.mainModule));

return isWin() ? slashUnix(path) : path;
return isWin() ? slashU(path) : path;
}
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
17 changes: 17 additions & 0 deletions test/import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {assertEquals} from "../deps.test.ts";
import {importAssert} from "../src/import.ts";

const expect = <const>{
assert: {
type: "json"
}
};

Deno.test({
name: "Import: Assert Option",
fn(){
const option = importAssert();

assertEquals(option, expect);
}
});
6 changes: 4 additions & 2 deletions test/log.deno.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {assertEquals, exists} from "../deps.test.ts";
import {logEntry} from "../src/log.deno.ts";

const name = "operation";

Deno.test({
name: "Log: Entry",
async fn(){
const log = logEntry();
const log = logEntry(name);
log.info("Lorem ipsum dolor sit amet.");

const result = await exists("./operation.log", {
const result = await exists(`./${name}.log`, {
isFile: true
});

Expand Down
16 changes: 8 additions & 8 deletions test/path.deno.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import {assertEquals, dirname, fromFileUrl} from "../deps.test.ts";
import {slashUnix, slashWin, tmpPath, dataPath, homePath, mainPath} from "../src/path.deno.ts";
import {slashU, slashW, tmpPath, dataPath, homePath, mainPath} from "../src/path.deno.ts";

Deno.test({
name: "Path: Separator",
async fn(){
fn(){
const sampleUnix = "C:/Windows/System32/cmd.exe";
const sampleWindows = "C:\\Windows\\System32\\cmd.exe";

assertEquals(slashUnix(sampleWindows), sampleUnix);
assertEquals(slashWin(sampleUnix), sampleWindows);
assertEquals(slashU(sampleWindows), sampleUnix);
assertEquals(slashW(sampleUnix), sampleWindows);
}
});

Deno.test({
ignore: Deno.build.os !== "windows",
name: "Path: Windows",
async fn(){
fn(){
assertEquals(tmpPath(), "C:/Windows/Temp");
assertEquals(dataPath(), "C:/ProgramData");
assertEquals(homePath(), slashUnix(Deno.env.toObject().USERPROFILE));
assertEquals(mainPath(), slashUnix(fromFileUrl(dirname(Deno.mainModule))));
assertEquals(homePath(), slashU(Deno.env.toObject().USERPROFILE));
assertEquals(mainPath(), slashU(fromFileUrl(dirname(Deno.mainModule))));
}
});

Deno.test({
ignore: Deno.build.os === "windows",
name: "Path: Unix",
async fn(){
fn(){
assertEquals(tmpPath(), "/tmp");
assertEquals(dataPath(), "/var");
assertEquals(homePath(), Deno.env.toObject().HOME);
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);
}
});
Loading

0 comments on commit 09ea5fa

Please sign in to comment.