Skip to content

Instantly share code, notes, and snippets.

View andycarrell's full-sized avatar

Andy Carrell andycarrell

View GitHub Profile
# bash
eval $(/opt/homebrew/bin/brew shellenv)
alias bashrefresh=". ~/.bash_profile"
alias bashopen="open ~/.bash_profile"
# fnm
eval "$(fnm env)"
# mac
alias big-sur="xcode-select --install"
class SetInterval extends React.Component {
componentDidMount() {
this.setNewInterval();
}
componentDidUpdate(prevProps) {
if (this.props.delay !== prevProps.delay) {
clearInterval(this.interval);
this.setNewInterval();
}
import { useCallback, useState } from "react";
export default function useMutation(mutate) {
const [isLoading, setIsLoading] = useState(false);
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const runMutation = useCallback(
(...args) => {
setIsLoading(true);
const useInputValue = initialValue => {
const [value, setX] = React.useState(initialValue);
const prop = p => o => o[p];
const setValue = Ramda.compose(
setX,
prop("value"),
prop("target"),
);
password: Yup.string()
.required("Password is required")
.min(8, "Password must be at least 8 characters long")
.matches(/\d/, "Password must contain a number")
.matches(
/[\W_]/,
"Password must contain a non-alphanumberic character",
),
// new RegExp(`^((?!${name}).)*$`);
const composeDecorators = (first, ...decorators) =>
decorators.reduce(
(accumulatedDecorators, nextDecorator) => storyFn =>
accumulatedDecorators(() => nextDecorator(storyFn)),
first,
);
// please don't
const ds = (i, ...r) => r.reduce((a, d) => f => a(() => d(f)), i);
function useStorageListener(key, onChange) {
useEffect(() => {
function handleChange({ key: eventKey, newValue }) {
if (eventKey === key) {
const parsed = newValue && JSON.parse(newValue);
onChange(parsed);
}
if (!eventKey) {
onChange(getLocalStorageItem(key));
}
const [hasCheckedSession, setHasCheckedSession] = useState(false);
useEffect(() => {
if (hasCheckedSession) {
return;
}
if (!sessionToken) {
setHasCheckedSession(true);
return;
const withFormikField = F => {
const HoC = ({ name, onBlur, onChange, ...rest }) => {
const handleChange = value => onChange({ target: { name, value } });
const handleBlur = () => onBlur({ target: { name } });
return <F {...rest} onChange={handleChange} onBlur={handleBlur} />;
};
return HoC;
};
const SelectForFormik = withFormikField(Select);
function useTimeout() {
const ids = React.useRef([]);
React.useEffect(() => () => ids.current.forEach(clearTimeout), []);
// setTimeout takes a callback and a delay, then returns the id associated with the timeout.
const _setTimeout = React.useCallback((callback, delay) => {
const id = setTimeout(callback, delay);
ids.current.push(id);