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 / README.md
Last active July 25, 2020 13:43
Functions only UI POC

A framework that lets you write your entire UI

  • as functions
  • with React hooks
  • without React
  • without classes
  • without vDOM/DOM diffing
  • without web components
  • without build step
@Munawwar
Munawwar / ridiculous-chaining.js
Last active July 3, 2020 17:28
Lodash-like chaining using reduce for Absurdum. Why? Cuz we can..
// Law of Absurd Reduces: Everything that can be implemented with reduce() will eventually be implemented with reduce()
/**
* @param {Object} chainableFunctions object where each key is the name of a function and it's value is the function itself
*/
const createChainables = (chainableFunctions) => ({
chain(initialValue) {
const operations = [];
const evaluate = () => operations.reduce(
(value, [func, ...args]) => func(value, ...args),
@Munawwar
Munawwar / cloud-machines-benchmarks.md
Last active August 21, 2020 07:22
Cloud machines benchmarks

sysbench --test=cpu --cpu-max-prime=20000 run

(all ubuntu 16)

Summary (95 percentile)

Scaleway DEV1-S = 2.40ms
Scaleway VC1M = 2.51ms
Scaleway VC1S = 2.56ms
Local Machine (Mackbook Pro 2015) = 2.66ms
@Munawwar
Munawwar / daisy-chain.js
Last active July 13, 2021 11:51
Lodash chaining without importing the entire library
function chain(value) {
return {
/**
* @param {function|string} func function or function name (in chained value)
* @param {...any} args
*/
fn(func, ...args) {
if (typeof func === 'string') {
return chain(value[func](...args));
}
@Munawwar
Munawwar / axios-auto-cancel.js
Last active February 18, 2021 13:38
Axios call that aborts/cancels the previous call automatically
const postAndCancelPrevCall = (() => {
let cancelTokenInstance;
return async (body) => {
try {
if (cancelTokenInstance) {
cancelTokenInstance.cancel('cancelled by user');
}
cancelTokenInstance = CancelToken.source();
const axiosResponse = await axios.post(body, {
cancelToken: cancelTokenInstance.token
@Munawwar
Munawwar / object-recursion.js
Last active April 5, 2020 09:09
Template for recursively traversing and transforming an object
const { isPlainObject } = require('lodash');
function objectRecursionAndTransformationTemplate(obj) {
if (!obj || !(isPlainObject(obj) || Array.isArray(obj))) {
// put your primitive val transformations here
return obj;
}
if (Array.isArray(obj)) {
// put your array transformations here
return obj.map(objectRecursionAndTransformationTemplate);
@Munawwar
Munawwar / upload.sh
Last active February 29, 2020 07:12
Simple node.js rsync
# exclude node_modules folder and files like .DS_STORE on OSX
rsync -rv --exclude 'node_modules' --exclude '.*' --delete-after --ignore-errors . [email protected]:AppDirectory/
# execute yarn install
ssh [email protected] 'cd AppDirectory && yarn install'
@Munawwar
Munawwar / redis-concurrency-control.js
Last active July 12, 2021 16:22
Redis concurrency control
const redis = require('redis');
const bluebird = require('bluebird');
const redisClient = redis.createClient({});
bluebird.promisifyAll(Object.getPrototypeOf(redisClient));
const luaScript = `
local newPayload = ARGV[1]
local newVersionStr, newData = ARGV[1]:match("^([0-9]+)|(.+)$")
local prevVal = redis.call("get", KEYS[1]) or nil
@Munawwar
Munawwar / leonardo-distribute.js
Created January 16, 2020 07:30
leonardocolor.io contrast distribution
const d3 = require('d3');
const d3hsluv = require('d3-hsluv');
const { generateContrastColors } = require('./leonardo');
Object.assign(d3, d3hsluv);
function interpolateLumArray(newColors) {
const lums = [];
for (let i = 0; i < newColors.length; i += 1) {
@Munawwar
Munawwar / randomlyDistributedHash.js
Last active June 23, 2021 09:37
randomlyDistributedHash
// Deprecated: Dont use this. Just use fnv1a hash
function randomlyDistributedHash(s) {
// fnv1a hash
var h = 0x811c9dc5;
for (var i = 0, l = s.length; i < l; i++) {
h ^= s.charCodeAt(i);
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}
h = (h >>> 0);