Skip to content

Instantly share code, notes, and snippets.

View moinulmoin's full-sized avatar
👀
Available for collaboration

Moinul Moin moinulmoin

👀
Available for collaboration
View GitHub Profile
@moinulmoin
moinulmoin / try-catch.ts
Created March 22, 2025 10:42
Go like error handling for promises in TS
/**
* Wraps a promise and returns a tuple where the first element is the data if successful,
* or null if an error occurred, and the second element is the error if any, or null if successful.
*
* @param promise The promise to wrap.
* @returns A promise that resolves to [data, null] on success or [null, error] on failure.
*/
export async function tryCatch<T, E = Error>(promise: Promise<T>): Promise<[T, null] | [null, E]> {
try {
const data = await promise;
@moinulmoin
moinulmoin / clean_code.md
Created October 22, 2024 15:40 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@moinulmoin
moinulmoin / Dockerfile
Created June 26, 2024 09:47
Dockerfile for Next.js
FROM node:lts-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json pnpm-lock.yaml* ./
@moinulmoin
moinulmoin / easing.css
Created May 13, 2024 17:17 — forked from bendc/easing.css
Easing CSS variables
:root {
--ease-in-quad: cubic-bezier(.55, .085, .68, .53);
--ease-in-cubic: cubic-bezier(.550, .055, .675, .19);
--ease-in-quart: cubic-bezier(.895, .03, .685, .22);
--ease-in-quint: cubic-bezier(.755, .05, .855, .06);
--ease-in-expo: cubic-bezier(.95, .05, .795, .035);
--ease-in-circ: cubic-bezier(.6, .04, .98, .335);
--ease-out-quad: cubic-bezier(.25, .46, .45, .94);
--ease-out-cubic: cubic-bezier(.215, .61, .355, 1);
@moinulmoin
moinulmoin / grokking_to_leetcode.md
Created March 28, 2024 20:33 — forked from tykurtz/grokking_to_leetcode.md
Grokking the coding interview equivalent leetcode problems

GROKKING NOTES

I liked the way Grokking the coding interview organized problems into learnable patterns. However, the course is expensive and the majority of the time the problems are copy-pasted from leetcode. As the explanations on leetcode are usually just as good, the course really boils down to being a glorified curated list of leetcode problems.

So below I made a list of leetcode problems that are as close to grokking problems as possible.

Pattern: Sliding Window

@moinulmoin
moinulmoin / useCountdownTimer.tsx
Created January 11, 2024 16:11
Countdown timer hook
import { useState, useEffect, useRef } from 'react';
type CountdownTimer = {
remainingTime: number;
startTimer: () => void;
stopTimer: () => void;
isRunning: boolean;
};
const useCountdownTimer = (duration: number): CountdownTimer => {
@moinulmoin
moinulmoin / .env
Last active November 12, 2023 13:41
docker postgres service
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=postgres
DATABASE_URL="postgres://${DB_USER}:${DB_PASSWORD}@localhost:5432/${DB_NAME}"
@moinulmoin
moinulmoin / env.mjs
Created September 16, 2023 16:12
Typesafe envs
// ref: https://github.com/webdevcody/online-marketplace/blob/main/src/env.mjs
import { z } from "zod";
/**
* Specify your server-side environment variables schema here. This way you can ensure the app isn't
* built with invalid env vars.
*/
const server = z.object({
DATABASE_URL: z.string().url(),
@moinulmoin
moinulmoin / autoScroll.ts
Created August 30, 2023 14:20
AutoScroll down - Puppeteer Page
// ref: https://stackoverflow.com/a/53527984/14491361
/**
* Scrolls the page automatically until either the end of the page
* @param page - The page to scroll.
* @returns A Promise that resolves when the scrolling is complete.
*/
async function autoScroll(page: Page): Promise<void> {
await page.evaluate(async () => {
await new Promise<void>((resolve) => {
@moinulmoin
moinulmoin / useLocalStorage.js
Created January 25, 2023 11:05
LocalStorage Hook for Nextjs SSR
import { useCallback, useEffect, useState } from 'react';
const useLocalStorage = (key, initialValue) => {
const [state, setState] = useState(null);
useEffect(() => {
const initialize = (key) => {
try {
const item = localStorage.getItem(key);
if (item && item !== 'undefined') {