Skip to content

Instantly share code, notes, and snippets.

View SagnikPradhan's full-sized avatar
💕
Hope you have a lovely day!

Sagnik Pradhan SagnikPradhan

💕
Hope you have a lovely day!
View GitHub Profile
@SagnikPradhan
SagnikPradhan / useForm.ts
Last active January 8, 2021 05:16
🎣 useForm hook. Simple as it can get
import { Dispatch, useReducer } from "react"
import * as z from "zod"
/**
* useForm hook
*/
export function useForm<
F extends string,
A,
B extends z.ZodTypeDef,
@SagnikPradhan
SagnikPradhan / calendar.tsx
Created November 21, 2020 12:09
Crooked code for calendar
import React, { useReducer, useState } from "react"
import styled from "styled-components"
import { addMonths, getDaysInMonth, startOfMonth, getDay, endOfMonth, addDays, isEqual } from "date-fns"
const WEEK_DAYS = [ "S", "M", "T", "W", "T", "F", "S" ] as const
const CalendarCard = styled.div`
display: flex;
flex-direction: column;
@SagnikPradhan
SagnikPradhan / eslintrc.json
Created November 22, 2020 05:12
🔱ESlint Configuration
{
"$schema": "https://json.schemastore.org/eslintrc",
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
@SagnikPradhan
SagnikPradhan / error.ts
Last active January 28, 2021 04:32
💀 Custom error and error handler
export class AppError extends Error {
public readonly name: string;
public readonly isOperational: boolean;
public readonly cause?: Error;
public readonly additionalProps: { [additionalProps: string]: unknown };
constructor({
name,
message,
isOperational,
@SagnikPradhan
SagnikPradhan / logger.ts
Last active January 25, 2021 02:59
📢 Custom logger
import { blue, green, red, yellow } from "colorette";
import { relative } from "path";
// @ts-expect-error
import stringify from "string.ify";
interface IObject<V> {
[K: string]: V;
}
@SagnikPradhan
SagnikPradhan / callsite.ts
Last active January 29, 2021 12:30
Spent all day working on this
import fs from "fs";
import path from "path";
import { SourceMapConsumer } from "source-map";
type Nullable<T> = T | null;
export interface ParsedCallSite {
fileName: Nullable<string>;
line: Nullable<number>;
column: Nullable<number>;
@SagnikPradhan
SagnikPradhan / client.ts
Created April 7, 2021 03:00
🏗️ Server plugin system
import discord from 'eris'
import { Module, Server } from '../../server'
export interface DiscordEvents {
clientConnected: []
}
interface DiscordModuleInjected {
startClient: () => Promise<void>
}
class F {
f = "I am F"
}
class A extends F { a = "I am a" }
class B extends F { b = "I am b" }
class C extends F { c = "I am c" }
interface Something<T extends { [i: string]: F } = {}> {
register: <K extends string, C extends F>(name: K, classInstance: C) => Something<T & { [key in K]: C }>
@SagnikPradhan
SagnikPradhan / chain.ts
Created June 22, 2021 16:46
⛓️ Chain functions as arrays
export type Fn<Input = any, Output = any> = (input: Input) => Output;
export type Chain<Fns extends Fn[]> =
// We chain two functions at one time
Fns extends [infer Fn1, infer Fn2, ...infer RestFns]
? // Make sure two elements are functions
Fn1 extends Fn<infer Fn1Argument>
? Fn2 extends Fn<infer Fn2Argument>
? RestFns extends Fn[]
? // Rest of the elements exist
import "dotenv/config";
import { Command } from "commander";
import React from "react";
import { render } from "ink";
import { addOptions } from "./helpers/cli/options";
import { PushCommand } from "./commands/push";
const program = new Command();