Skip to content

Instantly share code, notes, and snippets.

View Script.tsx
// just a reminder of a nice API for a <Script /> component,
// reference: https://nextjs.org/docs/basic-features/script#executing-code-after-mounting-onready
import { useRef } from 'react'
import Script from 'next/script'
export function Home() {
const mapRef = useRef()
return (
<>
View useDebouncedCallback.ts
import { useEffect, useMemo } from 'react';
import debounce from 'lodash/debounce';
export function useDebouncedCallback(
callback: (...args: any) => any,
delay: number,
) {
const debouncedCallback = useMemo(
() => debounce(callback, delay),
[callback, delay],
@everdimension
everdimension / useStaleQuery.ts
Created Jul 20, 2022
A helper on top of useQuery hook
View useStaleQuery.ts
import { useCallback, useState } from 'react';
import { useQuery } from 'react-query';
import type {
QueryKey,
QueryFunction,
UseQueryOptions,
UseQueryResult,
} from 'react-query';
/**
@everdimension
everdimension / git-rebase-onto.sh
Created May 20, 2020
"git rebase --onto" — used to rebase only latest (or several latest) commits onto a new branch
View git-rebase-onto.sh
# This is just a note to self
git rebase --onto <base-branch> <current-branch>~ <current-branch>
# e.g. git rebase --onto master feature/something~ feature/something
# it's important here to use branch name and not other commit id.
# Can't use "HEAD" instead of "feature/something" ¯\_(ツ)_/¯
@everdimension
everdimension / gist:590395dd20831f9a787101c4c3084085
Created May 20, 2020
"git rebase --onto" — used to rebase only latest (or several latest) commits onto a new branch
View gist:590395dd20831f9a787101c4c3084085
# This is just a note to self
git rebase --onto <base-branch> <current-branch>~ <current-branch>
# e.g. git rebase --onto master feature/something~ feature/something
# it's important here to use branch name and not other commit id.
# Can't use "HEAD" instead of "feature/something" ¯\_(ツ)_/¯
@everdimension
everdimension / collect-html-form-errors.js
Created Jul 31, 2019
Collect all errors of an html form. The "errors" object can be serialized.
View collect-html-form-errors.js
/**
* Considering that each html input
* has a "name" attribute and proper
* validation attributes (type, pattern, required, etc)
* we collect "validity" states of invalid inputs
* and the "validationMessage" provided by the browsers
*/
const errors = Array.from(form.elements)
.filter((el) => el.hasAttribute('name'))
.reduce((acc, el) => {
@everdimension
everdimension / sequential-polling.ts
Last active Apr 19, 2019
Polling: "make request — wait for response, but no less than 1 second — make another request"
View sequential-polling.ts
export function sequentialPolling({
taskFn,
interval = 1000,
}: {
taskFn: () => Promise<any>;
interval?: number;
}) {
let isActive = true;
let timeoutId: number | undefined;
let rejector: Function | undefined;
@everdimension
everdimension / git-cleanup-approaches.md
Created Apr 17, 2019
Different git branch cleanup approaches
View git-cleanup-approaches.md

Cleanup merged branches

Source: https://stackoverflow.com/a/6127884/3523645

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Cleanup orphaned branches

Problem: If pull-request are merged into the main branch using a "squash" strategy and then the remote branches are removed,

@everdimension
everdimension / normalizedIncludes.ts
Created Oct 18, 2018
Detect occurrence of a string in another string by normalizing both strings and ignoring diacritical marks
View normalizedIncludes.ts
function normalizedIncludes(str1: string, str2: string) {
/**
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
*
* https://stackoverflow.com/a/37511463/3523645
*/
const diacriticsRange = /[\u0300-\u036f]/g;
const str1Normalized = str1.normalize('NFD').replace(diacriticsRange, '');
const str2Normalized = str2.normalize('NFD').replace(diacriticsRange, '');
return str1Normalized.includes(str2Normalized);