Skip to content

Instantly share code, notes, and snippets.

@christianjuth
christianjuth / clearStorage.js
Created April 4, 2015 19:09
localStorage clear function with exceptions
clearStorage = function(exceptions){
var storage = localStorage
var keys = [];
var exceptions = [].concat(exceptions) //prevent undefined
//get storage keys
$.each(localStorage, function(key, val) {
keys.push(key);
});
@christianjuth
christianjuth / index.ts
Last active February 5, 2022 01:50
Share logic
function openPopup(url: string, height: number, width: number) {
if (typeof window !== "undefined") {
const newwindow = window.open(
url,
"Share Window",
`height=${height}, width=${width}, top=` +
(window.innerHeight / 2 - height) +
", left=" +
(window.innerWidth / 2 - width) +
", toolbar=0, location=0, menubar=0, directories=0, scrollbars=0"
@christianjuth
christianjuth / hooks.ts
Last active November 16, 2021 04:45
React Hooks
function useMouseSpeed(updateInterval = 500) {
const lastUpdateRef = useRef(Date.now())
const prevPositionRef = useRef<MousePosition | null> (null)
const currentPositionRef = useRef<MousePosition | null> (null)
const [speed, setSpeed] = useState(0)
useEffect(() => {
function trackMouse({ pageX: x, pageY: y }: MouseEvent) {
x = x / window.innerWidth
y = y / window.innerHeight
@christianjuth
christianjuth / App.tsx
Last active August 6, 2021 19:18
This is my own React state machine inspired by XState
import { createMachine, useStateMachine } from "./stateMachine";
const machine = createMachine({
initial: "saved",
context: {
input: ""
},
states: {
saved: {
edit: "hasChanges"
@christianjuth
christianjuth / main.js
Created March 24, 2021 21:27
Memo function
function memo(fn) {
const dict = {}
let id
const perform = { calls: 0, memoHits: 0 }
return (...args) => {
perform.calls++
clearTimeout(id)
id = setTimeout(() => {
console.log(`memo hit rate = ${perform.memoHits}/${perform.calls}`)
}, 1000)
@christianjuth
christianjuth / machine.js
Last active March 18, 2021 18:56
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@christianjuth
christianjuth / merge_sort.py
Created November 28, 2020 05:15
Merge Sort in Python
def sort(arr):
length = len(arr)
if length >= 2:
splitIndex = int(length / 2)
left = arr[0:splitIndex]
right = arr[splitIndex:length]
left = sort(left)
@christianjuth
christianjuth / index.html
Created February 6, 2017 04:47
Working Terminal
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Roboto:900|Pacifico" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link rel='stylesheet' href='style.css'/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src='script.js'></script>
</head>
<body>