View throttle.ts
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 const throttle = <FN extends (...args: Parameters<FN>) => ReturnType<FN>>( | |
fn: FN, | |
delay: number | |
) => { | |
let wait = false | |
let timeout: undefined | number | |
let prevValue: ReturnType<FN> | undefined = undefined | |
const throttleFn = (...args: Parameters<FN>) => { | |
if (wait) { |
View useResizeObserver.ts
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, useState } from 'react' | |
export function useResizeObserver<E extends Element>() { | |
const ref = useRef<E>(null) | |
const [width, setWidth] = useState(0) | |
const [height, setHeight] = useState(0) | |
useEffect(() => { | |
if (!ref.current) { | |
return () => {} |
View useThrottledState.ts
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 { useState, useRef, useEffect, useCallback } from 'react' | |
export type StateSetter<S> = (intermediateState: S) => S | |
// For optimal performance mutate state in setIntermediateState | |
// and pass a custom `makeNewState` for when the final state is | |
// updated. e.g. `obj => {...obj}` or `arr => [...arr]` | |
export function useThrottledState<S>( | |
initialState: S | (() => S), | |
timeout = 300, |
View fastifySchemaExample.ts
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
fastify.get<{ Querystring: { address: string } }>( | |
'/checkAddress', | |
{ | |
schema: { | |
querystring: { | |
type: 'object', | |
properties: { | |
postcode: { type: 'string' }, | |
}, | |
required: ['postcode'], |
View server.ts
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 Fastify from 'fastify' | |
import middie from 'middie' | |
import cors from 'cors' | |
import frameguard from 'frameguard' | |
import xXssProtection from 'x-xss-protection' | |
const fastify = Fastify({ | |
logger: true, | |
}) |
View binarySearch.ts
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 binarySearch<T = any>( | |
findValue: number, | |
items: T[], | |
getValue: (item: T) => number | |
) { | |
let lo = 0 | |
let hi = items.length - 1 | |
while (lo <= hi) { | |
const mi = (lo + hi) >>> 1 |
View package.json
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
{ | |
"main": "dist/index.cjs.js", | |
"module": "dist/index.js", | |
"exports": { | |
".": { | |
"import": "./dist/index.js", | |
"require": "./dist/index.cjs.js" | |
} | |
}, | |
"types": "dist/index.d.ts", |
View useSizeObserver.ts
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 } from 'react' | |
export function useSizeObserver( | |
elRef: React.RefObject<HTMLElement | null>, | |
onChange: (width: number, height: number) => void, | |
shouldObserve = true | |
) { | |
const onChangeRef = useRef(onChange) | |
onChangeRef.current = onChange |
View math.test.ts
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 { round, floor, ceil, roundToTick, floorToTick, ceilToTick } from './math' | |
describe('math', () => { | |
it('round works up to 8 decimals and 999 million', () => { | |
expect(round(123.123456789, 0)).toEqual(123) | |
expect(round(123.123456789, 1)).toEqual(123.1) | |
expect(round(123.123456789, 2)).toEqual(123.12) | |
expect(round(123.123456789, 3)).toEqual(123.123) | |
expect(round(123.123456789, 4)).toEqual(123.1235) | |
expect(round(123.123456789, 5)).toEqual(123.12346) |
View DecayAnimation.ts
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
type UpdateCb = (x: number, y: number) => void; | |
export interface DecayAnimationProps { | |
deceleration?: number; | |
onUpdate: UpdateCb; | |
} | |
export class DecayAnimation { | |
deceleration: number; | |
_onUpdate: UpdateCb; |
NewerOlder