World's Simplest Code Generator: implemented in pure Javascript
the core of the program is hardly ten lines long, two simple functions:
function calculate() {
//split the data into an array of rows
var dataRows = $('txtData').value.split($('rowDelim').value);
for (var i = 0; i < dataRows.length;i++) {
//apply the pattern to each row
result += apply($('txtPattern').value, dataRows[i], $('fieldDelim').value) + '\r\n';
}
}
function apply(pattern, row, fieldDelim) {
//split the row into an array of fields
var fields = rows.split(fieldDelim);
for (var col = fields.length; col > 0; col --) {
//replace '$1' with fields[0]... and so on for each field.
//(applied in descending order, so that, for example, '$10' isn't mis-interpreted as '$1')
pattern = pattern.replace(new RegExp('\\$' + col, "g") , trim(fields[col-1]));
}
return pattern;
}
The actual code is spaced out a little more (lines aren't nearly so long)
Because it requires no postbacks, this version of the WSCG is the fastest yet. It doesn't have all the features of the previous version but i think i can use eval() to make it far more powerful yet.
Now something else I've been thinking though, is that I'd love to rewrite this in F#. Anyone care to help out? A silverlight implementation would also be fun. I've written a powershell version (in one horribly long line...) and i should blog that some time.
(Side bar: there was a good hanselminutes about F# recently, where Robert Pickering was interviewed.)
As it turns out, the thing I found hardest about writing this without internet access was testing the seemingly simple regular expressions used in the little 'trim' functions i had to write:
function ltrim(input)
{
//if it starts with spaces... remove them!
return input.replace(/^\s+/gm,'')
}
function rtrim(input)
{
//if it ends with spaces... remove them!
return input.replace(/\s+$/gm,'')
}
function trim(input) {
//trim spaces from the front and the back of the string.
return ltrim(rtrim(input));
}
If I'd had internet access, I would've used David Seruyange's excellent regex testing tool.
One last thing: it felt unnatural to write a descending for statement:
for (var col = fields.length; col > 0; col --)
Have gotten very used to foreach.
'Matt Casto' on Wed, 15 Aug 2007 10:26:39 GMT, sez: Hurray! Another version of my favorite programming tool on the internet. You need to take away your internet access more often!
I hadn't seen that regex testing tool before. I've added it to my links and I'll find it helpful for quick regex verification. I usually use Regulator which I keep on a thumb drive. In fact, most of my development tools I have as portable application or installs stored on my thumb drive so I don't ever end up on a machine without any development tools.
'Des Traynor' on Wed, 15 Aug 2007 11:12:30 GMT, sez: Nice work, I've been using WSCG for a while now.
I wrote a Javascript based Code to HTML converter, which has but a subset of your functionality. Basically you can throw any type of code into the first box, and you will get XHTML for it in the third box.
http://destraynor.com/c2h.html
'Simon' on Wed, 15 Aug 2007 13:15:52 GMT, sez: You love it! I love the WSCG, and this does improve it one notch more. Well done.
'Andrew Webb' on Wed, 15 Aug 2007 14:06:18 GMT, sez: Also on Javascript:
http://www.codinghorror.com/blog/archives/000857.html
'David Stone' on Wed, 15 Aug 2007 16:05:47 GMT, sez: Javscript 1.7 actually has a foreach:
for(var x in myArray)
{
//yadda yadda yadda
}
Even works in IE 7. :)
'lb' on Wed, 15 Aug 2007 21:05:12 GMT, sez: thanks for the feedback! glad to know the WSCG is still in use (i don't watch its traffic or usage... i used to but, gave up a while back)
I use it for all sorts of things myself.
@Des -- hey i've tinkered with that tool of yours. Some things I'd ask for, 1: a link on that page that tells us where to give feedback. 2: personally i'd like it if leading spaces get converted to non-breaking spaces. 3: I'm pretty wary of using the 'pre' tag as it stops a long line from wrapping which can destroy page layout/templates etc. 4: colour coding!! come on des! you can do it... only have to infer the language, and parse for all known operators/keywords/comment types etc. 5: compile the entered code and highlight any errors. 6: okay, number 5 went too far.
@David -- ah thanks!!
'Raj Chaudhuri' on Thu, 16 Aug 2007 06:36:57 GMT, sez: You didn't mention wanting to do a CMD.EXE version, so I did one.
http://rajch.spaces.live.com/
|