Skip to content

Instantly share code, notes, and snippets.

View sagarpanchal's full-sized avatar

Sagar Panchal sagarpanchal

View GitHub Profile
@sagarpanchal
sagarpanchal / useNumberInput.ts
Created May 28, 2024 11:42
React: useNumberInput
const useNumberInput = (ref: React.RefObject<HTMLInputElement>) => {
const stepUp = React.useCallback(() => {
if (!ref.current) return
ref.current.stepUp()
ref.current.dispatchEvent(new Event("change", { bubbles: true }))
}, [ref])
const stepDown = React.useCallback(() => {
if (!ref.current) return
ref.current.stepDown()
@sagarpanchal
sagarpanchal / LoaderService.ts
Last active February 15, 2024 12:38
Zustand Usage Example
import { createStore } from "zustand"
import { subscribeWithSelector } from "zustand/middleware"
export function chain<T>(object: T) {
const call =
<V>(previousResult: V) =>
<U>(callback: (object: T, result: V) => U) => {
const result = callback(object, previousResult)
return { call: call(result), result: () => result, object: () => object }

Problem

I have two Github accounts: oanhnn (personal) and superman (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).

Solution

Use ssh keys and define host aliases in ssh config file (each alias for an account).

How to?

  1. Generate ssh key pairs for accounts and add them to GitHub accounts.
@sagarpanchal
sagarpanchal / tree
Last active January 2, 2024 08:23
TreeNode
# Tree
Tree implementations in different programming languages.
@sagarpanchal
sagarpanchal / grahviz.html
Last active October 27, 2023 13:48
GraphViz
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="author" content="Sagar Panchal | [email protected]" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Graph</title>
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml," />
<script src="https://unpkg.com/@hpcc-js/wasm@2/dist/graphviz.umd.js" type="javascript/worker"></script>
@sagarpanchal
sagarpanchal / logTime.ts
Created October 5, 2023 12:34
Ts log-time
type Callback<T> = (...args: any[]) => T
export function logTime<FR>(func: Callback<FR>, name = "anonymous"): FR {
const t0 = performance.now()
// const logToConsole = process.env.JEST_WORKER_ID === undefined
try {
const output = func?.()
if (output instanceof Promise) {
@sagarpanchal
sagarpanchal / isUrl.ts
Created October 5, 2023 12:23
TS is-url
const rURL =
/^((https?|ftp):)?\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!$&'()*+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!$&'()*+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!$&'()*+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2}
@sagarpanchal
sagarpanchal / isEmpty.ts
Last active October 5, 2023 12:23
TS is-empty
export interface IsEmptyOptions {
isEmpty?: any[]
isNotEmpty?: any[]
}
export function isEmpty(input: any, options: IsEmptyOptions = {}): boolean {
options = { isEmpty: [], isNotEmpty: [], ...options } as IsEmptyOptions
if (options.isEmpty?.includes?.(input)) return true
if (options.isNotEmpty?.includes?.(input)) return false
@sagarpanchal
sagarpanchal / chain.ts
Created October 5, 2023 12:21
TS Chain
export function chain<T>(object: T) {
const call =
<V>(previousResult: V) =>
<U>(callback: (object: T, result: V) => U) => {
const result = callback(object, previousResult)
return { call: call(result), result: () => result, object: () => object }
}
return { call: call(undefined), result: () => undefined, object: () => object }
}
@sagarpanchal
sagarpanchal / map-files.ts
Created October 5, 2023 12:20
Find files
import path from "path"
import fs from "fs-extra"
import ignore from "ignore"
import type { Ignore } from "ignore"
import { minimatch } from "minimatch"
import type { SetOptional } from "type-fest"
const ignoreDirectories = [".*", ".git", ".idea", ".venv", ".vscode", "bin", "node_modules", "obj", "vendor"]
const ignoreBuildDirectories = ["build", "dist", "out", "public"]