Skip to content

Instantly share code, notes, and snippets.

@shlevy
Created March 9, 2016 02:30
Show Gist options
  • Select an option

  • Save shlevy/3fd96b0c230471bdcf4e to your computer and use it in GitHub Desktop.

Select an option

Save shlevy/3fd96b0c230471bdcf4e to your computer and use it in GitHub Desktop.
var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
while (walker.nextNode()) {
var newText = walker.currentNode.nodeValue.replace(/(\d+\s*)?π/g, function (match, multiplierString) {
var multiplier = parseInt(multiplierString.trim(), 10);
if (isNaN(multiplier)) {
return "τ/2";
} else if (multiplier == 2) {
return "τ";
} else {
return (multiplier / 2).toString() + "τ" + ((multiplier % 2 == 0) ? "" : "/2");
}
});
if (newText != walker.currentNode.nodeValue) {
walker.currentNode.nodeValue = newText;
}
}
@nightpool

Copy link
Copy Markdown

line 11 seems incorrect? i.e. 5π will turn into 2.5τ/2 if i'm reading it correctly.

@shlevy

shlevy commented Mar 20, 2016

Copy link
Copy Markdown
Author

Yeah, I realized that shortly after posting. Didn't expect anyone to find this except through my original post about it, I'm curious how you did?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment