Skip to content

Instantly share code, notes, and snippets.

View mmazzarolo's full-sized avatar
🐌
Busy — I'll be slow to respond!

Matteo Mazzarolo mmazzarolo

🐌
Busy — I'll be slow to respond!
View GitHub Profile
@mmazzarolo
mmazzarolo / runtime-globals-checker.js
Last active June 8, 2023 14:27
Find what JavaScript variables are leaking into the global `window` object at runtime (see: https://mmazzarolo.com/blog/2022-02-14-find-what-javascript-variables-are-leaking-into-the-global-scope/)
/**
* RuntimeGlobalsChecker
*
* You can use this utility to quickly check what variables have been added (or
* leaked) to the global window object at runtime (by JavaScript code).
* By running this code, the globals checker itself is attached as a singleton
* to the window object as "__runtimeGlobalsChecker__".
* You can check the runtime globals programmatically at any time by invoking
* "window.__runtimeGlobalsChecker__.getRuntimeGlobals()".
*
/**
* GlobalsDebugger
*
* Inspect the stack when a global variable is being set on the window object.
* Given a global variable name, it proxies the variable name in the window
* object adding some custom code that will be invoked whenever the variable
* is set. The custom code will log the current stack trace and halt the code
* execution to allow inspecting the stack and context in your browser DevTools.
* You can use the "globalsToInspect" query-parameter to set a comma-separated
* list of names of the variables you want to inspect.
@mmazzarolo
mmazzarolo / fill-localstorage.ts
Last active April 2, 2023 14:00
Using JavaScript to fill localStorage to its maximum capacity
/**
* Fill localStorage to its maximum capacity.
*
* First, we find the highest order bit (the bit with the highest place value)
* that fits in localStorage by storing localStorage an increasingly big
* string of length 2, 4, 8, 16, etc. until it won't fill localStorage anymore.
*
* By working in iterations, starting with very long strings, and storing data
* in different items, we can keep a low memory profile and reduce the number of
* writes — making this process pretty fast.
@mmazzarolo
mmazzarolo / localstorage-error-handling.ts
Created June 29, 2022 16:49
Handling localStorage errors (such as quota exceeded errors)
/**
* Determines whether an error is a QuotaExceededError.
*
* Browsers love throwing slightly different variations of QuotaExceededError
* (this is especially true for old browsers/versions), so we need to check
* different fields and values to ensure we cover every edge-case.
*
* @param err - The error to check
* @return Is the error a QuotaExceededError?
*/
@mmazzarolo
mmazzarolo / nativewind+2.0.11.patch
Created February 4, 2023 13:55
Fix Nativewind 2.0.11 stale `colorScheme`
diff --git a/node_modules/nativewind/dist/style-sheet/color-scheme.js b/node_modules/nativewind/dist/style-sheet/color-scheme.js
index c08e40b..0505525 100644
--- a/node_modules/nativewind/dist/style-sheet/color-scheme.js
+++ b/node_modules/nativewind/dist/style-sheet/color-scheme.js
@@ -7,6 +7,16 @@ class ColorSchemeStore {
this.colorSchemeListeners = new Set();
this.colorScheme = react_native_1.Appearance.getColorScheme() || "light";
this.colorSchemeSystem = "system";
+ react_native_1.Appearance.addChangeListener(({ colorScheme }) => {
+ if (this.colorSchemeSystem === "system") {
@mmazzarolo
mmazzarolo / country-codes.ts
Created May 6, 2023 12:09
WIP - Type-safe TypeScript ISO 3166 country codes (names, two-letters codes, and three-letters codes)
// ==========================================
// (WIP) TYPE-SAFE ISO 3166 COUNTRY CODES
// ==========================================
/**
* Countries data.
* This is the source that should be updated if you want to add/remove new country codes.
*/
const countriesData = [
["Afghanistan", "AF", "AFG"],
@mmazzarolo
mmazzarolo / transform-barrel-file-imports.ts
Created November 10, 2024 19:37
Codemod to kill barrel file references
/**
* This script/codemod transforms imports from barrel files into direct imports.
* It's designed to work with jscodeshift to modify TypeScript/JavaScript files.
*
* Features:
* - Handles multiple barrel files
* - Transforms both value and type imports
* - Maintains correct relative paths
* - Handles re-exports within barrel files
*