Skip to content

Commit

Permalink
Feat ext (#89)
Browse files Browse the repository at this point in the history
* add date and sjis.

* feat: csv

* create directory.

* WIP: big change.

* fix asset path.

* fix log path.

* sha fix.

* word.

* improve.

* change name.

* fix path.

* feat: zip

* fix dirname.

* add attachments.

* type spec.

* add jsdoc.

* remove export.

* ext link.

* WIP

* sheetjs update.

* fix test.

* not export.

* integrate export.

* change name.

* update docs.

* feat: fetch dom

* add list.

* fix header.

* url direct.

* update.
  • Loading branch information
dojyorin authored Jan 28, 2024
1 parent 6de3cfc commit 2d95952
Show file tree
Hide file tree
Showing 62 changed files with 1,137 additions and 454 deletions.
42 changes: 26 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,43 @@
Useful snippet collection.

# Details
Collection of thin snippets to improve usability, some features are unique to this module.
This section describes the unique features of this module.
Collection of utilities to improve usability.

## Minipack
### Minipack
"Minipack" is file archive format unique to this module.

Originally developed for browser, purpose was concatenate multiple files input with DOM File API to single binary.
So no concept of filesystem, features by simple and minimal structure that stores only binary and name.

The actual binary structure looks like this:
Actual binary structure looks like this:

|Index|Type|Title|Size|
|:--|:--|:--|:--|
|1|Header|NameSize|1|
|2|Header|ContentSize|4|
|3|Body|Name|Max 255 (Defined in NameSize)|
|4|Body|Content|Max 4294967295 (Defined in ContentSize)|
|Index|Label|Size (byte)|
|:--|:--|:--|
|1|NameSize|1|
|2|BodySize|4|
|3|Name|Max 255 (by NameSize)|
|4|Body|Max 4294967295 (by BodySize)|

This structure is repeats for the number of files.
This structure is repeats for number of files.

# Browser
Collected only parts of this module that not use the `Deno` namespace and prepared as browser-compatible code to [`mod.universal.ts`](./mod.universal.ts).
# Export
This module has several variations depending on usage.

You can use script from [esm.sh](https://esm.sh).
|Module<br>Name|All<br>Features|No<br>Dependency|Browser<br>Compatible|Description|
|:--|:-:|:-:|:-:|:--|
|[`mod.ts`](./mod.ts)||||Most standard, basically using with Deno.|
|[`mod.full.ts`](./mod.full.ts)||||All features available.|
|[`mod.pure.ts`](./mod.pure.ts)||||Without `Deno`, basically using with browser.|
|[`mod.pure.full.ts`](./mod.pure.full.ts)||||All features of pure script available.|

```ts
import {fetchExtend} from "https://esm.sh/gh/dojyorin/deno_simple_utility@version/mod.universal.ts?bundle&target=esnext";
Export without external dependencies have fewer features than full, but not susceptible to external module vulnerabilities.

When used in browser available via [esm.sh](https://esm.sh).

```html
<script>
import {fetchExtend} from "https://esm.sh/gh/dojyorin/deno_simple_utility@version/mod.pure.ts?bundle&target=esnext";
</script>
```

# API
Expand Down
7 changes: 7 additions & 0 deletions deps.deno_ext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {Logger, ConsoleHandler, FileHandler} from "https://deno.land/[email protected]/log/mod.ts";
export {format} from "https://deno.land/[email protected]/datetime/mod.ts";

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

// @deno-types="npm:@types/[email protected]"
export {createTransport} from "npm:[email protected]";
7 changes: 7 additions & 0 deletions deps.pure_ext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {parse, stringify} from "https://deno.land/[email protected]/csv/mod.ts";

export {ZipReader, ZipWriter, Uint8ArrayReader, Uint8ArrayWriter} from "https://deno.land/x/[email protected]/index.js";

// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.1/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.1/package/xlsx.mjs";
export * as xlsxcp from "https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/cpexcel.full.mjs";
8 changes: 5 additions & 3 deletions deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export {assertEquals} from "https://deno.land/[email protected]/assert/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/[email protected]/path/mod.ts";
export {exists} from "https://deno.land/[email protected]/fs/mod.ts";
export {assertEquals} from "https://deno.land/[email protected]/assert/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/[email protected]/path/mod.ts";
export {exists} from "https://deno.land/[email protected]/fs/mod.ts";

export {DOMParser} from "https://deno.land/x/[email protected]/deno-dom-wasm.ts";
3 changes: 0 additions & 3 deletions deps.ts

This file was deleted.

9 changes: 9 additions & 0 deletions mod.full.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from "./mod.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_ext/dom.ts";
export * from "./src/deno_ext/log.ts";
export * from "./src/deno_ext/smtp.ts";
5 changes: 5 additions & 0 deletions mod.pure.full.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./mod.pure.ts";

export * from "./src/pure_ext/csv.ts";
export * from "./src/pure_ext/excel.ts";
export * from "./src/pure_ext/zip.ts";
12 changes: 12 additions & 0 deletions mod.pure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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";
43 changes: 25 additions & 18 deletions mod.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import "./test/base64.test.ts";
import "./test/byte.test.ts";
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";
import "./test/path.deno.test.ts";
import "./test/platform.deno.test.ts";
import "./test/primitive.test.ts";
import "./test/process.deno.test.ts";
import "./test/stream.test.ts";
import "./test/text.test.ts";
import "./test/time.test.ts";
import "./test/worker.test.ts";
export * from "./test/pure/base64.test.ts";
export * from "./test/pure/byte.test.ts";
export * from "./test/pure/crypto.test.ts";
export * from "./test/pure/deep.test.ts";
export * from "./test/pure/deflate.test.ts";
export * from "./test/pure/fetch.test.ts";
export * from "./test/pure/minipack.test.ts";
export * from "./test/pure/primitive.test.ts";
export * from "./test/pure/stream.test.ts";
export * from "./test/pure/text.test.ts";
export * from "./test/pure/time.test.ts";
export * from "./test/pure/worker.test.ts";

export * from "./test/pure_ext/csv.test.ts";
export * from "./test/pure_ext/excel.test.ts";
export * from "./test/pure_ext/zip.test.ts";

export * from "./test/deno/json.test.ts";
export * from "./test/deno/os.test.ts";
export * from "./test/deno/path.test.ts";
export * from "./test/deno/process.test.ts";

export * from "./test/deno_ext/dom.test.ts";
export * from "./test/deno_ext/log.test.ts";
export * from "./test/deno_ext/smtp.test.ts";
34 changes: 16 additions & 18 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
export * from "./src/base64.ts";
export * from "./src/byte.ts";
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/primitive.ts";
export * from "./src/stream.ts";
export * from "./src/text.ts";
export * from "./src/time.ts";
export * from "./src/worker.ts";
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 "./src/json.deno.ts";
export * from "./src/log.deno.ts";
export * from "./src/path.deno.ts";
export * from "./src/platform.deno.ts";
export * from "./src/process.deno.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";
13 changes: 0 additions & 13 deletions mod.universal.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/deflate.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/json.deno.ts → src/deno/json.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {type Opt} from "./deep.ts";
import {type Opt} from "../pure/deep.ts";

/**
* Read JSON file and convert to object.
* @example
* ```ts
* const json = await jsonRead("./resource.json");
* const json = await jsonRead("./data.json");
* ```
*/
export async function jsonRead<T extends Opt<T>>(path:string):Promise<T>{
Expand All @@ -15,7 +15,7 @@ export async function jsonRead<T extends Opt<T>>(path:string):Promise<T>{
* Convert from object to JSON and write to file.
* @example
* ```ts
* await jsonWrite("./resource.json", {
* await jsonWrite("./data.json", {
* foo: "bar"
* });
* ```
Expand All @@ -30,8 +30,8 @@ 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 dresource from "./resource.json" assert {type: "json"};
* const resource = await jsonLoad("./resource.json", dresource);
* import data from "./data.json" assert {type: "json"};
* const json = await jsonLoad("./data.json", data);
* ```
*/
export async function jsonLoad<T extends Opt<T>>(path:string, def:T):Promise<T>{
Expand Down
8 changes: 8 additions & 0 deletions src/deno/os.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Are you running on Windows?
* @example
* ```ts
* const isWin = osWin;
* ```
*/
export const osWin:boolean = Deno.build.os === "windows";
34 changes: 23 additions & 11 deletions src/path.deno.ts → src/deno/path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {dirname, fromFileUrl} from "../deps.ts";
import {isWin} from "./platform.deno.ts";
import {osWin} from "./os.ts";

/**
* Convert from backslash to slash.
Expand All @@ -26,31 +25,43 @@ export function slashW(path:string):string{
}

/**
* Return system-wide temporary directory path for each OS.
* System-wide temporary directory path for each OS.
* `/tmp` for UNIX and `C:/Windows/Temp` for Windows.
* @example
* ```ts
* const path = tmpPath();
* ```
*/
export function tmpPath():string{
return isWin() ? "C:/Windows/Temp" : "/tmp";
return osWin ? "C:/Windows/Temp" : "/tmp";
}

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

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

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

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

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

return isWin() ? slashU(path) : path;
return protocol === "file:" ? osWin ? path.replace(/^\//, "") : path : `${origin}${path}`;
}
File renamed without changes.
Loading

0 comments on commit 2d95952

Please sign in to comment.