Skip to content

Instantly share code, notes, and snippets.

View ETNOL's full-sized avatar
🌝

Eric Nolte ETNOL

🌝
View GitHub Profile
@ETNOL
ETNOL / ExecutionContext.ts
Created December 4, 2024 19:39
ExecutionContext Decorator Idea
/** For use in testing only */
export const resetExecutionHistory = () => {
Object.keys(executionHistory).forEach(key => delete executionHistory[key])
}
/**
* Specifies when a class or function has an execution dependency on another
* function or class.
@ETNOL
ETNOL / addStyleHooks.ts
Last active May 30, 2025 13:56
Hooks for style changes [WIP]
/**
* Adds callbacks to an element's style changes.
* Useful if you need to make changes right before and/or right
* after an element recieves a style change.
* @param el Element
* @param hooks Pre and post style change callbacks
*/
export const addStyleHooks = (
el: HTMLElement,
@ETNOL
ETNOL / cacheFunction.ts
Last active December 29, 2020 14:48
Cache a function's return
type FuncOfUnknownArgs<ReturnType> = (...args: Array<any>) => ReturnType
const cache = <ReturnType>(fn: FuncOfUnknownArgs<ReturnType>): FuncOfUnknownArgs<ReturnType> => {
let cachedVal: ReturnType
/** Don't trust cache val to be truthy */
let hasCached = false
return function (...args: Array<any>) {
if (!hasCached) {
hasCached = true
@ETNOL
ETNOL / DUCHT
Last active August 14, 2020 17:46
{
"nl": {
"default": {
"heading": "Wij geven om uw privacy",
"details1": {
"text1": "Wij en",
"link1": "onze partners",
"purpose1": "Informatie op een apparaat opslaan en / of openen"
},
"details2": {
@ETNOL
ETNOL / getAllBreakpoints.js
Created June 30, 2020 13:37
Find & count all breakpoints in a site's css
/**
* Find all style and link/stylesheet tags.
* Request all the css from link tags.
* Parse through all the style and css text.
* Find any min-width or max-width within parentheses.
* Log out a key of each min/max width and a count of how
* often it was used.
*/
const getAllBreakpoints = () => {
const styleTexts = []
export function insertScript(script: HTMLScriptElement, callback?: Function) {
const firstScript = document.getElementsByTagName(
'script'
)[0] as HTMLScriptElement
const parentNode = firstScript.parentNode as HTMLElement
const errorMessage = `Error loading asset ${script.getAttribute('src')}`
parentNode.insertBefore(script, firstScript)
script.addEventListener('load', () => (callback ? callback() : null))
script.addEventListener('error', () =>
callback ? callback(errorMessage) : null
@ETNOL
ETNOL / TimedFunction.spec.ts
Created April 30, 2018 12:58
Timed Function class
import TimedFunction from '../TimedFunction'
describe('Timed Function', () => {
let func
let timedFunction: TimedFunction
beforeEach(() => {
func = jest.fn()
// speed this shit up
TimedFunction.tickRate = 100
@ETNOL
ETNOL / Togglable.tsx
Last active August 1, 2017 00:40
Typescript + React + Mixins
/*
* I'd been working on getting this pattern just right for adding common behaviors to class-based React components.
* The last hiccup I had was getting the state and props to inherit correctly from a mixin into a superclass and this
* appears to finally work as expected.
*/
import * as React from 'react'
type Component<P, S, T = React.Component<P, S>> = new (...args: any[]) => T
@ETNOL
ETNOL / change-calc-refactor.rb
Created September 22, 2014 16:11
Long verbose methods
def have_correct_change?(desired_total, your_coins)
possible_arrangements = your_coins.count
for_all(possible_arrangements) do |a_possible_arrangement|
return true if correct_change_can_be_made?(desired_total, a_possible_arrangement, your_coins)
end
false
end
def correct_change_can_be_made?(desired_total, arrangement, your_coins)
possible_combos_in_an_arrangement = all_combinations_of(arrangement, your_coins)
@ETNOL
ETNOL / no-refactor.rb
Last active August 29, 2015 14:06
Check for exact change
def change_calculator(total, change_array)
change_array.length.times do |iteration|
combinations = change_array.combination(iteration)
combinations.each do |combo|
return true if (combo.inject(:+)) == total
end
end
return false
end