Skip to content

Commit

Permalink
Code format (#107)
Browse files Browse the repository at this point in the history
* change interface name

* code format.
  • Loading branch information
dojyorin authored Jul 4, 2024
1 parent fbd3979 commit 39dab20
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/deno_ext/smtp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {createTransport} from "../../deps.ts";
import {type DataMap} from "../pure/minipack.ts";
import {type DataEntry} from "../pure/minipack.ts";

/**
* E-MAIL message.
Expand All @@ -11,7 +11,7 @@ export interface MailMessage {
body: string;
cc?: string[];
bcc?: string[];
files?: DataMap[];
files?: DataEntry[];
}

/**
Expand Down
26 changes: 13 additions & 13 deletions src/pure/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface ResponseType {
interface ReturnTypeMap {
"text": string;
"json": unknown;
"form": FormData;
Expand Down Expand Up @@ -29,7 +29,7 @@ export interface FetchInit extends Omit<RequestInit, "integrity" | "window"> {
* const response = await fetchExtend("./asset", "byte");
* ```
*/
export async function fetchExtend<T extends keyof ResponseType>(path: string, type: T, option?: FetchInit): Promise<ResponseType[T]> {
export async function fetchExtend<T extends keyof ReturnTypeMap>(path: string, type: T, option?: FetchInit): Promise<ReturnTypeMap[T]> {
const u = new URL(path, globalThis?.location?.href);
u.hash = "";

Expand All @@ -52,17 +52,17 @@ export async function fetchExtend<T extends keyof ResponseType>(path: string, ty
});

switch(type) {
case "text": return <ResponseType[T]>await response.text();
case "json": return <ResponseType[T]>await response.json();
case "form": return <ResponseType[T]>await response.formData();
case "byte": return <ResponseType[T]>new Uint8Array(await response.arrayBuffer());
case "buffer": return <ResponseType[T]>await response.arrayBuffer();
case "blob": return <ResponseType[T]>await response.blob();
case "stream": return <ResponseType[T]>(response.body ?? undefined);
case "ok": await response.body?.cancel(); return <ResponseType[T]>response.ok;
case "code": await response.body?.cancel(); return <ResponseType[T]>response.status;
case "header": await response.body?.cancel(); return <ResponseType[T]>response.headers;
case "response": return <ResponseType[T]>response;
case "text": return <ReturnTypeMap[T]> await response.text();
case "json": return <ReturnTypeMap[T]> await response.json();
case "form": return <ReturnTypeMap[T]> await response.formData();
case "byte": return <ReturnTypeMap[T]> new Uint8Array(await response.arrayBuffer());
case "buffer": return <ReturnTypeMap[T]> await response.arrayBuffer();
case "blob": return <ReturnTypeMap[T]> await response.blob();
case "stream": return <ReturnTypeMap[T]> (response.body ?? undefined);
case "ok": await response.body?.cancel(); return <ReturnTypeMap[T]> response.ok;
case "code": await response.body?.cancel(); return <ReturnTypeMap[T]> response.status;
case "header": await response.body?.cancel(); return <ReturnTypeMap[T]> response.headers;
case "response": return <ReturnTypeMap[T]> response;
default: throw new Error();
}
}
8 changes: 4 additions & 4 deletions src/pure/minipack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const BODY_SIZE = 4;
/**
* Simple name and body pair.
*/
export interface DataMap {
export interface DataEntry {
name: string;
body: Uint8Array;
}
Expand All @@ -24,7 +24,7 @@ export interface DataMap {
* const decode = minipackDecode(encode);
* ```
*/
export function minipackEncode(files: DataMap[]): Uint8Array {
export function minipackEncode(files: DataEntry[]): Uint8Array {
const archive = new Uint8Array(files.reduce((size, {name, body}) => size + NAME_SIZE + BODY_SIZE + textEncode(name).byteLength + body.byteLength, 0));

let i = 0;
Expand Down Expand Up @@ -60,8 +60,8 @@ export function minipackEncode(files: DataMap[]): Uint8Array {
* const decode = minipackDecode(encode);
* ```
*/
export function minipackDecode(archive: Uint8Array): DataMap[] {
const files: DataMap[] = [];
export function minipackDecode(archive: Uint8Array): DataEntry[] {
const files: DataEntry[] = [];

for(let i = 0; i < archive.byteLength;) {
const ns = new DataView(archive.buffer, i).getUint8(0);
Expand Down
20 changes: 10 additions & 10 deletions src/pure/primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type WidenLiteral<T> = T extends string ? string : T extends number ? number : T
type MaybeString = string | null | undefined;
type TypeStrict<T extends unknown, U extends boolean> = U extends true ? T : T | undefined;

interface TypeMap {
interface PrimitiveTypeMap {
"string": string;
"number": number;
"boolean": boolean;
Expand All @@ -24,15 +24,15 @@ function strictUndef(strict?: boolean) {
* const value = primitiveParse("123", "number", true);
* ```
*/
export function primitiveParse<T extends keyof TypeMap, U extends boolean>(text: MaybeString, type: T, strict?: U): TypeStrict<TypeMap[T], U> {
export function primitiveParse<T extends keyof PrimitiveTypeMap, U extends boolean>(text: MaybeString, type: T, strict?: U): TypeStrict<PrimitiveTypeMap[T], U> {
if(text === undefined || text === null) {
return <TypeStrict<TypeMap[T], U>>strictUndef(strict);
return <TypeStrict<PrimitiveTypeMap[T], U>> strictUndef(strict);
}

switch(type) {
case "string": return <TypeStrict<TypeMap[T], U>>text.toString();
case "number": return <TypeStrict<TypeMap[T], U>>parseInt(text);
case "boolean": return <TypeStrict<TypeMap[T], U>>(text === "true");
case "string": return <TypeStrict<PrimitiveTypeMap[T], U>> text.toString();
case "number": return <TypeStrict<PrimitiveTypeMap[T], U>> parseInt(text);
case "boolean": return <TypeStrict<PrimitiveTypeMap[T], U>> (text === "true");
default: throw new Error();
}
}
Expand All @@ -48,13 +48,13 @@ export function primitiveParse<T extends keyof TypeMap, U extends boolean>(text:
*/
export function primitiveParseX<T extends string | number | boolean>(text: MaybeString, def: T): WidenLiteral<T> {
if(text === undefined || text === null) {
return <WidenLiteral<T>>def;
return <WidenLiteral<T>> def;
}

switch(typeof def) {
case "string": return <WidenLiteral<T>>text.toString();
case "number": return <WidenLiteral<T>>parseInt(text);
case "boolean": return <WidenLiteral<T>>(text === "true");
case "string": return <WidenLiteral<T>> text.toString();
case "number": return <WidenLiteral<T>> parseInt(text);
case "boolean": return <WidenLiteral<T>> (text === "true");
default: throw new Error();
}
}
4 changes: 2 additions & 2 deletions src/pure_ext/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ export function sheetDecode(data: Uint8Array, cp?: number, pw?: string): Record<
for(const [name, sheet] of Object.entries(Sheets)) {
const rows: string[][] = [];

for(const row of <(RawWorkCell[] | undefined)[]>sheet["!data"] ?? []) {
for(const row of <(RawWorkCell[] | undefined)[]> sheet["!data"] ?? []) {
const columns: string[] = [];

for(const column of <(RawWorkCell | undefined)[]>row ?? []) {
for(const column of <(RawWorkCell | undefined)[]> row ?? []) {
if(!column || column.t === "e" || column.v === undefined) {
columns.push("");
} else if(column.v instanceof Date) {
Expand Down
8 changes: 4 additions & 4 deletions src/pure_ext/zip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ZipReader, ZipWriter, Uint8ArrayReader, Uint8ArrayWriter} from "../../deps.pure.ts";
import {type DataMap} from "../pure/minipack.ts";
import {type DataEntry} from "../pure/minipack.ts";

/**
* Convert from named binary to ZIP archive.
Expand All @@ -14,7 +14,7 @@ import {type DataMap} from "../pure/minipack.ts";
* const files = await zipDecode(zip);
* ```
*/
export async function zipEncode(files: DataMap[], pw?: string, weak?: boolean): Promise<Uint8Array> {
export async function zipEncode(files: DataEntry[], pw?: string, weak?: boolean): Promise<Uint8Array> {
const zip = new ZipWriter(new Uint8ArrayWriter(), {
password: pw,
zipCrypto: weak
Expand Down Expand Up @@ -42,8 +42,8 @@ export async function zipEncode(files: DataMap[], pw?: string, weak?: boolean):
* const files = await zipDecode(zip);
* ```
*/
export async function zipDecode(archive: Uint8Array, pw?: string, encode?: string): Promise<DataMap[]> {
const files: DataMap[] = [];
export async function zipDecode(archive: Uint8Array, pw?: string, encode?: string): Promise<DataEntry[]> {
const files: DataEntry[] = [];
const zip = new ZipReader(new Uint8ArrayReader(archive), {
useWebWorkers: false,
filenameEncoding: encode,
Expand Down
6 changes: 3 additions & 3 deletions test/deno_ext/smtp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Deno.test({
const result = new Promise<MailMessage>((res) => {
server.bind((_, __, {headers, body}) => {
res({
from: <string>headers.from,
to: [<string>headers.to],
title: <string>headers.subject,
from: <string> headers.from,
to: [<string> headers.to],
title: <string> headers.subject,
body: body ?? ""
});
});
Expand Down

0 comments on commit 39dab20

Please sign in to comment.