Skip to content

Instantly share code, notes, and snippets.

@stembrain
Created October 13, 2021 20:09
Show Gist options
  • Save stembrain/d774536def0683b945a4c52351003419 to your computer and use it in GitHub Desktop.
Save stembrain/d774536def0683b945a4c52351003419 to your computer and use it in GitHub Desktop.
adds commas / international formatting to all numbers on the page. probably has a few small bugs
//thanks to rick maisano
var ignoreNodes = new Set(['INPUT', 'TEXTAREA']);
function walk(node) {
if (ignoreNodes.has(node.tagName)) {
return;
}
switch (node.nodeType) {
// Element
case 1:
// Document
case 9:
// Document fragment
case 11: {
let next, child = node.firstChild;
while (child !== null) {
next = child.nextSibling;
walk(child);
child = next;
}
break;
}
// Text node
case 3: {
node.nodeValue = node.nodeValue.replace(/(\d+\.?\d*)/, function replacer(match) {
return new Intl.NumberFormat('en-US').format(match)
});
break;
}
default: {
}
}
}
walk(document.body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment