Skip to content

Instantly share code, notes, and snippets.

@night-fury-rider
night-fury-rider / typescript-generics.tsx
Created September 28, 2025 14:22
TypeScript - Generics
import React from "react";
import { Select, MenuItem, FormControl, InputLabel, Box } from "@mui/material";
interface MovieFilterProps<T extends string | number> {
primaryOptionLabel?: string;
primaryOptionValue?: T;
options: T[];
selectedFilter: T;
setSelectedFilter: React.Dispatch<React.SetStateAction<T>>;
title: string;
@night-fury-rider
night-fury-rider / performance-throttling.js
Created May 16, 2025 03:30
Performance - Throttling
// Make sure we have a input field with id "inputData" in our DOM.
const inputData = document.getElementById("inputData");
const getDataFromServer = (name) => {
console.log(`${name} :: getting Data from Server...........`);
};
const throttle = (callback, delay) => {
delay = delay || 2000;
@night-fury-rider
night-fury-rider / performance-debounce.js
Created May 16, 2025 03:17
Performance - Debounce
// Make sure we have a input field with id "inputData" in our DOM.
const inputData = document.getElementById("inputData");
const getDataFromServer = (name) => {
console.log(`${name} :: getting Data from Server...........`);
};
const debounce = (callback, delay) => {
delay = delay || 2000;
@night-fury-rider
night-fury-rider / optimization-memoization.js
Last active May 15, 2026 02:30
Optimization - Memoization
const memoize = (callback, limit = 100, delimiter = "\u00B7") => {
if (typeof callback !== "function") {
throw new TypeError("Memoize expects a function");
}
const cache = new Map();
return (...args) => {
const cacheKey = args.join(delimiter); // To get key without collision
if (cache.has(cacheKey)) {
console.log(`Retrieving from cache for ${cacheKey}`);
@night-fury-rider
night-fury-rider / concurrency-pattern-async-queue.js
Last active May 6, 2026 02:09
Design Pattern - Async Queue
class AsyncQueue {
constructor() {
this.values = []; // To hold actual values
this.callbacks = []; // To hold the callbacks
}
/**
* If the callbacks array has something, remove it and execute it.
* Otherwise, add the value to the values array.
*/
@night-fury-rider
night-fury-rider / design-pattern-observer.js
Last active May 16, 2026 05:53
Design Pattern - Observer
class Observable {
#observers = new Set();
subscribe(observer) {
// Only support function as callback
if (typeof observer !== "function") {
throw new TypeError("Observer must be a function");
}
this.#observers.add(observer);
return () => {
@night-fury-rider
night-fury-rider / styles.css
Last active May 13, 2025 03:14
CSS - Common Styles
body {
background-color: skyblue;
margin: 20px;
height: 90%;
}
.flex_center {
display: flex;
align-items: center;
justify-content: center;
@night-fury-rider
night-fury-rider / vscode-mac-snippets-html.json
Last active May 13, 2025 02:53
Visual Studio Code - Mac - HTML Boilerplate Snippets
// Make these changes in the ~/Library/Application Support/Code/User/snippets/html.json
// Place your snippets for html here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
@night-fury-rider
night-fury-rider / concurrency-pattern-pubsub.js
Last active May 16, 2026 03:48
Concurrency Pattern - Pub/Sub
/**
* PubSub (Publish-Subscribe) Pattern Implementation
*
* A lightweight event bus that decouples producers (publishers) from
* consumers (subscribers). Publishers emit named events with data;
* subscribers register callbacks that fire when those events are published.
*/
class PubSub {
#events = new Map();
@night-fury-rider
night-fury-rider / jest-render-test-component-using-method.test.ts
Last active February 12, 2025 04:51
jest - Render Test Component using Method
import { Provider } from "react-redux";
import thunk, { ThunkMiddleware } from "redux-thunk";
import { render, RenderAPI } from "react-test-renderer";
import configureMockStore from "redux-mock-store";
import TestComponent from "./TestComponent";
import { describe } from "node:test";
const middlewares: Array<ThunkMiddleware> = [thunk];
const createMockStore = configureMockStore(middlewares);