Skip to content

Instantly share code, notes, and snippets.

@ali-master
ali-master / regexp.ts
Created June 8, 2025 12:56
Javascript Regex patterns for Address, Birthday, Email, Driver License, ...
const boundaryPrefix = String.raw`(^|\s)`
const boundarySuffix = String.raw`(\s|$)`
/*** Patterns ***/
// Address patterns
const zipPattern = String.raw`\d{5}(?:-\d{4})?`
const cityPattern = String.raw`(?:[A-Z][a-z.-]+[ ]?){0,20}`
const stateAbbrvPattern = String.raw`AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY`
const cityStateZipPattern = String.raw`${cityPattern},\s*(?:${stateAbbrvPattern}),?\s*${zipPattern}`
const streetSuffixPattern = String.raw`Avenue|Lane|Road|Boulevard|Place|Drive|Street|Ave|Dr|Rd|Blvd|Ln|St|Pl`
@ali-master
ali-master / idb-store.ts
Created May 19, 2025 21:21
Javascript Async Data Storage in Browser
import { get, set, del, setMany, keys } from 'idb-keyval'
export class IDBStore {
private memoryStore: Record<string, string> = {}
private useIndexedDB: boolean
constructor() {
this.useIndexedDB = typeof window !== 'undefined' && !!window.indexedDB
}
@ali-master
ali-master / play-notification-sound.ts
Created May 19, 2025 20:07
Play a Notification Sound in Browser JS
export const playNotificationSound = (audioContext: AudioContext) => {
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
const options = {
type: 'sine' as OscillatorType,
freq: [
@ali-master
ali-master / use-keyboard-shortcut.hook.tsx
Last active May 18, 2025 15:57
A React hook to trigger a callback function when a specific keyboard shortcut is pressed globally.
import { useCallback, useEffect } from "react";
/**
* Represents the parsed details of a keyboard shortcut.
*/
type KeyCombo = {
key: string; // Original key name (e.g., 'e', 'enter') - useful for display
code?: string | null; // Expected KeyboardEvent.code (e.g., 'KeyE', 'Enter') - used for matching
ctrl: boolean; // True if Ctrl key is required
meta: boolean; // True if Meta key (Cmd/Win) is required
@ali-master
ali-master / README.md
Created May 14, 2025 13:07
Converting circular structure to JSON in JS

When does this error occur?

This error occurs when you try to convert a JavaScript object to JSON, but the object contains a circular reference. A circular reference occurs when an object refers to itself, directly or indirectly.

@ali-master
ali-master / keyfleur-poetic-generator.md
Last active May 29, 2025 22:57
Generate beautiful, memorable, and poetic API keys and unique identifiers for your applications.

Keyfleur Poetic Generator

TypeScript License

Generate beautiful, memorable, and poetic API keys and unique identifiers for your applications.

Overview

Keyfleur creates human-readable, aesthetically pleasing API keys and unique identifiers inspired by natural language patterns and poetry. Unlike traditional random strings, Keyfleur keys are designed to be both functional and beautiful.

@ali-master
ali-master / crypto-subtle-hmac.ts
Created April 23, 2025 23:35
Javascript HMAC with Crypto Subtle example
import * as b64ArrBufferConvertor from "base64-arraybuffer";
const SECRET_HMAC_KEY =
"209C5BBE79E2752FD1E4E687AD410778DD098546CC9B15ECAB1AEB4F21A46EF2";
async function importHmacSecretKey(secret: string) {
return crypto.subtle.importKey(
"raw",
new TextEncoder().encode(secret),
{ name: "HMAC", hash: "SHA-256" },
false,
@ali-master
ali-master / README.md
Created April 4, 2025 17:38
setTimeout caps out at ~25 days. If you need a bigger timeout, use setBigTimeout.

Javascript Big Timeout

setTimeout caps out at ~25 days. If you need a bigger timeout, use setBigTimeout.

import { setBigTimeout } from "./mod.mts";

const FORTY_DAYS_IN_MILLISECONDS = 3.456e9;
@ali-master
ali-master / solution.js
Created January 14, 2025 21:06
Disable Right-Click Using JavaScript
document.addEventListener("contextmenu", function(event) {
event.preventDefault();
alert("Right-click is disabled on this website!");
});
// Disable Right-Click for Specific Elements Only
document.getElementById("protected-content").addEventListener("contextmenu", function(event) {
event.preventDefault();
});
@ali-master
ali-master / MinHeap.md
Created December 29, 2024 23:35
Top-K Data Structure in Javascript

1. Min-Heap Implementation in TypeScript

We first define a generic MinHeap class with the following operations:

  • size(): Returns the number of elements in the heap.
  • peek(): Returns the minimum (root) without removing it.
  • push(value): Inserts a value, keeping the heap structure.
  • pop(): Removes and returns the minimum (root).
  • heapifyUp() and heapifyDown(): Internal methods to maintain heap order.