Skip to content

Commit

Permalink
Remove dom fetch (#94)
Browse files Browse the repository at this point in the history
* remove: dom fetch

* update date and excel.

* fix test.
  • Loading branch information
dojyorin authored Feb 1, 2024
1 parent 394470a commit 40d7bfd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
13 changes: 0 additions & 13 deletions src/deno_ext/dom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {type Element, DOMParser} from "../../deps.deno_ext.ts";
import {type FetchInit, fetchExtend} from "../pure/fetch.ts";
import {deepClone} from "../pure/deep.ts";

function selectedElement(elements:Element[], attribute:"checked" | "selected"){
Expand All @@ -25,18 +24,6 @@ function extractValue(element?:Element){
}
}

/**
* Download and parse web page.
* @see https://deno.land/x/deno_dom
* @example
* ```ts
* const dom = await fetchDOM("https://www.google.com");
* ```
*/
export async function fetchDOM(path:string, option?:FetchInit):Promise<Element>{
return parseDOM(await fetchExtend(path, "text", option));
}

/**
* Convert from HTML to DOM.
* @see https://deno.land/x/deno_dom
Expand Down
6 changes: 4 additions & 2 deletions src/pure/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ export async function delay(time:number):Promise<number>{
* const format = dtSerial();
* ```
*/
export function dtSerial(date?:Date):string{
export function dtSerial(date?:Date, split?:boolean):string{
const d = date ?? new Date();
const ss = split ? "/" : "";
const sc = split ? ":" : "";

return `${d.getFullYear()}${pad0(d.getMonth() + 1)}${pad0(d.getDate())}${pad0(d.getHours())}${pad0(d.getMinutes())}${pad0(d.getSeconds())}`;
return `${d.getFullYear()}${ss}${pad0(d.getMonth() + 1)}${ss}${pad0(d.getDate())}${split ? " " : ""}${pad0(d.getHours())}${sc}${pad0(d.getMinutes())}${sc}${pad0(d.getSeconds())}`;
}
16 changes: 15 additions & 1 deletion src/pure_ext/excel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {type RawWorkBook, type RawWorkSheet, type RawWorkCell, xlsxcp, set_cptable, xlsxRead, xlsxWrite, xlsxUtil} from "../../deps.pure_ext.ts";
import {deepClone} from "../pure/deep.ts";
import {dtSerial} from "../pure/time.ts";

export type {RawWorkBook, RawWorkSheet, RawWorkCell};

Expand All @@ -19,6 +20,8 @@ export function excelEncodeRaw(book:RawWorkBook, cp?:number, pw?:string):Uint8Ar
const buf = <ArrayBuffer>xlsxWrite(book, {
type: "array",
compression: true,
cellStyles: true,
cellDates: true,
codepage: cp,
password: pw
});
Expand Down Expand Up @@ -81,6 +84,8 @@ export function excelDecodeRaw(data:Uint8Array, cp?:number, pw?:string):RawWorkB
type: "array",
dense: true,
raw: true,
cellStyles: true,
cellDates: true,
codepage: cp,
password: pw
});
Expand All @@ -107,7 +112,16 @@ export function excelDecode(data:Uint8Array, cp?:number, pw?:string):Record<stri
const columns:string[] = [];

for(const column of <(RawWorkCell | undefined)[]>row ?? []){
columns.push(!column || column.t === "e" ? "" : column.w ?? "");
if(!column || column.t === "e" || column.v === undefined){
columns.push("");
}
else if(column.v instanceof Date){
column.v.setMinutes(new Date().getTimezoneOffset());
columns.push(dtSerial(column.v, true));
}
else{
columns.push(`${column.v}`);
}
}

rows.push(columns);
Expand Down
17 changes: 1 addition & 16 deletions test/deno_ext/dom.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals, DOMParser} from "../../deps.test.ts";
import {fetchDOM, parseDOM, collectInputById, getElementsByName, getValueById, getValuesByName, getValueByRadioActive} from "../../src/deno_ext/dom.ts";
import {parseDOM, collectInputById, getElementsByName, getValueById, getValuesByName, getValueByRadioActive} from "../../src/deno_ext/dom.ts";

const sample1 = "<input type='radio' id='aaa' name='aaa' value='123' checked><input type='radio' name='aaa' value='456'>";
const sample2 = new DOMParser().parseFromString(sample1, "text/html")?.documentElement;
Expand All @@ -8,21 +8,6 @@ if(!sample2){
throw new Error();
}

Deno.test({
name: "DOM: Fetch HTML",
async fn(){
const server = Deno.serve({
hostname: "127.0.0.1",
port: 62000,
}, () => new Response(sample1));

const result = await fetchDOM("http://127.0.0.1:62000");
await server.shutdown();

assertEquals(result.innerHTML, sample2.innerHTML);
}
});

Deno.test({
name: "DOM: Parse HTML",
fn(){
Expand Down
6 changes: 5 additions & 1 deletion test/pure_ext/excel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const sample2:RawWorkBook = {
t: "s",
v: "foo",
h: "foo",
w: "foo"
w: "foo",
z: "General",
s: {
patternType: "none"
}
}]
]
}
Expand Down

0 comments on commit 40d7bfd

Please sign in to comment.