Skip to content

Instantly share code, notes, and snippets.

Avatar

Brian Park park-brian

View GitHub Profile
@park-brian
park-brian / editor.js
Last active Jun 23, 2020
HTML Editor Bookmarklet (highlight and drag into bookmarks bar)
View editor.js
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 Jun 23, 2020
A simple utility to validate objects (based off the Angular validation api)
View validate.js
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];
View forEachPromise.js
/**
* 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
Regex Router
View router.js
/*
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 Oct 17, 2019
A minimal R application server
View server.R
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 Aug 23, 2019
Canvas Manhattan Plot
View axis.js
(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 Aug 23, 2019
React Redux ES5 - TodoMVC
View index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<!-- React/ReactDOM -->
<script src="https://cdn.jsdelivr.net/combine/npm/react@16.8.6/umd/react.production.min.js,npm/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
<!-- Redux/Redux Thunk/React Redux -->
<script src="https://cdn.jsdelivr.net/combine/npm/redux@4.0.4/dist/redux.min.js,npm/redux-thunk@2.3.0/dist/redux-thunk.min.js,npm/react-redux@7.1.0/dist/react-redux.min.js"></script>
<link rel="stylesheet" href="styles.css">
@park-brian
park-brian / index.html
Created Jul 17, 2019
Simple HashRouter
View index.html
<!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/babel-polyfill@6.26.0/dist/polyfill.min.js,npm/whatwg-fetch@3.0.0"></script>
@park-brian
park-brian / request.js
Created May 30, 2019
Promise-based wrapper for space-constrained Node.js applications (aws lambda, gc functions, etc)
View request.js
/**
* 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')
*/
const request = (url, opts) => new Promise((resolve, reject) => {
if (!url instanceof URL) url = new URL(url);
const lib = url.protocol == 'https:' ? require('https') : require('http');
const req = lib.request(url, {...opts}, res => {
View index.html
<!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;
You can’t perform that action at this time.