Skip to content

Instantly share code, notes, and snippets.

View jamesfulford's full-sized avatar
💭
👍

James Fulford jamesfulford

💭
👍
View GitHub Profile
@jamesfulford
jamesfulford / .slate
Created May 26, 2019 21:35
Slate Config
config defaultToCurrentScreen true
config nudgePercentOf screenSize
config resizePercentOf screenSize
# Resize Bindings
bind right:ctrl;cmd;shift resize +25% +0
bind left:ctrl;cmd;shift resize -25% +0
bind up:ctrl;cmd;shift resize +0 -25%
bind down:ctrl;cmd;shift resize +0 +25%
bind right:ctrl;cmd resize -25% +0 bottom-right
@jamesfulford
jamesfulford / cypress-modify-response.js
Last active May 27, 2020 01:33
Cypress POST handler template
function handleRequestWith(makeResponse) {
return (rawRequest) => {
const { xhr } = rawRequest;
Object.defineProperty(xhr.__proto__, 'response', { writable: true });
xhr.response = JSON.stringify(makeResponse(rawRequest));
return rawRequest;
}
}
// sample usage:
# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host ec2_vm_01
HostName 18.189.29.105
User ec2-user
IdentityFile ~/.ssh/james-1.pem
@jamesfulford
jamesfulford / useTime.js
Last active December 13, 2022 09:07
useTime() React hook
//
// useTime hook
//
import { useEffect, useState } from 'react';
export const useTime = (refreshCycle = 100) => {
// Returns the current time
// and queues re-renders every `refreshCycle` milliseconds (default: 100ms)
const [now, setNow] = useState(getTime());
@jamesfulford
jamesfulford / useTimeout.ts
Created November 28, 2019 23:53
useTimeout React Hook
import { useEffect, useCallback, useRef } from 'react';
// React hook for delaying calls with time
// returns callback to use for cancelling
const useTimeout = (
callback: () => void, // function to call. No args passed.
// if you create a new callback each render, then previous callback will be cancelled on render.
timeout: number = 0, // delay, ms (default: immediately put into JS Event Queue)
): () => void => {
@jamesfulford
jamesfulford / partial-use-time-signature.js
Created January 6, 2020 05:11
partial-use-time-signature.js
// ...
import { getTime } from './getTime';
import { DateTime } from 'luxon';
export interface IUseTimeOptions {
_getTime: () => DateTime
}
export const useTime = (
refreshCycleMilliseconds: number = 100,
import { renderHook } from '@testing-library/react-hooks'
import { useTime } from '.';
describe('useTime (pure)', () => {
it('should return the current time (mocked through parameters)', () => {
const now = 'mockNow';
const _getTime = jest.fn().mockReturnValue(now);
// Override the default value and inject our mock _getTime function
const { result } = renderHook(() => useTime(100, { _getTime }));
import React from 'react';
import { DateTime } from 'luxon';
import { useTime } from '.';
interface ICountdownProps {
end: DateTime,
}
export const Countdown: React.FC<ICountdownProps> = ({ end }: ICountdownProps) => {
const now: DateTime = useTime(200); // this countdown will queue a re-render every 200ms.
import { renderHook, act } from '@testing-library/react-hooks'
// Before importing useTime hook
jest.mock('./getTime', () => ({
getTime: jest.fn().mockReturnValue('mockNow')
}));
import { getTime } from './getTime'; // mocked function
import { useTime } from '.';
import { useContext, createContext } from 'react';
const defaultConfig = {
baseurl: 'https://localhost:1948',
env: 'dev',
};
export const ConfigurationContext = createContext(defaultConfig);
export const useResumeURL = () => {