Created
October 13, 2021 20:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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