This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Regex { | |
var pattern: String { | |
didSet { updateRegex() } | |
} | |
var expressionOptions: NSRegularExpressionOptions { | |
didSet { updateRegex() } | |
} | |
var matchingOptions: NSMatchingOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function StopWatch() { | |
let started = Date.now(), | |
time = 0, | |
running = false; | |
function getTime() { | |
return running ? Date.now() - started : time; | |
} | |
function start() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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': |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}, []); |
OlderNewer