Last active
June 9, 2020 08:20
-
-
Save nyancodeid/088cbf84703269e13a4149ca906ad3da to your computer and use it in GitHub Desktop.
# Deno Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { walkSync } from "https://deno.land/std/fs/mod.ts" | |
/** | |
* Get list of file or folder starting from path | |
* @param {String} path | |
* @param {Number} maxDepth - folder deep | |
* @example getFolderList(".", 2) , getFolderList("./folder", 1) | |
*/ | |
export const getFolderList = (path: string, maxDepth: number = 1) { | |
for (const entry of walkSync(path, { maxDepth: maxDepth })) { | |
console.log(entry.path) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ensureDir } from "https://deno.land/std/fs/mod.ts"; | |
/** | |
* create dir recursively, if the directory structure does not exist, it is created. Like mkdir -p. | |
* @param {String} path | |
*/ | |
export const makeRecusiveDir = (path: string): Promise<void> { | |
return ensureDir(path) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Application, Router, send } from "https://deno.land/x/oak/mod.ts" | |
const app = new Application() | |
const router = new Router() | |
router | |
.get("/", async (context) => { | |
await send(context, context.request.url.pathname, { | |
root: `${Deno.cwd()}/public`, | |
index: "index.html", | |
}); | |
}) | |
.get("/api", (context) => { | |
context.response.body = "Hello world!"; | |
}); | |
app.use(router.routes()); | |
app.use(router.allowedMethods()); | |
console.log("Server listening on port :8000") | |
await app.listen({ port: 8000 }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Md5 } from "https://deno.land/std/hash/md5.ts"; | |
const provider = new Md5() | |
/** | |
* Turn string/message into md5 hash string | |
*/ | |
export const md5 = (data: any): string => { | |
return provider.update(data).toString() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get image from url, it will return Blob | |
* @param {String} url | |
* @return Promise<Blob> | |
*/ | |
export const getImage = async (url: string): Promise<Blob> { | |
const res = await fetch(url, { method: "GET" }) | |
return res.blob() | |
} | |
/** | |
* Store image into a file, it will convert blob into Unit8Array | |
* @param {String} url | |
*/ | |
export const saveImage = async (url: string) => { | |
const image = await getImage(url) | |
const buffer = await image.arrayBuffer() | |
const unit8arr = new Deno.Buffer(buffer).bytes() | |
return Deno.writeFile("/path/to/your/file.ext", unit8arr) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment