Skip to content

Instantly share code, notes, and snippets.

View jamesfulford's full-sized avatar
💭
👍

James Fulford jamesfulford

💭
👍
View GitHub Profile
@jamesfulford
jamesfulford / useLocalStorage.js
Last active December 2, 2021 08:32
useLocalStorage() React Hook (from https://usehooks.com/useLocalStorage/)
import { useState } from "react";
export function useLocalStorage<T>(key: string, initialValue: T): [T, (s: T) => void] {
// State to store our value
// Pass initial state function to useState so logic is only executed once
const [storedValue, setStoredValue] = useState<T>(() => {
try {
// Get from local storage by key
const item = window.localStorage.getItem(key);
// Parse stored json or if none return initialValue
@jamesfulford
jamesfulford / track-ticker.py
Created May 22, 2020 02:29
Alpaca-compliant algorithm for tracking a single asset. Underperforms most assets except in downturns - a conservative strategy.
from pylivetrader.api import *
import logbook
log = logbook.Logger('track-ticker')
def enter_play(context, data):
s = context.ticker
function startCountingForHideAndSeek() {
// State for managing cleanup and cancelling
let finished = false;
let cancel = () => finished = true;
const promise = new Promise((resolve, reject) => {
//
// Custom Promise Logic
//
// NOTE: This countdown not finish in exactly 10 seconds, don't build timers this way!
// Promis-ifying `setTimeout`, handy to have
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Example promise-creator. This one happens to load scripts synchronously in a browser,
// but you can work with any kind of promise here, even in node.
function getScript(src) {
return new Promise((resolve, reject) => {
const element = document.createElement('script');
@jamesfulford
jamesfulford / build.sh
Created March 16, 2020 03:18
Building a GitHub.io-ready static site
#!/bin/bash
expo build:web
web_build_return_code="$?"
echo "yourwebsitehere.com" > web-build/CNAME
exit "${web_build_return_code}"
@jamesfulford
jamesfulford / commands.js
Created March 16, 2020 02:06
Cypress: Mock Geolocation API
Cypress.Commands.add('mockGeolocation', (latitude, longitude) => {
cy.window().then(($window) => {
cy.stub($window.navigator.geolocation, 'getCurrentPosition', (callback) => {
return callback({ coords: { latitude, longitude } });
});
});
});
@jamesfulford
jamesfulford / clear-service-workers.js
Created March 16, 2020 01:59
Clear Service Workers, Cypress
// From https://github.com/cypress-io/cypress/issues/702#issuecomment-435873135
beforeEach(() => {
if (window.navigator && navigator.serviceWorker) {
navigator.serviceWorker.getRegistrations()
.then((registrations) => {
registrations.forEach((registration) => {
registration.unregister();
});
});
}
import { renderHook, act } from '@testing-library/react-hooks'
import { useFakeTimers, SinonFakeTimers } from 'sinon';
import { useTime } from '.';
describe('useTime (sinon clocks)', () => {
let clock: SinonFakeTimers;
beforeEach(() => { clock = useFakeTimers(); });
afterEach(() => { clock.restore(); });
import React, { FunctionComponent, ReactNode } from 'react';
import { useResumeURL, ConfigurationContext } from '.';
import { renderHook } from '@testing-library/react-hooks';
describe('useResumeURL (context)', () => {
const makeWrapper = (value: any): FunctionComponent => ({ children }: { children?: ReactNode }) => (
<ConfigurationContext.Provider value={value}>
{children}
</ConfigurationContext.Provider>
);
import React from 'react';
import { useResumeURL } from '.';
export const ConfigSection = () => {
const url = useResumeURL();
return <div>
<a href={url} target="_blank" rel="noopener noreferrer">Open my resume</a>
</div>;
}