3 ms·
For those who wish to see the non-minified source: The code has not been minified, except perhaps for the replacement of the (carriage return+indent) by semi-c
by pygy_ 14y ago
For those who wish to see the non-minified source:
The code has not been minified, except perhaps for the replacement of the (carriage return+indent) by semi-columns. Most variable names are semantic.
For sure, the 16 LoC thing is misleading, but the whole thing weights around 3.5 KB, which is impressive for the amount of functionality packed in it.
Let's take the first two lines:
[J,M,duc,euc,n,e,w]=[JSON,Math,decodeURIComponent,encodeURIComponent,((p)->p.replace(/\/*$/,'').replace /^\/?/,'/'),((d,s)->d[k]=(if d[k]instanceof Array then d[k].concat v else v)for k,v of s;d),(s,c)->s.split(/\s+/).map c]
[http,url,qs,fs,async,WS,E,haml,ejs,mime]=w 'http url querystring fs async faye-websocket vault/node/aes haml ejs mime',require
For iOS readers, you can scroll the code box using two fingers.
So:
[J,M,duc,euc]=[JSON,Math,decodeURIComponent,encodeURIComponent]
is quite obvious. So is the second line except perhaps for the use of the w function. It splits the input string on spaces in an array and maps the callback passed as second argument (in this case, require, if you didn't scroll til the end of the line). We'll come to its meaning later.
n = (p)->p.replace(/\/*$/,'').replace /^\/?/,'/'
# normalize?(path??) not sure about his one.
# p is also used elsewhere as a generic name.
e = (d,s)->d[k]=(if d[k]instanceof Array then d[k].concat v else v)for k,v of s;d)
# extend(destination, source)
# k and v are obviously key and value.
w = (s,c)->s.split(/\s+/).map c]
# w(string, callback)
In Ruby, w%foo bar baz% is a shortcut for creating an array of strings ["foo","bar","baz"]. The w method here is enhanced version of this, and a nod to _why, who often used it to require a bunch of libraries at the top of his scripts. See for example https://github.com/camping/camping/blob/ddea5760289a4de3c27047c17475fd1165256664/lib/camping-unabridged.rb#L31 https://github.com/camping/camping/blob/ddea5760289a4de3c270...
For those who don't know about him, _why is the author of Camping, the grand-father of golf Web microframeworks. The current version doesn't use the w trick anymore because the reduction of dependencies made the normal method shorter. Here's the code (possibly NSFL for some...):
https://github.com/camping/camping/blob/master/lib/camping.rb https://github.com/camping/camping/blob/master/lib/camping.r...
and its heavily commented (but as terse, codewise) version:
https://github.com/camping/camping/blob/master/lib/camping-unabridged.rb https://github.com/camping/camping/blob/master/lib/camping-u...
Camping used to be the go to ruby microframework until _why's disappearance. Then dev stalled for a while and Sinatra took its place. It has since been revamped and is actively maintained.
Neither Camping nor Berliner are meant for professional use, obviously.
- pooriaazimi 14y agoI'm sorry, but I don't get this part: something.replace /^\/?/,'/' I'm a little sleepy, so maybe I'm missing something, but doesn't it replace a possible '/' at the beginning of some lines with '/'? Isn't that just a waste of CPU cycles? It effectively does nothing.
- pygy_ 14y agoThe key is the question mark. The regexp also adds a slash if none is present initially.
- ajanuary 14y ago> For iOS readers, you can scroll the code box using two fingers. You've been able to do it with a single finger for a little while, which makes it a lot easier.
- pygy_ 14y agoThanks, I didn't get the memo since I'm still using an iPhone 3G.
- lazerwalker 14y agoIs there a good reason for defining 'J' and 'M' variables instead of just calling JSON and Math directly? It seems to me like that's a poor trade-off of readability for a few measly characters, but I'd love to hear an argument for it.
- draegtun 14y ago>Camping used to be the go to ruby microframework until _why's disappearance. Then dev stalled for a while and Sinatra took its place. It has since been revamped and is actively maintained. I don't recall there being any stalling when _why left? Camping was already a community entrenched project well before _why disappeared. > Neither Camping nor Berliner are meant for professional use, obviously. I think the Camping developers & users would disagree. Here's an HN Post which highlights why they think Camping is better designed than Sinatra: http://news.ycombinator.com/item?id=1532178 http://news.ycombinator.com/item?id=1532178