Skip to content

Instantly share code, notes, and snippets.

View jayantbh's full-sized avatar
🟣
Building middlewarehq.com

Jayant Bhawal jayantbh

🟣
Building middlewarehq.com
View GitHub Profile
@jayantbh
jayantbh / tailwind.config.js
Created June 15, 2021 15:33
Tailwind Empty Pseudo-Selector Variant
const plugin = require("tailwindcss/plugin");
// tested for tailwindcss 2.1.2
module.exports = {
// other config
variants: {
extend: {
// other variant extensions
display: ["empty"],
@jayantbh
jayantbh / use-frame.js
Created June 21, 2020 16:10
useFrame hook - takes a function, and throttles it using requestAnimationFrame
/* eslint-env browser */
import {useRef} from 'react';
export const useFrame = <T = any>(fn: (...args: T[]) => any) => {
const frameRef = useRef();
return (..._args: T[]) => {
frameRef.current && cancelAnimationFrame(frameRef.current);
frameRef.current = requestAnimationFrame(() => fn(..._args));
@jayantbh
jayantbh / offscreen-canvas-polyfill.js
Last active July 28, 2023 01:32 — forked from n1ru4l/offscreen-canvas-polyfill.js
offscreen-canvas-polyfill.js
if (!window.OffscreenCanvas) {
window.OffscreenCanvas = class OffscreenCanvas {
constructor(width, height) {
this.canvas = document.createElement('canvas');
this.canvas.width = width;
this.canvas.height = height;
this.canvas.transferToImageBitmap = () => {
const ctx = this.canvas.getContext('2d');
if (!ctx) return;
@jayantbh
jayantbh / lgtm.github.md
Last active February 27, 2020 09:21
Github LGTM Card

AMAZING

@jayantbh
jayantbh / example.js
Created February 7, 2019 10:50
JS - Wait for given function to return true before proceeding
import { waitFor } from './waitFor';
let unrelatedStuffValue = false;
async function test() {
const result = await waitFor(() => unrelatedStuffValue);
console.log('Result: ', result); // Result: true
}
async function unrelatedAsyncStuff() {
@jayantbh
jayantbh / package.json
Created September 11, 2018 11:51
Some useful package.json bits
{
"dependencies": {
"node-sass-chokidar": "^1.3.0",
"npm-run-all": "^4.1.3"
},
"scripts": {
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"build-css": "node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/ --watch --recursive",
@jayantbh
jayantbh / SuperBasicTodo.elm
Created May 3, 2018 14:02
The most basic Todo code in Elm
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import List
main = Html.beginnerProgram { model = model, view = view, update = update }
type alias Todo = { text: String }
type alias Model = { todos: List Todo, newTodo: String }
@jayantbh
jayantbh / random-date-in-range.js
Last active September 21, 2017 20:02
Generate a random date within a given range | https://jsfiddle.net/jayantbh/6x2gcnxn/2/
// Basic Random Date Generator
function randomInt(a = 0, b = 0) { return (a + window.crypto.getRandomValues(new Uint8Array(1))[0] % Math.abs(b - a + 1)); }
function log(json) {
for (key in arguments) {
let string = JSON.stringify(arguments[key], null, ' ');
document.body.innerHTML += `<pre>${string}</pre>&nbsp;`;
}
console.log(...arguments);
document.body.innerHTML += '<br>';
@jayantbh
jayantbh / generate-phone.js
Last active April 18, 2017 20:27
Generate random N-digit phone number with or without specific prefixes
let prefix = ['98', '78', '97', '96', '89', '99', '70', '79', '90', '94', '76', '84', '86', '81'];
function randomInt(a = 0, b = 0) { return (a + window.crypto.getRandomValues(new Uint8Array(1))[0] % Math.abs(b - a)).toString(); }
Uint32Array.prototype.unique = function () { return this.filter((v, i) => ( this.indexOf(v) == i )); }
function numbers(n = 16384, usePrefix, len) {
let seed = randomInt(0,99999999);
let numbers = window.crypto.getRandomValues(new Uint32Array(n)).unique();
numbers.forEach((n) => {
let pfx = usePrefix ? prefix[n % prefix.length] : '';
@jayantbh
jayantbh / generate-email.js
Created April 18, 2017 20:10
Generate a random email based on a given name
function randomInt(a = 0, b = 0) { return (a + window.crypto.getRandomValues(new Uint32Array(1))[0] % Math.abs(b - a)).toString(); }
String.prototype.nRepeat = function (n) { return '_'.repeat(n).split('').map(x => this.toString()); }
String.prototype.removeVowels = function (n) { return this.toString().replace(/a|e|i|o|u/g,''); }
function toss() { return Boolean(Math.round(Math.random())); }
let domain = '@gmail.com'.nRepeat(7).concat(['@live.com', '@yahoo.com', '@hotmail.com']); // Giving more weight to gmail domains
let names = ['Tony Stark', 'Bruce Wayne', 'Steve Rogers', 'Clark Kent'];
function randomDomain() { return domain[randomInt(0, domain.length)]; }