Skip to content

Instantly share code, notes, and snippets.

@romellem
romellem / categorize-test-images.mjs
Last active June 23, 2026 14:04
Prefix images with aspect ratio
#!/usr/bin/env node
import { execSync } from 'node:child_process';
import {
readdirSync,
statSync,
unlinkSync,
existsSync,
mkdirSync,
copyFileSync,
renameSync,
@romellem
romellem / unicode-slice.js
Last active April 22, 2026 18:24
JS String Slice - UTF-8, UTF-16, UTF-32, and Graphemes
function utf16slice(str, start, end) {
// UTF-16 code units - same as native
return str.slice(start, end);
}
function utf8slice(str, start, end) {
// UTF-8 bytes; slicing mid-codepoint yields U+FFFD
const bytes = new TextEncoder().encode(str);
return new TextDecoder().decode(bytes.slice(start, end));
}
@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
*/