Skip to content

Instantly share code, notes, and snippets.

View dr-skot's full-sized avatar

Scott Shepherd dr-skot

View GitHub Profile
@dr-skot
dr-skot / Regex.swift
Last active March 9, 2016 13:02 — forked from ningsuhen/Regex.swift
Swift extension that adds convenient regex methods to String. Forked from ningsuhen & cleaned up for Swift 2.0
import Foundation
struct Regex {
var pattern: String {
didSet { updateRegex() }
}
var expressionOptions: NSRegularExpressionOptions {
didSet { updateRegex() }
}
var matchingOptions: NSMatchingOptions
@dr-skot
dr-skot / stopwatch.js
Created April 23, 2021 16:17
Basic javascript stopwatch object
function StopWatch() {
let started = Date.now(),
time = 0,
running = false;
function getTime() {
return running ? Date.now() - started : time;
}
function start() {
@dr-skot
dr-skot / use-async-reducer-example.jsx
Last active May 10, 2022 00:01
React hook like useReducer but can update state asynchronously
// useAsyncReducer works like useReducer, except the reducer has access to the dispatch function
// so it can change state after async operations complete
// example usage:
import useAsyncReducer from './use-async-reducer';
function reducer(state, dispatch, action, args) {
switch (action) {
case 'fetch':
@dr-skot
dr-skot / visit-with-stubs.js
Last active September 10, 2021 12:51
Stub module imports in Cypress
// cypress/support/commands.js
// ...
Cypress.Commands.add('visitWithStubs', (url, stubs) => {
if (!stubs) return cy.visit(url);
cy.visit(url, {
onBeforeLoad(win) {
Object.entries(stubs).forEach(([name, stubFunction]) => {
let target = undefined;
// attach getters and setters for this name on window
Object.defineProperty(win, name, {
@dr-skot
dr-skot / waitUntil.ts
Last active August 31, 2022 17:52
javascript: wait until a condition becomes true
export function waitUntil<T>(condition: () => T, timeout = 10000, interval = 50): Promise<T> {
let result = condition();
if (result) return Promise.resolve(result);
return new Promise((resolve) => {
const finish = () => {
clearInterval(intervalId);
resolve(result);
};
const intervalId = setInterval(() => {
result = condition();
@dr-skot
dr-skot / useParseQuerySafer.ts
Last active March 18, 2022 16:39
workaround for an issue with @parse/react-ssr 0.0.1.alpha-17 (https://github.com/parse-community/parse-react/issues/81)
import { EncodedParseQuery, useParseQuery } from '@parse/react-ssr';
import usePrevious from '@/hooks/usePrevious';
import { useEffect, useMemo, useRef, useState } from 'react';
import { UseParseQueryResult } from '@parse/react-base';
// this is a workaround for
// https://github.com/parse-community/parse-react/issues/81
const useParseQuerySafer: typeof useParseQuery = (query, options) => {
const didntSyncYet = useRef(false);
@dr-skot
dr-skot / useEffectOnMount.ts
Last active July 31, 2022 18:13
run an effect when the dependencies change, but not at initialization
import { useEffect, useRef, EffectCallback, DependencyList } from 'react';
// React.useEffect runs its effect 1) at initialization and then 2) whenever the deps change
// Sometimes you don't want (1), just (2). That's what this is for.
export function useEffectOnMount(effect: EffectCallback, deps: DependencyList): void {
const isMounted = useRef(false);
useEffect(() => {
if (isMounted.current) return effect();
@dr-skot
dr-skot / Parse.ts
Last active May 16, 2022 12:17
Utilities for using the Parse Platform with nextjs. Prefer this to @parse/react-ssr
import BrowserParse from 'parse';
import ServerParse from 'parse/node';
import _ from 'lodash';
const isBrowser = !!global.window;
const Parse = isBrowser ? BrowserParse : ServerParse;
export default Parse;
export function initialize(serverURL: string, applicationId: string, javascriptKey: string): void {
Parse.serverURL = serverURL;
@dr-skot
dr-skot / createStoredSignal.ts
Last active July 30, 2022 14:33
A SolidJS signal that maintains persistence in local or session storage.
import { createSignal, Signal } from "solid-js";
export function createStoredSignal<T>(key: string, defaultValue: T, storage = localStorage): Signal<T> {
const initialValue = tryToParse(storage.getItem(key), defaultValue);
const [value, setValue] = createSignal<T>(initialValue);
const setValueAndStore = ((arg) => {
const v = setValue(arg);
storage.setItem(key, JSON.stringify(v));
return v;
@dr-skot
dr-skot / useRefWithRerender.ts
Last active July 31, 2022 17:57
Sometimes you want both useRef's never-stale guarantee and useState's rerender-on-change functionality. This hook delivers a ref & a setter that triggers rerender on change.
function useRefWithRerender<T>(initialState: T): [MutableRefObject<T>, (value: T) => void] {
const ref = useRef(initialState);
const [, setState] = useState(true);
const setter = useCallback((value: T) => {
if (ref.current === value) return;
ref.current = value;
setState((n) => !n);
}, []);