Skip to content

Instantly share code, notes, and snippets.

@nyancodeid
Last active June 9, 2020 08:20
Show Gist options
  • Save nyancodeid/088cbf84703269e13a4149ca906ad3da to your computer and use it in GitHub Desktop.
Save nyancodeid/088cbf84703269e13a4149ca906ad3da to your computer and use it in GitHub Desktop.
# Deno Snippet

Personal Deno Snippet

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)
}
}
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)
}
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 });
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()
}
/**
* 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