Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
ValentaTomas / useMouseIndicator.ts
Created February 23, 2023 21:29
Helper hook for creating divs attached to mouse cursor
import { useEffect, useState } from 'react'
export type UpdateIndicator = (x: number, y: number) => void
export function useMouseIndicator(indicatorFactory?: () => { el: HTMLElement; update: UpdateIndicator; }) {
const [isActive, setIsActive] = useState(false)
useEffect(function attach() {
if (!isActive) return
if (!indicatorFactory) return
@ValentaTomas
ValentaTomas / findSequences.ts
Created February 22, 2023 12:30
Find sequences of incremening numbers in a sorted array of integers
function getLast<T>(items: T[]) {
if (items.length > 0) {
return items[items.length - 1]
}
}
/**
* @param items Sorted array of integers
*/
export function findSequences(items: number[]) {
@ValentaTomas
ValentaTomas / popupModal.ts
Created February 20, 2023 14:58
Open a popup modal window
export function openPopupModal(url: URL) {
const features = {
popup: 'yes',
width: 820,
height: 700,
top: 'auto',
left: 'auto',
toolbar: 'no',
menubar: 'no',
}
@ValentaTomas
ValentaTomas / useExpiringState.ts
Created January 16, 2023 17:29
Set state that will expire and revert to the default state after a timeout
import debounce from 'lodash.debounce'
import { useCallback, useMemo, useState } from 'react'
export interface Props<T> {
defaultValue: T
timeout: number // in ms
}
function useExpiringState<T>({
defaultValue,
@ValentaTomas
ValentaTomas / module.d.ts
Created December 11, 2022 19:38
Module declaration for packages without types
declare module 'package-path' {
export function normalExport(): any
export default function defaultExport(): any
}
@ValentaTomas
ValentaTomas / composePlugins.js
Last active November 25, 2022 14:00
Compose Next.js plugins without nesting and additional variables
function isPlugin(maybePlugin) {
return typeof maybePlugin === 'function'
}
function composePlugins(...plugins) {
return (baseConfig) => plugins
.filter(isPlugin)
.reduce((c, plugin) => plugin(c), baseConfig)
}
@ValentaTomas
ValentaTomas / asArray.ts
Created November 8, 2022 17:29
Wrap the argument in an array if it is not already an array
export function asArray<T>(item: T | T[]) {
return Array.isArray(item) ? item : [item]
}
@ValentaTomas
ValentaTomas / script.sh
Last active November 25, 2022 14:02
Best practices for writing shell scripts
#!/usr/bin/env bash
# https://sharats.me/posts/shell-script-best-practices/
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
@ValentaTomas
ValentaTomas / identity.ts
Last active September 21, 2023 08:29
Identify function
export function identity<T>(item: T) {
return item
}
@ValentaTomas
ValentaTomas / sortByOrder.ts
Last active April 19, 2023 11:47
Sort by an order given in an array
export function sortByOrder<O, I>(order: O[], getID: (item: I) => O) {
return (a: I, b: I) => {
const aID = getID(a)
const bID = getID(b)
return order.indexOf(aID) - order.indexOf(bID)
}
}