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 / js_utility_functions.js
Created April 3, 2016 14:02
Some commonly used JS utility functions I use regularly.
Array.prototype.getItemCount = function (val) {
var count = 0, i = -1;
while ((i = this.indexOf(val, i + 1)) != -1) {
count++;
}
return count;
};
Array.prototype.sortObjectsWithSingleKeyByValue = function (isAscending) {
return (isAscending) ? this.sort((a, b) => a[Object.keys(a)[0]] - b[Object.keys(b)[0]]) : this.sort((a, b) => b[Object.keys(b)[0]] - a[Object.keys(a)[0]]);
@jayantbh
jayantbh / exec_family_and_printing.c
Last active April 27, 2016 15:54
Exec family of functions usage demo.
//exec() family of functions
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv){
pid_t id;
id = vfork();
if(id == 0){
@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)]; }
@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 / 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 / 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 / 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 / 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 / lgtm.github.md
Last active February 27, 2020 09:21
Github LGTM Card

AMAZING

@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;