Skip to content

Commit

Permalink
Merge pull request #50 from dojyorin/dev
Browse files Browse the repository at this point in the history
fix forgot JSDoc return type.
  • Loading branch information
dojyorin authored May 18, 2023
2 parents 45ef237 + 9e4b19e commit 112a81f
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* const restored = base64Decode(converted);
* ```
*/
export function base64Encode(data:Uint8Array){
export function base64Encode(data:Uint8Array):string{
return btoa([...data].map(n => String.fromCharCode(n)).join(""));
}

Expand All @@ -20,6 +20,6 @@ export function base64Encode(data:Uint8Array){
* const restored = base64Decode(converted);
* ```
*/
export function base64Decode(data:string){
export function base64Decode(data:string):Uint8Array{
return new Uint8Array([...atob(data)].map(s => s.charCodeAt(0)));
}
14 changes: 7 additions & 7 deletions src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){
* const uuid = cryptoUuid();
* ```
*/
export function cryptoUuid(){
export function cryptoUuid():string{
return crypto.randomUUID();
}

Expand All @@ -61,7 +61,7 @@ export function cryptoUuid(){
* const random = cryptoRandom(16);
* ```
*/
export function cryptoRandom(n:number){
export function cryptoRandom(n:number):Uint8Array{
return crypto.getRandomValues(new Uint8Array(n));
}

Expand All @@ -73,7 +73,7 @@ export function cryptoRandom(n:number){
* const hash = await cryptoHash(256, bin);
* ```
*/
export async function cryptoHash(bit:256|384|512, data:Uint8Array){
export async function cryptoHash(bit:256|384|512, data:Uint8Array):Promise<Uint8Array>{
return new Uint8Array(await crypto.subtle.digest(`SHA-${bit}`, data));
}

Expand Down Expand Up @@ -115,7 +115,7 @@ export async function cryptoGenerateKey(isECDH:boolean):Promise<PortableCryptoKe
* }, converted);
* ```
*/
export async function cryptoEncrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array){
export async function cryptoEncrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise<Uint8Array>{
const gcm = aesGcmConfig(cryptoRandom(sizeIv));
const output = new Uint8Array((gcm.tagLength ?? 0 / 8) + gcm.iv.byteLength + data.byteLength);
output.set(<Uint8Array>gcm.iv, 0);
Expand Down Expand Up @@ -143,7 +143,7 @@ export async function cryptoEncrypt({publicKey, privateKey}:PortableCryptoKeyPai
* }, converted);
* ```
*/
export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array){
export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise<Uint8Array>{
const gcm = aesGcmConfig(data.subarray(0, sizeIv));

return new Uint8Array(await crypto.subtle.decrypt(gcm, await deriveSecretKey({publicKey, privateKey}), data.subarray(gcm.iv.byteLength)));
Expand All @@ -159,7 +159,7 @@ export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPai
* const verified = await cryptoVerify(key.publicKey, signature, bin);
* ```
*/
export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array){
export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array):Promise<Uint8Array>{
return new Uint8Array(await crypto.subtle.sign(dsaHash, await crypto.subtle.importKey("pkcs8", privateKey, dsaKey, false, ["sign"]), data));
}

Expand All @@ -173,6 +173,6 @@ export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array){
* const verified = await cryptoVerify(key.publicKey, signature, bin);
* ```
*/
export async function cryptoVerify(publicKey:PortableCryptoKey, signature:Uint8Array, data:Uint8Array){
export async function cryptoVerify(publicKey:PortableCryptoKey, signature:Uint8Array, data:Uint8Array):Promise<boolean>{
return await crypto.subtle.verify(dsaHash, await crypto.subtle.importKey("spki", publicKey, dsaKey, false, ["verify"]), signature, data);
}
4 changes: 2 additions & 2 deletions src/deflate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function streamConvert(data:Uint8Array, ts:TransformStream<Uint8Array, Uin
* const restored = await deflateDecode(converted);
* ```
*/
export async function deflateEncode(data:Uint8Array){
export async function deflateEncode(data:Uint8Array):Promise<Uint8Array>{
return await streamConvert(data, new CompressionStream("deflate-raw"));
}

Expand All @@ -26,6 +26,6 @@ export async function deflateEncode(data:Uint8Array){
* const restored = await deflateDecode(converted);
* ```
*/
export async function deflateDecode(data:Uint8Array){
export async function deflateDecode(data:Uint8Array):Promise<Uint8Array>{
return await streamConvert(data, new DecompressionStream("deflate-raw"));
}
24 changes: 12 additions & 12 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface FetchInit extends Omit<RequestInit, "window">{
/**
* Map of fetch response type and string specify them.
*/
export interface FetchResponseType{
export interface ResponseType{
"text": string;
"json": JsonStruct;
"form": FormData;
Expand All @@ -35,7 +35,7 @@ export interface FetchResponseType{
* const response = await fetchExtend("./asset", "byte");
* ```
*/
export async function fetchExtend<T extends keyof FetchResponseType>(path:string, type:T, option?:FetchInit){
export async function fetchExtend<T extends keyof ResponseType>(path:string, type:T, option?:FetchInit):Promise<ResponseType[T]>{
const {origin, pathname} = /^http(s|):\/\//i.test(path) ? new URL(path) : new URL(path, location.href);
const query = new URLSearchParams(option?.query).toString();

Expand All @@ -56,16 +56,16 @@ export async function fetchExtend<T extends keyof FetchResponseType>(path:string
});

switch(type){
case "text": return <FetchResponseType[T]>await response.text();
case "json": return <FetchResponseType[T]>await response.json();
case "form": return <FetchResponseType[T]>await response.formData();
case "byte": return <FetchResponseType[T]>new Uint8Array(await response.arrayBuffer());
case "buffer": return <FetchResponseType[T]>await response.arrayBuffer();
case "blob": return <FetchResponseType[T]>await response.blob();
case "ok": return <FetchResponseType[T]>response.ok;
case "code": return <FetchResponseType[T]>response.status;
case "header": return <FetchResponseType[T]>response.headers;
case "response": return <FetchResponseType[T]>response;
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 "ok": return <ResponseType[T]>response.ok;
case "code": return <ResponseType[T]>response.status;
case "header": return <ResponseType[T]>response.headers;
case "response": return <ResponseType[T]>response;
default: throw new Error();
}
}
4 changes: 2 additions & 2 deletions src/minipack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type FileInit = [string, Uint8Array];
* const restored = minipackDecode(converted);
* ```
*/
export function minipackEncode(files:FileInit[]){
export function minipackEncode(files:FileInit[]):Uint8Array{
const archive = new Uint8Array(files.reduce((a, [k, v]) => a + sizeName + sizeBody + utfEncode(k).byteLength + v.byteLength, 0));

let i = 0;
Expand Down Expand Up @@ -59,7 +59,7 @@ export function minipackEncode(files:FileInit[]){
* const restored = minipackDecode(converted);
* ```
*/
export function minipackDecode(archive:Uint8Array){
export function minipackDecode(archive:Uint8Array):FileInit[]{
const files:FileInit[] = [];

for(let i = 0; i < archive.byteLength; false){
Expand Down
12 changes: 6 additions & 6 deletions src/path.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {isWin} from "./platform.deno.ts";
* const path = slashUnix("C:\\file");
* ```
*/
export function slashUnix(path:string){
export function slashUnix(path:string):string{
return path.replaceAll("\\", "/");
}

Expand All @@ -21,7 +21,7 @@ export function slashUnix(path:string){
* const path = slashWin("C:/file");
* ```
*/
export function slashWin(path:string){
export function slashWin(path:string):string{
return path.replaceAll("/", "\\");
}

Expand All @@ -33,7 +33,7 @@ export function slashWin(path:string){
* const path = tmpPath();
* ```
*/
export function tmpPath(){
export function tmpPath():string{
return isWin() ? "C:/Windows/Temp" : "/tmp";
}

Expand All @@ -45,7 +45,7 @@ export function tmpPath(){
* const path = dataPath();
* ```
*/
export function dataPath(){
export function dataPath():string{
return isWin() ? "C:/ProgramData" : "/var";
}

Expand All @@ -57,7 +57,7 @@ export function dataPath(){
* const path = homePath();
* ```
*/
export function homePath(){
export function homePath():string{
const {HOME, USERPROFILE} = Deno.env.toObject();

return isWin() ? slashUnix(USERPROFILE) : HOME;
Expand All @@ -70,7 +70,7 @@ export function homePath(){
* const path = mainPath();
* ```
*/
export function mainPath(){
export function mainPath():string{
const path = fromFileUrl(dirname(Deno.mainModule));

return isWin() ? slashUnix(path) : path;
Expand Down
2 changes: 1 addition & 1 deletion src/platform.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* const runOnWin = isWin();
* ```
*/
export function isWin(){
export function isWin():boolean{
return Deno.build.os === "windows";
}
2 changes: 1 addition & 1 deletion src/process.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* const success = executeCommand(["echo", "foobar"]);
* ```
*/
export async function runCommand(...commands:string[]){
export async function runCommand(...commands:string[]):Promise<boolean>{
const {success} = await new Deno.Command(commands.shift() ?? "", {
args: commands,
stdin: "null",
Expand Down
12 changes: 6 additions & 6 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* const restored = utfDecode(converted);
* ```
*/
export function utfEncode(data:string){
export function utfEncode(data:string):Uint8Array{
return new TextEncoder().encode(data);
}

Expand All @@ -20,7 +20,7 @@ export function utfEncode(data:string){
* const restored = utfDecode(converted);
* ```
*/
export function utfDecode(data:Uint8Array){
export function utfDecode(data:Uint8Array):string{
return new TextDecoder().decode(data);
}

Expand All @@ -33,7 +33,7 @@ export function utfDecode(data:Uint8Array){
* const restored = hexDecode(converted);
* ```
*/
export function hexEncode(data:Uint8Array){
export function hexEncode(data:Uint8Array):string{
return [...data].map(n => n.toString(16).toUpperCase().padStart(2, "0")).join("");
}

Expand All @@ -46,7 +46,7 @@ export function hexEncode(data:Uint8Array){
* const restored = hexDecode(converted);
* ```
*/
export function hexDecode(data:string){
export function hexDecode(data:string):Uint8Array{
return new Uint8Array(data.match(/[0-9a-fA-F]{2}/g)?.map(s => parseInt(s, 16)) ?? []);
}

Expand All @@ -58,7 +58,7 @@ export function hexDecode(data:string){
* const formated = trimExtend(text);
* ```
*/
export function trimExtend(data:string){
export function trimExtend(data:string):string{
return data.trim().replace(/\r/g, "").replace(/\t/g, " ").replace(/ +/g, " ").replace(/ +$/mg, "");
}

Expand All @@ -71,6 +71,6 @@ export function trimExtend(data:string){
* const characters = accurateSegment(text);
* ```
*/
export function accurateSegment(data:string){
export function accurateSegment(data:string):string[]{
return [...new Intl.Segmenter().segment(data)].map(({segment}) => segment);
}
6 changes: 3 additions & 3 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* const date = unixtimeDecode(time);
* ```
*/
export function unixtimeEncode(date?:Date){
export function unixtimeEncode(date?:Date):number{
return Math.floor((date ?? new Date()).getTime() / 1000);
}

Expand All @@ -20,7 +20,7 @@ export function unixtimeEncode(date?:Date){
* const date = unixtimeDecode(time);
* ```
*/
export function unixtimeDecode(time:number){
export function unixtimeDecode(time:number):Date{
return new Date(time * 1000);
}

Expand All @@ -31,7 +31,7 @@ export function unixtimeDecode(time:number){
* const time = unixtimeParse("2023-05-18T08:31:32.292Z");
* ```
*/
export function unixtimeParse(ds:string){
export function unixtimeParse(ds:string):number{
const [y, mo, d, h, mi, s] = ds.split(/[/ :TZ_.-]/i).map(s => Number(s));

return unixtimeEncode(new Date(y, (mo ?? 1) - 1, d ?? 1, h ?? 0, mi ?? 0, s ?? 0));
Expand Down

0 comments on commit 112a81f

Please sign in to comment.