Tag: javascript

JSCodeRetreat

Last saturday I took part in the JSCodeRetreat, it has been an awesome experience thanks to Uxebu and Mayflower since I still had the code on my laptop I decided to split it in the commits as it was developed and finish it. It is available on Github now and if you are interested about the process just browse the commits.

And again jQuery Slider

Trying to make some slider plugins for jQuery work with a HTML list didn’t prove to be as simple as expected I started working on a simple slider to work with all HTML elements. So here it is in a first working version to be improved and worked on in the futur as need arises.

Clean configuration defaults via Object.create in Node.js

While recently coming back to clean up some Node.js module
code I was looking for a nicer way to handle configuration of an object besides
doing something like this to handle both configuration options and defaults.

var MyModule = function(opts){
  this.foo = opts.foo || 1;
  this.bar = opts.bar || 2;
  // the list goes on
}

In my opinion this code is not really readable, and defaults are not as
obvious as I would like them to be. So I decided to let myself get inspired from
jQuery which provides a nice way to handle settings and defaults in plugins, via
$.extend(defaults, options) so my first
Idea was to simple create an extend function in node, but since I would have to
extend Object to make this as clean as possible I was looking for a way to
handle this natively, and this is where
Object.create
comes in handy.

Object.create allows creating of an object based on some prototype as well as
some default values, which is exactly what I was looking for. Do to the way
Object.create handles properties the options need to be prepared before merging
them in the configuration, which is finally bound to this.

var MyModule = function(opts){
  // some default values 
  var defaults = { foo: 1, bar: 2 };

  // prepare the options for Object.create
  var options = {};
  for(var i in opts){
    options[i] = { 
      value: opts[i], 
      enumerable: true, 
      writeable: true, 
      configurable: true } 
    };
  }

  // let Object.create merge the options with the defaults
  var config = Object.create(defaults, options);

  // bind to this
  for(var o in config){
    this[o] = config[o];
  }
}

Any suggestions for a different cleaner handling of configuration options are
highly welcome.

EDIT: Fixed extra level of nesting in options thanks to Sami Samhuri

Networking with NodeJS @munichjs

Yesterday I did a quick talk about ‘Lower’ level networking applications with
nodejs at munichjs. Lower in this
case means sending binary Data via a UDP/TCP socket, handle DNS resolving and
such. Interested? Talk a look at the slides

Examples can be found on github like Nodrrr to use growl from nodejs, the DNS channel patch for node and also even though it is alpha an will be broken from time to time the RDNS resolver

Growling from Node … or why is Array.pack missing from Javascript?

Since it came out I’ve been really interessed in Node. So I decided to do something with it, and to get me started I hacked together a little module to allow me to do what every good programming environment should allow: Sent me some Growl notifications :D…

Nodejs growl.png

This involved some rather low level UDP packet construction, but for now it works. Maybe somebody will find it useful, I put the code up on Github.

While writing the module I realized I was missing one piece coming from network coding using ruby, Array.pack(format) why is this missing? Is there an alternative? How do I turn an Array into a buffer following some custom format? Well for now I solved this by a little hacked function extending Array, this is neither clean nor well tested but it works for me while looking for a way to this right. For anyone interested here it is, and don’t kill me cause it’s ugly

Enjoy.

For my own sake … Promotejs

JS Array .concat

Good Javascript docu is out there, just need it easily findable! And for my own coding sanity sake it needs to be #1 on google!