Skip to content

Instantly share code, notes, and snippets.

@romellem
romellem / workspaces-parallel.js
Created February 23, 2024 22:17
Yarn workspaces parallel run
const {exec, execSync} = require('child_process');
const util = require('util');
const asyncExec = util.promisify(exec);
const red = (str) => util.format('\x1b[31m%s\x1b[0m', str);
const green = (str) => util.format('\x1b[32m%s\x1b[0m', str);
const magenta = (str) => util.format('\x1b[35m%s\x1b[0m', str);
// @example package.json
@romellem
romellem / whyDidYouUpdate.js
Created April 6, 2023 15:41
WhyDidYouUpdate react class component helper
function comparePrevPropsAndState(prevProps, prevState, logName) {
try {
const defaultLogName = `${this.constructor?.name || ''} Updated:`.trim();
const allPropsKeys = Object.keys(Object.assign({}, prevProps, this.props));
const allStateKeys = Object.keys(Object.assign({}, prevState, this.state));
const changesObj = { props: {}, state: {} };
let somethingChange = false;
for (let [keys, prev, currStore] of [
[allPropsKeys, prevProps, 'props'],
@romellem
romellem / MapWithDefaultValues.js
Created March 21, 2023 19:08
Extends native JS `Map` class to allow for default values when a `key` does not exist on the map
class MapWithDefaultValues extends Map {
constructor(...args) {
super(...args);
this._defaultGetValue = undefined;
}
setDefault(value) {
this._defaultGetValue = value;
return this;
}
@romellem
romellem / example.flow.js
Created November 25, 2022 14:39
Flow argument variance with object's enum value
// @flow
// https://flow.org/try/#0PTAEAEDMBsHsHcBQiAuBPADgU1AUQHYCuAtgIIDGKAlrPqALygDkAhk6AD7MBG7XT5JgG5k6bKACS+AG4sATlRb4UFarQagA3h0ShQxLCgAWsACYAuPETKUa+ADSIOAXxGpMOAMKxZCpStt1Rm1dUABqA2MzSwISVTtHFzdIQnxAulNYKV9FZQAVIyp8AHMAChZ0y2z5XIC1fABKLWdEFLT60EzvHP8CorKK+stumv942ibNFtEPUDyPAHluACsNbX1DEwtmNk5XRHJaAGcUUGN+y3nsJdXgjajt1nZ9xEzqv3zCktLzkoaRLo+Uaffo-L7Ff6IIA
type EnumAction = 'a' | 'b' | 'c';
type InvariantAction = {|
method: EnumAction,
|};
type CovariantAction = {|
@romellem
romellem / INSTRUCTIONS.md
Last active February 27, 2024 00:51
Fix homebrew node installations - dyld: Library not loaded: /usr/local/opt/icu4c/lib/$VERSION

On my unsupported macOS (currently 10.14), whenever I brew upgrade, node breaks with the following error

$ node -v
dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.69.dylib
  Referenced from: /usr/local/opt/node@16/bin/node
  Reason: image not found

If you ls in the /usr/local/opt/icu4c/lib/ directory, you'll see a different version, e.g. libicui18n.71.dylib or whatever.

@romellem
romellem / testMeasureTime.js
Last active July 27, 2022 15:26
NodeJS Measure Execution Time
/**
* @param {String} [str]
*/
const logTime = (() => {
let hrstart, hrend;
return str => {
if (hrstart === undefined) {
hrstart = process.hrtime();
return;
}
@romellem
romellem / .prettierrc
Created December 7, 2021 22:41
My Prettier Config
{
"arrowParens": "always",
"bracketSameLine": false,
"bracketSpacing": true,
"jsxSingleQuote": false,
"printWidth": 100,
"quoteProps": "as-needed",
"semi": true,
"singleQuote": true,
"tabWidth": 4,
@romellem
romellem / useWrappedIndex.js
Last active November 9, 2021 15:13
React hook to allow for "wrapping" index setters
import { useCallback, useState } from 'react';
/**
* @typedef {Object} UseWrappedIndexResult
* @property {Number} index
* @property {Function} prev
* @property {Function} next
* @property {Function} setIndex
*/
@romellem
romellem / INSTRUCTIONS.md
Created October 28, 2021 13:30
Raspberry Pi 4 Model B Rev 1.4 - RetroPie 4.7.1 Bluetooth Setup for 8bitdo Pro 2 Wireless Controller

I was setting up a new RetroPie 4.7.1 build to connect to a new 8bitdo Pro 2 controller.

However, almost every time I'd open Bluetooth config and try to Pair and Connect to Bluetooth Device, I'd see Searching... followed by this error

No devices were found. Ensure device is on and try again

Power cycling the controller / raspberry pi did nothing.

Occasionally it would find my device, but then when I'd try and select the DisplayYesNo security mode option, the computer would freeze.

@romellem
romellem / node_native_color.js
Last active March 13, 2024 18:14
Simple Node.js Color Formatting in console.log()
const util = require('util');
const black = (s) => util.format('\x1b[30m%s\x1b[0m', s);
const red = (s) => util.format('\x1b[31m%s\x1b[0m', s);
const green = (s) => util.format('\x1b[32m%s\x1b[0m', s);
const yellow = (s) => util.format('\x1b[33m%s\x1b[0m', s);
const blue = (s) => util.format('\x1b[34m%s\x1b[0m', s);
const magenta = (s) => util.format('\x1b[35m%s\x1b[0m', s);
const cyan = (s) => util.format('\x1b[36m%s\x1b[0m', s);
const white = (s) => util.format('\x1b[37m%s\x1b[0m', s);