Skip to content

Instantly share code, notes, and snippets.

@park-brian
park-brian / importDynamoDBTable.js
Last active July 19, 2021 23:49
Uses parallel batchWrite requests to quickly load data into DynamoDB.
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function insertDynamoDBItems(documentClient, tableName, items) {
if (!items.length) return;
let responses = [];
let response = await documentClient
.batchWrite({
@park-brian
park-brian / index.html
Last active July 6, 2021 15:39
Breast Cancer QQ Plot
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>PLCO QQ Plot</h1>
@park-brian
park-brian / fromCsv.js
Last active June 2, 2021 15:24
CSV utilities
/**
* A parser which handles any rfc4180 compliant csv file
* Configuration takes the following properties
* delimiter: specifies the field delimiter (default: ",")
* escape: specifies the escape character (default: ")
* skipLines: specifies the number of lines to skip (default: 0)
* transformRow: transform sthe default ouput from an array of strings to your custom format (eg: an array of objects)
* transformValue: transforms individual values (eg: for custom typecasting logic)
*/
@park-brian
park-brian / index.html
Last active August 11, 2021 19:00
RWS Editor Test Latest
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<input id="height" placeholder="Height">
<input id="width" placeholder="Width">
@park-brian
park-brian / index.html
Last active January 30, 2021 20:12
RWS Editor Test 2
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<input id="height" placeholder="Height">
<input id="width" placeholder="Width">
@park-brian
park-brian / QuadTree.js
Last active October 12, 2020 21:53
A simple quadtree for rapidly fetching spatial data
/**
* A simple quadtree for rapidly fetching spatial data
* @param {number} xMin
* @param {number} xMax
* @param {number} yMin
* @param {number} yMax
*/
function QuadTree(xMin, xMax, yMin, yMax) {
// swap xMin/xMax and yMin/yMax if needed
if (xMin > xMax) [xMin, xMax] = [xMax, xMin];
@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})],
];