print_r() for javascript

I typically just use uneval() to figure out what’s inside an array/object, but what about when it’s large and heterogeneous? I wanted to find a version of php’s print_r() for JavaScript. Here is link to the Original Version of dump() I based mine off of:

www.openjs.com/scripts/others/dump_function_php_print_r.php

When I tried it, the first thing I noticed what that it put quotes around everything, and that long strings with line breaks got messy, so I did a quick adaptation to suit my immediate needs and came up with this:

function dump(arr,level) {
  function magicquotes(value) { return (isNaN(value)) ? '"' + value.replace(/\n/g,"\n"+indent) + '"' : value; }
  level = level | 0;
  var indent = new Array(level+1).join("\t"), dumped_text = "";

  if(typeof(arr) == 'object') { //Array/Hashes/Objects
    for(var item in arr) {
      var value = arr[item];

      if(typeof(value) == 'object')
          dumped_text += indent + "'" + item + "' :\n" + dump(value,level+1);
      else
          dumped_text += indent + "'" + item + "' => " + magicquotes(value) + "\n";
    }
  } else { //Stings/Chars/Numbers etc.
    dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  }
  return dumped_text;
}

I also found links to many other print_r(), var_dump() equivalents, but they either depended on write, were overly complicated, or returned a lot of excess text I wasn’t interested in.

So why not just call it print_r ? Well, the original I copied was called dump, and I’ve always been annoyed typing that underscore, so I just didn’t change it.

Update: found another alternative. It’s really long and puts out a lot of extraneous information, but it’s worth looking at: wiki.greasespot.net/Code_snippets#Dump_the_properties_of_an_object

July 28th, 2009 | Finish Writing Me Plz, Nerd | No comments

Secret Stash script

I started writing my “Secret Stash” php script as a simple DirectoryIndex skin and evolved it to a full-featured entity of its own. My client needed an aesthetic way of showing their assets. Previously, they would either send them to an open directory on their web server, or write a new HTML page every time they needed something more. Even though they had their workflow down to a series of copy and pastes, it was a lot of work every time there was a change. Especially compared to the system I gave them.

What I did was create a php script that created a parallel version of the open directory. The script used the same CSS and design cues from the main site, scraped the open directory and its subdirectories, and presented the clients files in a context-appropriate way. Subdirectories were grouped together, there was a widget to play audio files from the browser, videos could be played in-line, and images were thumbnailed and loaded in a lightbox. And the best part is that just maintaining the files was all it took to create the corresponding client site.

I added more features beyond that– like simple file maintenance through the web interface, and the ability to add comments to each file.

The script was flexible enough to be used for different kind of clients, significantly reduced the maintenance time, and also made it possible for anyone with an FTP client to create client sites.

July 21st, 2009 | Case Study, Portfolio | No comments