Skip to content

Instantly share code, notes, and snippets.

@park-brian
park-brian / editor.js
Last active June 23, 2020 14:56
HTML Editor Bookmarklet (highlight and drag into bookmarks bar)
data:text/html,<body oninput="document.querySelector('iframe').srcdoc=h.value+'<style>'+c.value+'</style><script>'+j.value+'</script>'"><style>textarea,iframe{box-sizing:border-box;float:right;width:100%;height:50%;border:1px solid steelblue;}body{margin:0}textarea{width:33.33%;font-size:18;resize:none;}</style><textarea placeholder=JS id=j></textarea><textarea placeholder=CSS id=c></textarea><textarea placeholder=HTML id=h></textarea><iframe>
@park-brian
park-brian / validate.js
Last active June 23, 2020 15:00
A simple utility to validate objects (based off the Angular validation api)
function validate(object, rules) {
return Object.entries(object).reduce((errors, [key, value]) => {
let validators = rules[key];
if (!validators || validators.length === 0)
return errors;
if (typeof validators === 'function')
validators = [validators];
@park-brian
park-brian / forEachPromise.js
Last active June 23, 2020 15:08
promise-utils
/**
* Sometimes, we need a plain es5 function to chain execution of promises (eg: when
* targeting older browsers using polyfills, while eschewing a build step). This function
* takes an array of promises or functions which return promises, and applies the supplied
* callback function against each promise sequentially, waiting for resolution before
* executing/resolving the next promise.
*
* Promises always resolve in their order of appearance. However, this function only
* defers execution of promises if functions which return promises are provided (eg:
* using .bind to create bound functions).
@park-brian
park-brian / router.js
Last active May 8, 2020 20:37
Regex Router
/*
Routing consists of matching a string against a set of regular expressions to call a function.
In this case, we can define routes as an array of regular expressions and callbacks.
For example:
let routes = [
[/^\/user\/(\d+)\/?$/, (id) => response({id})],
[/^\/user\/([a-z0-9-_]+)\/?$/, (name) => response({name})],
[/^\/user\/([a-z0-9-_]+)\/(\d+)\/?$/, (name, id) => response({id, name})],
];
@park-brian
park-brian / server.R
Last active October 17, 2019 15:40
A minimal R application server
start.server <- function(host = "localhost", port = 8000, request.handler) {
max.request.length <- 100 * 1024 * 1024 # 100 MB request size
repeat {
socket <- make.socket(host, port, server = TRUE)
on.exit(close.socket(socket))
request <- parse.request(read.socket(socket, maxlen = max.request.length))
response <- request.handler(request)
write.socket(socket, response)
close.socket(socket)
}
@park-brian
park-brian / axis.js
Last active August 23, 2019 21:56
Canvas Manhattan Plot
(function(global) {
global.axis = {
axisLeft: axisLeft,
axisBottom: axisBottom,
};
function axisLeft(canvas, config) {
let font = config.font || systemFont;
let { xOffset, yOffset, scale, tickValues, tickSize } = config;
tickSize = tickSize || 6;
@park-brian
park-brian / index.html
Last active August 23, 2019 19:14
React Redux ES5 - TodoMVC
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<!-- React/ReactDOM -->
<script src="https://cdn.jsdelivr.net/combine/npm/[email protected]/umd/react.production.min.js,npm/[email protected]/umd/react-dom.production.min.js"></script>
<!-- Redux/Redux Thunk/React Redux -->
<script src="https://cdn.jsdelivr.net/combine/npm/[email protected]/dist/redux.min.js,npm/[email protected]/dist/redux-thunk.min.js,npm/[email protected]/dist/react-redux.min.js"></script>
<link rel="stylesheet" href="styles.css">
@park-brian
park-brian / index.html
Created July 17, 2019 00:31
Simple HashRouter
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="root">Loading...</div>
<script nomodule src="https://cdn.jsdelivr.net/combine/npm/[email protected]/dist/polyfill.min.js,npm/[email protected]"></script>
@park-brian
park-brian / request.js
Last active May 5, 2021 19:07
Promise-based wrapper for space-constrained Node.js applications (aws lambda, gc functions, etc)
/**
* A Promise-based wrapper for the http/https.request function
* @param {string|URL} url - Strings are parsed as URL objects
* @param {Object} opts - A set of options for http.request - includes `body`
* @example let response = await request('http://jsonplaceholder.typicode.com/posts/1')
*/
function request(url, opts) {
return new Promise((resolve, reject) => {
if (!(url instanceof URL)) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Hacker News</title>
<style>
body {
background-color: white;