Skip to content

Instantly share code, notes, and snippets.

View tbjgolden's full-sized avatar
🏙️
londinium

Tom Golden tbjgolden

🏙️
londinium
  • Error 401
  • London
  • 19:05 (UTC +01:00)
View GitHub Profile
@tbjgolden
tbjgolden / convert.js
Created November 8, 2019 17:12
A very short way to safely encode arbitrary data as an id friendly, url safe slug
// For node and pre-babel src
const toSafeSlug = str => "_" + encodeURIComponent(str)
.replace(/[.!~*'()_-]/g, match => `%${match.charCodeAt(0).toString(16)}`.toUpperCase())
.replace(/%/g, "_");
const fromSafeSlug = str => decodeURIComponent(str.substring(1).replace(/_/g, "%"));
// ES5 friendly
@tbjgolden
tbjgolden / debounce.js
Created May 8, 2019 06:30
Lightweight JS promise debounce
const debounce = (func, ms = 500) => {
let timeout;
return function(...args) {
clearTimeout(timeout);
return new Promise(resolve => {
timeout = setTimeout(() => {
resolve(func.bind(this)(...args));
}, ms);
});
};
@tbjgolden
tbjgolden / withWindowWidth.jsx
Created May 8, 2019 06:20
withWindowWidth React Higher Order Component (HOC) using Hooks
import React, { useState, useEffect, useRef } from 'react';
window.windowWidthListeners = window.windowWidthListeners || {};
window.prevWidth = window.prevWidth || window.innerWidth;
window.addEventListener('resize', () => {
if (window.prevWidth !== window.innerWidth) {
window.prevWidth = window.innerWidth;
Object.values(window.windowWidthListeners).forEach(fn => fn(window.prevWidth));
}
@tbjgolden
tbjgolden / clonify.sh
Created January 30, 2018 17:05
Script used to help mark assignments at UC Berkeley Web Dev, with example urls.txt file
#!/bin/bash
touch urls.txt
CURR=`pwd`
if [ $# -ne 0 ]
then
let COUNT=0
mkdir $1
while read url