Skip to content

Instantly share code, notes, and snippets.

View Munawwar's full-sized avatar
🍪
Cookies

Munawwar Firoz Munawwar

🍪
Cookies
View GitHub Profile
@Munawwar
Munawwar / generate-image.js
Created April 24, 2023 20:24
JSX to PNG via Satori and resvg-js
const htm = require('htm');
const { default: satori } = require('satori');
const { Resvg } = require('@resvg/resvg-js')
const { promises } = require('node:fs');
const { join } = require('node:path')
const html = htm.bind(function jsxToObject(type, props, ...children) {
return {
type,
props: {
@Munawwar
Munawwar / httpPing.js
Created April 7, 2023 17:58
Measure Time to first byte
// time to first byte
const https = require('https');
const http = require('http');
/**
* @param {Object} arg
* @param {String} arg.host
* @param {String} arg.path
* @param {{ [k: string]: string }} arg.headers
*/
@Munawwar
Munawwar / useObjectState.jsx
Last active September 26, 2021 11:02
Object state hook
import { useState } from 'react';
import { merge } from 'lodash';
function useObjectState(defaultVal = {}) {
const [state, setState] = useState(defaultVal);
const updateState = (newState) => {
setState((previousState) => ({ ...previousState, ...newState }));
};
const deepMergeState = (newState) => {
setState((previousState) => merge({}, previousState, newState));
@Munawwar
Munawwar / useAsyncState.js
Last active July 22, 2021 15:23
Async state fetching hook (for react)
import { useEffect, useState } from 'react';
/**
* @template T
* @param {(previousState: T) => Promise<T>} getAsyncState async function to fetch state
* @param {T} initialState
* @param {any[]} [dependencies=[]] similar to useEffect dependencies. change in dependencies will
* automatically call getAsyncState again
* @returns {[T, (newState: T) => void, () => Promise<void>]} [state, setState, refetch]
*/
@Munawwar
Munawwar / Dockerfile
Created June 22, 2021 15:31
node.js + puppeteer minimal docker file
# A minimal Docker file for node.js and Puppeteer
# debian 10 (buster slim) amd64 + node v14
# https://hub.docker.com/layers/node/library/node/14.17.1-buster-slim/images/sha256-10c6bf7204614c18b0734a218f576082ea2d15e9af7b7817a07eddcd7d05a255?context=explore
FROM node:14.17.1-buster-slim@sha256:10c6bf7204614c18b0734a218f576082ea2d15e9af7b7817a07eddcd7d05a255
RUN apt-get update \
# Install dependencies of Chromium that Puppeteer installs
# Dependency list for puppeteer v10.0.0 taken from https://github.com/puppeteer/puppeteer/blob/v10.0.0/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
# Puppeteer v10.0.0 installs Chromium 92.0.4512.0 / r884014
@Munawwar
Munawwar / regex-test.js
Last active April 13, 2021 06:25
Testing the speed of regex replace approach
const whitespace = '[\\x20\\t\\r\\n\\f]';
const unescapeRegExp = new RegExp('\\\\([\\da-f]{1,6}' + whitespace + '?|(' + whitespace + ')|.)', 'ig');
function unescOrig(str) {
return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
const high = '0x' + escaped - 0x10000;
// NaN means non-codepoint
// Workaround erroneous numeric interpretation of +"0x"
// eslint-disable-next-line no-self-compare
@Munawwar
Munawwar / PEM.md
Last active January 17, 2023 06:34
PEM

PEM is a CSS naming convention. Inspired by BEM CSS and Suit CSS naming convention.

P = Prefix
E = Element
M = Modifier

Acceptable CSS class name formats:

@Munawwar
Munawwar / dynamo-lock.js
Last active November 22, 2023 19:11
Distributed Lock with Dynamodb & node.js
// MIT License 2021 - https://opensource.org/licenses/MIT
const delay = require('delay');
/**
* @param {AWS.DynamoDB.DocumentClient} dynamodbDocumentClient
* @param {string} tableName
* @param {string} ownerId a lock's gotta have an owner!
* @param {Object} [options]
* @param {string} [options.hashKeyProperty='key'] default value is 'key'
* @param {string} [options.hashKeyValue='lock'] default value is 'lock'
# pip3 install opencv-contrib==4.5.1.48 opencv-contrib-python==4.5.1.48
import numpy as np
import cv2
src = cv2.imread('sample.jpg', 1)
blurred = cv2.GaussianBlur(src, (5, 5), 0)
blurred_float = blurred.astype(np.float32) / 255.0
# download model from https://github.com/opencv/opencv_extra/blob/5e3a56880fb115e757855f8e01e744c154791144/testdata/cv/ximgproc/model.yml.gz
edgeDetector = cv2.ximgproc.createStructuredEdgeDetection("model.yml")
@Munawwar
Munawwar / zustand-usage.js
Last active October 25, 2020 06:22
Zustand state manager usage
import create, { State, StateSelector } from 'zustand';
// initialization
const initialStates = { count: 0 }; // no need to add your actions here like how zustand README shows.
const useStore = create(() => initialStates);
const { getState, setState } = useStore;
// usage within function component (reactive)
const Component = () => {
const count = useStore(state => state.count);