JavaScript unit conversion gadget brings lots of RegEx fun
posted 2009-06-07 20:39:38, link to this article
My girlfriend started a cooking blog recently, if you want to keep track of what I eat, I suggest you subscribe to her feed. ;)
To help her American followers (and others suffering under the burden of a unit system from the
middle ages), I kludged together a little gadget to add to her Blogger pages. The gadget tries
to convert a few metric units to something equivalent in stones, feet and that Fahrenheit thing.
Doing so, I learned a nifty thing about JavaScript regular expressions. The replace method in JavaScript 3 makes it possible to call a function to return the replacement string. This way, I can match value, a possible prefix and unit, make backreferences and pass those to a conversion function and replace the matched text in the blog posts with converted values in one go, like so:
var re = /(\d+|\u00bd) ?([mcdk]|(?:mili|centi|deci|kilo))?(g(?:r|ram)?|l(?:iter)?|c(?:elcius)?)\b/gi;
function knvrtit() {
var entries = document.getElementsByClassName('entry-content');
for (var i = 0; i < entries.length; i++) {
entries[i].innerHTML = entries[i].innerHTML.replace(re, das_Konvertor);
};
}
But, what is that unicode \u00bd, you say? Oh, well as it happens some keyboard layouts have a 1/2 character and some people like to use it as well....
Das_Konvertor() then does it's magic using a case construct to multiply values based on the
prefix of a unit, and then uses a second case construct to decide to convert in what way based on the
type of unit encountered.
The code for das_Konvertor() is a bit long, but it looks somewhat like this:
function das_Konvertor (str, value, prefix, unit, offset, s) {
// do stuff
return string_in_imperical_units;
}
It would be fun, and very web2.0ish to, instead of doing my own ugly conversions, pass the calculation to be done to the almighty Google calculator in an XMLHttpRequest and display the result. If it keeps raining in the weekends, I might do so. ;)
JavaScript Cellular Automata Simulation of Fluid-flow
posted 2009-05-23 11:13:28,
link to this article
Read full article
Yesterday, I tried to make a simple Cellular Automaton-like discrete simulation of fluid-flow. Or I am not even sure flow is the right word, it simulates waves coming from the left, with an adjustable frequency, hitting obstacles in their path.
My simulation is by no means attempting to be correct in any way but it is nice to play with and I even spotted something resembling the canceling of waves when passing to a double slit.
Most of the work went into convincing JavaScript to do what I wanted, without being able to use Firebug due to some obscure bug. Maybe I'll write some more about the code behind this in a later article.
I also plan to make it possible to save the configuration of the obstacles in a later version.
Follow the read link to give it a try.
isquared.nl rss (atom)