Skip to content

Instantly share code, notes, and snippets.

View hunterwilhelm's full-sized avatar

Hunter Wilhelm hunterwilhelm

View GitHub Profile
@hunterwilhelm
hunterwilhelm / use-read-document-title.ts
Created October 20, 2022 18:12
React Hook: Listen to the document title state & rerender when it changes
import { useEffect, useState } from "react";
export function useReadDocumentTitle() {
const [title, setTitle] = useState(document.title);
useEffect(() => {
const titleElem = document.querySelector('title');
if (!titleElem) return;
const observer = new MutationObserver(function (mutations) {
setTitle(mutations[0].target.textContent ?? '');
});
@hunterwilhelm
hunterwilhelm / cachebust.md
Created January 14, 2022 00:37
Cache busting made easy and reliable
/**
 * Reliably adds a cache bust param to the url
 * @param url: string
 * @returns string 
 */
function cacheBust(url) {
  const urlObject = new URL(url, window.location.origin);
  const paramsObject = new URLSearchParams(urlObject.search);
 paramsObject.set("cacheBust", (new Date().getTime()).toString(36));
@hunterwilhelm
hunterwilhelm / prettyprintdict.erl
Last active February 16, 2022 03:21
Erlang: Pretty print nested dictionaries
%% @author Hunter Wilhelm
-module(prettyprintdict).
-export([pretty_print_dictionary/1]).
%%%--------
%%% helper functions
%%%--------
%% @doc The <kbd>isdict/1</kbd> function is a helper function that returns true if the
%% parameter is a dictionary. This function might not work in future updates of erlang if they
@hunterwilhelm
hunterwilhelm / objects-to-dict-by-property.js
Last active October 13, 2021 21:47
Convert a list of objects to a dictionary by one of their properties
/**
* Convert a list of objects to a dictionary by one of their properties
* Assumes that all objects have the property and the value is unique
*
* @param {Array<Object>} objects - The array of objects
* @param {string} property - The property to turn the list of objects into Dict
* @return {Object<Object>} Dictionary of unmodified objects with the value
* of the properties as keys
*/
function objectsToDictByProperty(objects, property) {
@hunterwilhelm
hunterwilhelm / benchmark.clj
Last active February 16, 2022 03:22
Clojure: Simple expression benchmark test
(defmacro c-time
"Time an expression. Returns the time in milliseconds
Complexity: O(1)
- expr: an expression (not a lambda)"
[expr]
`(let [start# (. System (nanoTime))
ret# ~expr]
(/ (double (- (. System (nanoTime)) start#)) 1000000.0)))
(defmacro benchmark
@hunterwilhelm
hunterwilhelm / LICENSE
Created October 4, 2021 16:22
This license applies to all public gists https://gist.github.com/yeskindofday
MIT License
Copyright (c) 2021 Hunter Wilhelm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@hunterwilhelm
hunterwilhelm / least-disliked-rating.js
Created October 3, 2021 00:05
The least disliked ranking algorithm
// The Dislike Total (DT) is calculated
// by totaling the below 'mean' differences.
// The lowest DT is the winner.
function calcLeastDisliked(multiArray) {
const sum = array=>array.reduce((a,b)=>a + b, 0);
const singleArray = [].concat(...multiArray);
const avg = sum(singleArray) / singleArray.length;
return multiArray.map(ratings=>{
const belowAvgRatings = ratings.filter(x=>avg > x, ratings);