Skip to content

Instantly share code, notes, and snippets.

View trevorhreed's full-sized avatar

Trevor trevorhreed

  • Utah
  • 07:44 (UTC -07:00)
View GitHub Profile
@trevorhreed
trevorhreed / estate.js
Last active October 7, 2024 16:53
A little state manager that does not scale. Uses localStorage and proxies.
export const encode = value => btoa(JSON.stringify(value))
export const decode = value => JSON.parse(atob(value))
export const save = (key, value) => localStorage.setItem(key, encode(value))
export const load = key => decode(localStorage.getItem(key) ?? btoa('{}'))
export const estate = (state = {}, {
localStorageKey = 'state',
} = {}) => {
const isProxy = Symbol('isProxy')
const handler = {
@trevorhreed
trevorhreed / DistributedS3LockService.java
Last active August 22, 2023 21:40
A distributed mutex service that uses S3 to manage locks.
/*
CRITIQUE: It's missing fences
I need a distributed lock system for a service I'm writing.
(There can be multiple instances of this service running at
a time, and I need to make sure that only one instance does
a particular cron job.)
According to [this page](https://aws.amazon.com/s3/consistency/),
@trevorhreed
trevorhreed / merge.js
Last active July 1, 2023 05:49
A deep merge of options or configuration values
export const merge = (target, ...sources) => {
target = { ...target }
if (!sources.length) return target
const source = sources.shift()
for(const key in source) {
const value = target[key]
const isObject = value
&& typeof value == 'object'
&& !(value instanceof Date)
&& !(value instanceof RegExp)
@trevorhreed
trevorhreed / json-diff.js
Created October 19, 2022 04:13
Get a diff of two json objects
const diff = (a, b, path = '') => {
let diffs = []
const {
type: typeA,
value: valueA
} = diff.meta(a)
const {
type: typeB,
value: valueB
} = diff.meta(b)
@trevorhreed
trevorhreed / format-phone-number.gs
Created July 7, 2022 05:22
Format phone numbers in Google Sheets using Google App Sheet
function onOpen() {
var ui = SpreadsheetApp.getUi()
ui.createMenu('Utilities')
.addItem('Format Phone Numbers', 'formatPhone')
.addToUi();
}
function formatPhone() {
const range = SpreadsheetApp.getActive().getActiveRange()
const values = range
@trevorhreed
trevorhreed / command-line-args.js
Last active January 20, 2022 23:34
Turns `process.argv` into an array-like object with fixed position arguments indexed by number and named arguments indexed by key.
/**
* Simple command line argument parser
*
* An instance of Args is an array-like
* object that contains a list of fixed
* and named arguments
*
* Given command line args of:
*
* node args.js command -a --param=value
@trevorhreed
trevorhreed / lcr.js
Last active July 24, 2021 03:41
LCR
{
console.clear()
;(async function run() {
copyMembersWithoutCallings()
})()
async function copyMembersWithoutCallings() {
const urls = [
export class UnitWorkerManager {
private resolves = {}
private rejects = {}
private promiseId = 0
private worker
constructor(scriptPath, opts) {
this.worker = new Worker(scriptPath, opts)
this.worker.addEventListener('message', data => {
const { id, err, result } = data || {}
@trevorhreed
trevorhreed / trigger-event-on-element-viewable.html
Last active November 2, 2019 18:57
Capture event when elements scroll in and out of view
<!doctype html>
<html>
<head>
<title>Test</title>
<style type="text/css">
html{
margin: 20px;
}
#stats{
position: fixed;
console.clear()
const Pid = (function(){
// UUID generator
!function(n){"use strict";function e(){var e=n.crypto||n.msCrypto;if(!f&&e&&e.getRandomValues)try{var r=new Uint8Array(16);s=f=function(){return e.getRandomValues(r),r},f()}catch(n){}if(!f){var o=new Array(16);i=f=function(){for(var n,e=0;e<16;e++)0===(3&e)&&(n=4294967296*Math.random()),o[e]=n>>>((3&e)<<3)&255;return o},"undefined"!=typeof console&&console.warn&&console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()")}}function r(){if("function"==typeof require)try{var n=require("crypto").randomBytes;c=f=n&&function(){return n(16)},f()}catch(n){}}function o(n,e,r){var o=e&&r||0,t=0;for(e=e||[],n.toLowerCase().replace(/[0-9a-f]{2}/g,function(n){t<16&&(e[o+t++]=y[n])});t<16;)e[o+t++]=0;return e}function t(n,e){var r=e||0,o=v;return o[n[r++]]+o[n[r++]]+o[n[r++]]+o[n[r++]]+"-"+o[n[r++]]+o[n[r++]]+"-"+o[n[r++]]+o[n[r++]]+"-"+o[n[r++]]+o[n[r++]]+"-"+o[n[r++]]+o[n[r++]]+o[n[r++]]+o[n[r++]]+o[n[r++]]+o[n[r++]]}functi