Skip to content

Instantly share code, notes, and snippets.

@dominictobias
dominictobias / express-oauth-example.js
Created January 6, 2019 23:27
NodeJS OAuth from scratch (using GitHub as an example)
// Packages like PassportJS and useless diagrams online make OAuth seem complicated to implement, but in reality it's simple...
const got = require('got');
const jwt = require('jsonwebtoken');
const querystring = require('querystring');
const mins = min => 1000 * 60 * min;
// 1. Direct users browser to this url.
app.get('auth/github', (req, res) => {
const csrfState = Math.random().toString(36).substring(7);
@dominictobias
dominictobias / colorScale.md
Created February 27, 2019 20:32
Javascript percentage colour scale
// green to red
const getColor = v => `hsl(${((1 - v) * 120)},100%,50%)`;

// red to green
const getColor = v => `hsl(${((1 - Math.abs(v - 100)) * 120)},100%,50%)`;

Pass in (perccentage / 100).

@dominictobias
dominictobias / storage-wrapper.js
Last active March 21, 2019 11:21
Simple local and session storage wrapper - falls back to memory in the case of Safari Private browser
function isAvailable() {
try {
this.storage.setItem('__testFeature', true);
this.storage.removeItem('__testFeature');
return true;
} catch (ex) {
return false;
}
}
@dominictobias
dominictobias / setupProxy.js
Created April 1, 2019 15:49
setupProxy.js for http->https with cookies
const proxy = require('http-proxy-middleware');
// Execute `copy(document.cookie)` in the console on the proxy site
// and paste into this string.
const cookies = '';
module.exports = function(app) {
app.use(proxy('/api', {
changeOrigin: true,
cookiePathRewrite: true,
@dominictobias
dominictobias / useResizeObserver.js
Last active July 28, 2022 06:55
useResizeObserver hook for React
import { useRef, useCallback, useEffect, useState } from 'react';
import { ResizeObserver as ResizeObserverPolyfill } from '@juggle/resize-observer';
const ResizeObserver = window.ResizeObserver || ResizeObserverPolyfill;
export default function useResizeObserver() {
const [size, setSize] = useState({ width: 0, height: 0 });
const resizeObserver = useRef(null);
const onResize = useCallback(entries => {
@dominictobias
dominictobias / ResizeObserver.ts
Last active January 21, 2020 12:59
ResizeObserver type
declare global {
interface DOMRectReadOnly {
readonly x: number;
readonly y: number;
readonly width: number;
readonly height: number;
readonly top: number;
readonly right: number;
readonly bottom: number;
readonly left: number;
@dominictobias
dominictobias / swipeDetection.js
Last active January 20, 2020 07:18
Native JS swipe detection
let startX = 0;
let startY = 0;
function handleTouchStart(e) {
startX = e.changedTouches[0].screenX;
startY = e.changedTouches[0].screenY;
}
function handleTouchEnd(e) {
const diffX = e.changedTouches[0].screenX - startX;
@dominictobias
dominictobias / GlobalState.js
Last active January 29, 2020 15:22
GlobalState
class GlobalState {
state = {};
listeners = [];
constructor(reducer, initialState = {}) {
this.reducer = reducer;
this.state = initialState;
this.devTools = typeof window !== 'undefined' && window?.__REDUX_DEVTOOLS_EXTENSION__?.connect();
}
@dominictobias
dominictobias / useGlobalState.js
Last active January 29, 2020 14:51
useGlobalState
function useGlobalState(globalState, stateGetter) {
const [state, setState] = useState(stateGetter(globalState.state));
// We don't want to re-create the listener as we want to unlisten on unmount
// of the component which uses this hook, so we "tunnel" the state in.
const stateRef = useRef(state);
stateRef.current = state;
const listener = useRef(nextState => {
const stateUpdate = stateGetter(nextState);
@dominictobias
dominictobias / appState.js
Last active January 29, 2020 15:35
appState
const darkTheme = {
name: 'dark',
background: 'black',
text: 'white',
}
const lightTheme = {
name: 'light',
background: 'white',
text: 'black',