Skip to content

Commit

Permalink
Fix method name (#101)
Browse files Browse the repository at this point in the history
* fix method name

* update deps.

* change method name.

* up

* wip

* wip

* reverse

* crypto prefix

* text method refine

* wip

* fix method name

* datetime improved.

* change method name.

* fix jsdoc.

* fix jsdoc

* wip

* wip

* small change.

* update jsdoc.

* type
  • Loading branch information
dojyorin authored Apr 26, 2024
1 parent 7275ce9 commit aebbb5c
Show file tree
Hide file tree
Showing 31 changed files with 302 additions and 290 deletions.
4 changes: 2 additions & 2 deletions deps.deno_ext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {Logger, ConsoleHandler, FileHandler} from "https://deno.land/std@0.221.0/log/mod.ts";
export {format} from "https://deno.land/std@0.221.0/datetime/mod.ts";
export {Logger, ConsoleHandler, FileHandler} from "https://deno.land/std@0.223.0/log/mod.ts";
export {format} from "https://deno.land/std@0.223.0/datetime/mod.ts";

export {type Element, DOMParser} from "https://deno.land/x/[email protected]/deno-dom-wasm.ts";

Expand Down
8 changes: 4 additions & 4 deletions deps.pure_ext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @deno-types="https://deno.land/std@0.221.0/csv/mod.ts"
export {parse, stringify} from "https://esm.sh/gh/denoland/deno_std@0.221.0/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/std@0.223.0/csv/mod.ts"
export {parse, stringify} from "https://esm.sh/gh/denoland/deno_std@0.223.0/csv/mod.ts?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
8 changes: 4 additions & 4 deletions deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export {assertEquals} from "https://deno.land/std@0.221.0/assert/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.221.0/path/mod.ts";
export {exists} from "https://deno.land/std@0.221.0/fs/mod.ts";
export {delay} from "https://deno.land/std@0.221.0/async/mod.ts";
export {assertEquals} from "https://deno.land/std@0.223.0/assert/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.223.0/path/mod.ts";
export {exists} from "https://deno.land/std@0.223.0/fs/mod.ts";
export {delay} from "https://deno.land/std@0.223.0/async/mod.ts";

export {DOMParser} from "https://deno.land/x/[email protected]/deno-dom-wasm.ts";

Expand Down
9 changes: 5 additions & 4 deletions mod.full.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from "./mod.ts";
export * from "./mod.pure.full.ts";

export * from "./src/pure_ext/csv.ts";
export * from "./src/pure_ext/excel.ts";
export * from "./src/pure_ext/zip.ts";
export * from "./src/deno/json.ts";
export * from "./src/deno/os.ts";
export * from "./src/deno/path.ts";
export * from "./src/deno/process.ts";

export * from "./src/deno_ext/dom.ts";
export * from "./src/deno_ext/log.ts";
Expand Down
13 changes: 1 addition & 12 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
export * from "./src/pure/base64.ts";
export * from "./src/pure/byte.ts";
export * from "./src/pure/crypto.ts";
export * from "./src/pure/deep.ts";
export * from "./src/pure/deflate.ts";
export * from "./src/pure/fetch.ts";
export * from "./src/pure/minipack.ts";
export * from "./src/pure/primitive.ts";
export * from "./src/pure/stream.ts";
export * from "./src/pure/text.ts";
export * from "./src/pure/time.ts";
export * from "./src/pure/worker.ts";
export * from "./mod.pure.ts";

export * from "./src/deno/json.ts";
export * from "./src/deno/os.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/deno/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function jsonWrite<T extends Opt<T>>(path:string, data:T):Promise<v
* Argument default value also act as type definition.
* @example
* ```ts
* import data from "./data.json" assert {type: "json"};
* import data from "./data.json" with {type: "json"};
* const json = await jsonLoad("./data.json", data);
* ```
*/
Expand Down
20 changes: 19 additions & 1 deletion src/deno/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,22 @@
* const isWin = osWin;
* ```
*/
export const osWin:boolean = Deno.build.os === "windows";
export const osWindows:boolean = Deno.build.os === "windows";

/**
* Are you running on Mac?
* @example
* ```ts
* const isMac = osMac;
* ```
*/
export const osMac:boolean = Deno.build.os === "darwin";

/**
* Are you running on Linux?
* @example
* ```ts
* const isLinux = osLinux;
* ```
*/
export const osLinux:boolean = Deno.build.os === "linux";
43 changes: 21 additions & 22 deletions src/deno/path.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {osWin} from "./os.ts";
import {osWindows} from "./os.ts";

/**
* Convert from backslash to slash.
* Useful for converting from Windows path to UNIX path.
* @example
* ```ts
* const path = slashU("C:\\file");
* const path = pathSepL("C:\\file");
* ```
*/
export function slashU(path:string):string{
export function pathSepL(path:string):string{
return path.replace(/\\/g, "/");
}

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

Expand All @@ -29,61 +29,60 @@ export function slashW(path:string):string{
* `/tmp` for UNIX and `C:/Windows/Temp` for Windows.
* @example
* ```ts
* const path = tmpPath();
* const path = pathTmp();
* ```
*/
export function tmpPath():string{
return osWin ? "C:/Windows/Temp" : "/tmp";
export function pathTmp():string{
return osWindows ? "C:/Windows/Temp" : "/tmp";
}

/**
* System-wide application data directory path for each OS.
* `/var` for UNIX and `C:/ProgramData` for Windows.
* @example
* ```ts
* const path = dataPath();
* const path = pathVar();
* ```
*/
export function dataPath():string{
return osWin ? "C:/ProgramData" : "/var";
export function pathVar():string{
return osWindows ? "C:/ProgramData" : "/var";
}

/**
* System-wide user config directory path for each OS.
* `~/.config` for UNIX and `~/AppData/Roaming` for Windows.
* @example
* ```ts
* const path = configPath();
* const path = pathConfig();
* ```
*/
export function configPath():string{
return `${homePath()}/${osWin ? "AppData/Roaming" : ".config"}`;
export function pathConfig():string{
return `${pathHome()}/${osWindows ? "AppData/Roaming" : ".config"}`;
}

/**
* System-wide home directory path for each OS.
* `${HOME}` for UNIX and `%USERPROFILE%` for Windows.
* @example
* ```ts
* const path = homePath();
* const path = pathHome();
* ```
*/
export function homePath():string{
export function pathHome():string{
const {HOME, USERPROFILE} = Deno.env.toObject();

return osWin ? slashU(USERPROFILE) : HOME;
return osWindows ? pathSepL(USERPROFILE) : HOME;
}

/**
* Directory of `Deno.mainModule`.
* @example
* ```ts
* const path = mainPath();
* const path = pathMain();
* ```
*/
export function mainPath():string{
const {protocol, origin, pathname} = new URL(Deno.mainModule);
const path = pathname.replace(/[^/]*$/, "");
export function pathMain():string{
const directory = new URL(Deno.mainModule).pathname.replace(/[^/]*$/, "");

return protocol === "file:" ? osWin ? path.replace(/^\//, "") : path : `${origin}${path}`;
return osWindows ? directory.replace(/^\//, "") : directory;
}
4 changes: 2 additions & 2 deletions src/deno/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Run command as subprocess.
* @example
* ```ts
* const success = await runCommand("echo", "foobar");
* const success = await processRun("echo", "foobar");
* ```
*/
export async function runCommand(...commands:string[]):Promise<boolean>{
export async function processRun(...commands:string[]):Promise<boolean>{
const {success} = await new Deno.Command(commands.shift() ?? "", {
args: commands,
stdin: "null",
Expand Down
62 changes: 29 additions & 33 deletions src/deno_ext/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ function getValue(element?:Element){
return element?.getAttribute("value") ?? "";
}

function getContent({textContent}:Element){
return textContent;
}

function extractValue(element?:Element){
switch(element?.tagName){
case "SELECT": return getValue(selectedElement(element.getElementsByTagName("option"), "selected"));
case "DATALIST": return getValue(selectedElement(element.getElementsByTagName("option"), "selected"));
case "OPTION": return getValue(element);
case "INPUT": return getValue(element);
case "TEXTAREA": return getContent(element);
case "TEXTAREA": return element.textContent;
default: return "";
}
}
Expand All @@ -28,10 +24,10 @@ function extractValue(element?:Element){
* @see https://deno.land/x/deno_dom
* @example
* ```ts
* const dom = parseDOM("<div>foo</div>");
* const dom = domDecode("<div>foo</div>");
* ```
*/
export function parseDOM(html:string):Element{
export function domDecode(html:string):Element{
const element = new DOMParser().parseFromString(html, "text/html")?.documentElement;

if(!element){
Expand All @@ -45,11 +41,11 @@ export function parseDOM(html:string):Element{
* Find all `input` `textarea` elements with `id` attribute and convert them to key-value record.
* @example
* ```ts
* const dom = parseDOM("<input id='foo'><textarea id='bar'></textarea>");
* const result = collectInputById(dom);
* const dom = domDecode("<input id='foo'>");
* const result = domValuesPerId(dom);
* ```
*/
export function collectInputById(element:Element):Record<string, string>{
export function domValuesPerId(element:Element):Record<string, string>{
const records:Record<string, string> = {};

for(const input of element.getElementsByTagName("INPUT")){
Expand All @@ -65,60 +61,60 @@ export function collectInputById(element:Element):Record<string, string>{
continue;
}

records[textarea.id] = getContent(textarea);
records[textarea.id] = textarea.textContent;
}

return structuredClone(records);
}

/**
* Find all elements with `name` attribute.
* Get value by `id` search.
* `.value` for `<input>`, `.textContent` for `<textarea>` and `.value` of `.selected` for `<select>` `<dataset>`.
* @example
* ```ts
* const dom = parseDOM("<input name='foo'>");
* const result = getElementsByName(dom, "foo");
* ```ts\
* const dom = domDecode("<input id='foo'>");
* const result = domValueById(dom, "foo");
* ```
*/
export function getElementsByName(element:Element, name:string):Element[]{
return element.getElementsByTagName("*").filter(v => v.getAttribute("name") === name);
export function domValueById(element:Element, id:string):string{
return extractValue(element.getElementById(id) ?? undefined);
}

/**
* Get value by `id` search.
* `.value` for `<input>`, `.textContent` for `<textarea>` and `.value` of `.selected` for `<select>` `<dataset>`.
* Find all elements with `name` attribute.
* @example
* ```ts\
* const dom = parseDOM("<input id='foo'>");
* const result = getValueById(dom, "foo");
* ```ts
* const dom = domDecode("<input name='foo'>");
* const result = domElementsByName(dom, "foo");
* ```
*/
export function getValueById(element:Element, id:string):string{
return extractValue(element.getElementById(id) ?? undefined);
export function domElementsByName(element:Element, name:string):Element[]{
return element.getElementsByTagName("*").filter(v => v.getAttribute("name") === name);
}

/**
* Get value by `name` search.
* `.value` for `<input>`, `.textContent` for `<textarea>` and `.value` of `.selected` for `<select>` `<dataset>`.
* @example
* ```ts
* const dom = parseDOM("<input name='foo'>");
* const result = getValuesByName(dom, "foo");
* const dom = domDecode("<input name='foo'>");
* const result = domValuesByName(dom, "foo");
* ```
*/
export function getValuesByName(element:Element, name:string):string[]{
return getElementsByName(element, name).map(v => extractValue(v));
export function domValuesByName(element:Element, name:string):string[]{
return domElementsByName(element, name).map(v => extractValue(v));
}

/**
* Gets value of `.checked` in group of radio buttons.
* Get value of `.checked` in group of radio buttons.
* @example
* ```ts
* const dom = parseDOM("<input type='radio' name='foo' value='1' checked><input type='radio' name='foo' value='2'>");
* const result = getValueByRadioActive(dom, "foo");
* const dom = domDecode("<input type='radio' name='foo' value='1' checked><input type='radio' name='foo' value='2'>");
* const result = domValueByRadioActive(dom, "foo");
* ```
*/
export function getValueByRadioActive(element:Element, name:string):string{
const elements = getElementsByName(element, name);
export function domValueByRadioActive(element:Element, name:string):string{
const elements = domElementsByName(element, name);

if(elements.some(v => v.tagName !== "INPUT" || v.getAttribute("type") !== "radio")){
return "";
Expand Down
Loading

0 comments on commit aebbb5c

Please sign in to comment.