Skip to content

Instantly share code, notes, and snippets.

View HichamBenjelloun's full-sized avatar

Hicham Benjelloun HichamBenjelloun

View GitHub Profile
@HichamBenjelloun
HichamBenjelloun / instructions-vmware-workstation-pro-16-on-fedora-36.sh
Last active June 2, 2024 19:49
How to install VMware Workstation Pro 16.2.3 on Fedora 36
# Install VMware Workstation Pro 16.2.3 on Fedora 36
# Install packages to build kernel modules
sudo dnf install @development-tools
sudo dnf install kernel-headers-$(uname -r) kernel-devel-$(uname -r) dkms elfutils-libelf-devel qt5-qtx11extras
# Download VMware Workstation Pro here: https://www.vmware.com/go/getworkstation-linux
# Install VMware Workstation Pro
chmod +x VMware-Workstation-Full-16.2.3-19376536.x86_64.bundle
@HichamBenjelloun
HichamBenjelloun / ionic-react-routes.test.jsx
Last active April 28, 2022 05:45
Testing Ionic React routing
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { createMemoryHistory } from 'history';
import { IonRedirect, IonRoute, IonRouterOutlet } from '@ionic/react';
import { IonReactRouter } from '@ionic/react-router';
import '@testing-library/jest-dom';
// Using `IonRouterOutlet` here will make `jsdom` render the following DOM tree:
// <body>
@HichamBenjelloun
HichamBenjelloun / findDays.js
Last active January 5, 2022 02:27
Find days of the year with a certain sum of digits
const nthDay = (n, year) => {
const firstDay = new Date(`${year}`);
const newDay = new Date(firstDay.getTime());
newDay.setDate(firstDay.getDate() + n);
return newDay;
};
const isLeapYear = (year) =>
((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
@HichamBenjelloun
HichamBenjelloun / max-concurrent-promises-gen.js
Created May 1, 2021 13:49
Limiting the number of concurrent running promises streamed via a generator
const DEFAULT_MAX_CONCURRENT_PROMISES = 5;
/**
* Runs a stream of promises and ensures only `max` promises are run at a time
* @param {*} promiseRunnerGen
* @param {*} max
*/
const run = (promiseRunnerGen, max = DEFAULT_MAX_CONCURRENT_PROMISES) => {
const iterator = promiseRunnerGen();
let runningPromiseCount = 0;
@HichamBenjelloun
HichamBenjelloun / max-concurrent-promises.js
Last active April 30, 2021 23:56
Limiting the number of concurrent running promises
let runningPromiseCount = 0;
const examplePromiseRunnerQueue = new Array(10)
.fill(0)
.map((_, idx) => idx)
.map((idx) => {
return async () => {
console.log(
`[${new Date().toISOString()}] Promise ${idx} will run! \n# of running promises: ${runningPromiseCount}`
);
@HichamBenjelloun
HichamBenjelloun / rearrange.js
Last active April 5, 2021 13:51
Rearrange object structure
import _ from 'lodash';
const isPlainObject = value =>
value === Object(value) &&
!Array.isArray(value);
function* leaves(node, path = []) {
if (!isPlainObject(node)) {
return yield {value: node, path};
@HichamBenjelloun
HichamBenjelloun / memoize-recursive.js
Last active January 2, 2022 21:38
Memoizing recursive functions
const memoizeFactory = hashFn => fn => {
const memoize = fn => {
const cache = {};
return (...args) => {
const key = hashFn(args);
if (key in cache) {
return cache[key];
}
@HichamBenjelloun
HichamBenjelloun / curry.js
Last active July 26, 2020 01:05
Currying functions
/**
* Transforms a function of the form:
* fn = (p1, ..., pN) => f(p1, ...pN)
* to its curried form:
* curried = p1 => p2 => ... => pN => fn(p1, p2, ..., pN);
*
* @param fn
* @returns the curried form of fn
*/
const curry = fn =>
@HichamBenjelloun
HichamBenjelloun / sieveOfEratosthenes.js
Last active July 10, 2020 14:51
Sieve of Eratosthenes
/**
* Initializes an empty sieve
*
* @returns {{primes: Set<number>, crossed: Set<number>, greatestComputedMultiples: Map<number, number>}}
*/
const createSieve = () => ({
primes: new Set(),
crossed: new Set(),
greatestComputedMultiples: new Map(), // `prime` -> greatest computed multiple for `prime`
});
@HichamBenjelloun
HichamBenjelloun / parseCookies.js
Last active June 25, 2020 18:27
A function that parses a string representation of a list of cookies into an object
const SEP_KEYVAL = '='; // represents a separator between a key and the corresponding value
const SEP_COOKIES = ';'; // represents a separator between two adjacent cookies
const parseKeyValuePair = keyValuePairStr => {
const [key, value] =
keyValuePairStr
.trimStart()
.split(SEP_KEYVAL);
return [key, decodeURIComponent(value)];