Skip to content

Instantly share code, notes, and snippets.

View sagarpanchal's full-sized avatar

Sagar Panchal sagarpanchal

View GitHub Profile
@sagarpanchal
sagarpanchal / npm-wrapper.sh
Created March 13, 2025 14:47
Npm Aws code-artifact integration
function aws_login {
local CONFIG_FILE="$HOME/.aws/config"
# Extract SSO session name
local SSO_SESSION
SSO_SESSION=$(awk -F ' = ' '/^\[sso-session / {gsub(/^\[sso-session |\]$/, "", $0); print; exit}' "$CONFIG_FILE")
# Extract profile name and other required details
local PROFILE
PROFILE=$(awk -F ' = ' '/^\[profile / {gsub(/^\[profile |\]$/, "", $0); print; exit}' "$CONFIG_FILE")
@sagarpanchal
sagarpanchal / setup-husky.sh
Last active April 1, 2025 12:41
Git hooks auto-setup shell script
#!/bin/zsh
# This script automates the process of setting up Husky for an npm package in a git repo.
# It creates the .husky directory, installs Husky, and sets up the pre-commit and post-merge hooks.
# It also updates the package.json with lint-staged configuration.
# Usage: ./setup-husky.sh
set -e
if [ ! -f "$PWD/package.json" ]; then
@sagarpanchal
sagarpanchal / globalRefs.ts
Created December 18, 2024 06:30
Hooks to manage Global Refs
import React from 'react'
declare global {
interface GlobalListsMap {
default: unknown
}
interface Window {
globalListsMap: {
[K in keyof GlobalListsMap]: GlobalListsMap[K] extends unknown ? unknown[] : GlobalListsMap[K][] // Turn types into arrays
@sagarpanchal
sagarpanchal / frozenSet.ts
Created November 19, 2024 13:01
Freeze Set
const frozenSet = (iterable) => {
const s = new Set(iterable);
s.add = s.delete = s.clear = undefined;
return s;
};
const test = frozenSet([1, 2, 3, 1, 2])
test.add(4)
@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) {