Skip to content

Instantly share code, notes, and snippets.

View jtulk's full-sized avatar

Justin Tulk jtulk

  • El Paso, Texas
View GitHub Profile
type Response = {
contents: (Folder | Item)[];
id: string;
name: string;
}
@jtulk
jtulk / romanNumeralConverter.js
Created October 9, 2019 07:07
Coding Challenge: Turn Roman Numerals into their Numeric Equivalents
// a mapping of the numerals and their values
const NUMERALS = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
@jtulk
jtulk / useLocalStorage.js
Created August 12, 2019 17:48
React Hook to Create State and Syndicate to Local Storage
import { useState, useEffect } from "react";
/**
* A hook to syndicate and hydrate component state into local storage
* @param {string} storageKey - the key to store
* @param {*} [defaultVal] - an initial value for syndication
*/
function useLocalStorage(storageKey, defaultVal = "") {
let initialVal = window.localStorage.getItem(storageKey);
if (initialVal) {
@jtulk
jtulk / example.css
Created August 29, 2018 18:22
What happens when overly specific CSS selectors are run through Autoprefixer (Huge Outputs)
#emailCaptureModal
.modal-body
.gform_wrapper
form
.gform_body
.email_input
.ginput_container_email
input::-webkit-input-placeholder,
#emailCaptureModal
.modal-body
@jtulk
jtulk / readme.md
Created January 18, 2018 05:49
Debugging `jest --watch` when it's hanging infinitely

jestjs/jest#4883 (comment)

Could you try going to node_modules/jest-resolve/build/index.js, and adding console.error(error) before every line that says throw error?

import { ACTIONS } from './actions';
const initialState = {
content: {},
error: null,
isFetching: false,
lastFetched: 0
}
const myContentReducer = (state = initialState, action = {}) => {
import { startFetch, abortFetch, errorFetch, updateFetch } from './syncActionCreators';
// Run the async fetch if the data is stale, otherwise abort the fetch and log it
export const updateDemoContentAsync = () => {
// Redux Thunk allows this, see its docs for more detail
return (dispatch, getState) => {
// Get the state of the store synchronously for the REDUCER IN QUESTION, e.g. myContent here
const timeSinceLastFetch = getState().myContent.lastFetched;
import { ACTIONS } from './actions';
// Log if a fetch is aborted by the async call running too aggressively.
export const abortFetch = () => ({
type: ACTIONS.abort
});
/**
* Log an error if the async call fails
* @param {object} error - the error thrown.
const ACTIONS = {
abort: "abort",
error: "error",
start: "start",
update: "update"
}
@jtulk
jtulk / constants.js
Last active January 25, 2022 17:07
Caching an API Response With Redux-Thunk
const numMinutesToStale = 60;
export const timeToStale = numMinutesToStale * 60 * 1000;