Skip to content

Instantly share code, notes, and snippets.

View jahilldev's full-sized avatar
⏱️
Perf.

James Hill jahilldev

⏱️
Perf.
View GitHub Profile
import Document, {
Main,
NextScript,
Head,
Html
} from 'next/document'
import {readFileSync} from "fs"
import {join} from "path"
class InlineStylesHead extends Head {
@jahilldev
jahilldev / memoize.ts
Created July 8, 2022 11:32
Fully typed Memoize function, maintains argument function and expected return types
function memoize<T extends (...args: any[]) => any>(
func: T
): (...funcArgs: Parameters<T>) => ReturnType<T> {
const results = {};
return (...args: Parameters<T>): ReturnType<T> => {
const cacheKey = JSON.stringify(args);
if (!results[cacheKey]) {
results[cacheKey] = func(...args);
@jahilldev
jahilldev / is-target-element.js
Last active September 13, 2022 13:00
Function to match an element via a target selector in the provided tree. Useful for event delegation.
function isTargetElement(element, selector) {
let target = element;
while (target) {
if (target?.matches && target?.matches(selector)) {
break;
}
target = target?.parentNode;
}
@jahilldev
jahilldev / get-hash-from-value.js
Created May 30, 2022 15:47
Generate a non-secure reproducible hash from an input seed value (not suitable for passwords, or sensitive data)
function getHash(value, length = 16) {
let hash = 0;
for (let index = 0; index < value.length; index++) {
hash = (hash << 5) - hash + value.charCodeAt(index);
hash = hash & hash;
}
hash = Math.abs(hash);
function isPrimeNumber(value) {
for(let index = 2; index < value; index++) {
if(value % index === 0) {
return false;
}
return value > 1;
}
return false;
@jahilldev
jahilldev / tiny-debounce.js
Last active July 28, 2022 17:05
Tiny JavaScript debounce function
function debounce(callback, frequency = 250, timer = null) {
return (...args) => (
clearTimeout(timer), (timer = setTimeout(callback, frequency, ...args))
);
}