json to csv download in the browser

While using a javascript based datagrid that was displaying json data provided to a page, I thought it smart to not have to re-download all the data again just to make export a csv file.

My solution was to use the “Papa Parse” library to create the CSV file and use URL.createObjectURL() to actually make local data appear as a file which can be downloaded.

I also tried directly setting base64 encoded data to the window like this solution but it choked on my 6-10MB report results.

jQuery(document).ready(function() {
 //My json formatted data from php.
 var dataSet = <?=json_encode($data)?>;
 //select our link, create an objectURL Blob of CSV data.
 jQuery('#downloadCsv').attr('href',URL.createObjectURL(new Blob([Papa.unparse(dataSet)], { type:"text/csv" } ) ) );

and the html for the link:

<a id="downloadCsv" class="button button-secondary" download="reportdata.csv">Download CSV</a>


Error: Cask ‘vagrant’ definition is invalid: Bad header line: parse failed

You need to update vagrant to get some work done and trying to use brew to install vagrant is failing with an error message of "Error: Cask 'vagrant' definition is invalid: Bad header line: parse failed".


You probably haven't updated brew in a really long time. Here's how to get that working again.

brew update; brew cleanup; brew cask cleanup
brew uninstall --force brew-cask; brew update

Thanks to adidalal of the caskroom project for this solution.


Cast to int is the fastest way to sanitize integer input

PHP plays fast and loose with its data types which is one of the features that folks coming from more structured languages find very off-putting.

In phpland it's also the fastest way to sanitize an input which you expect to be an integer.

<?php $var = (int) $var;?>

The result is an integer. If the value of value of var is a number, it's converted into an integer. If it's not a number or doesn't begin with a number, then it's given a value of 0.

Here's a quick test script to illustrate the point. It tests both success and failure conditions.

And the results of this test when run with php 5.5?

php ./test.php 
Time to complete 1000000 (int) conversion filters:0.46194696426392
Time to complete 1000000 int_val filters:0.72814893722534
Time to complete 1000000 preg_replace filters:6.5657861232758
Time to complete 1000000 filter_var(x,FILTER_SANITIZE_NUMBER_INT) filters:1.5239408016205


The test does a million repetitions of numeric values and not-numeric values. The result being type cast is almost twice as fast as intval(), 3.7 times faster than filter_var() and 18x faster than preg_replace(). If we had not done this in a tight loop the results wouldn't have been so dramatic but you get the idea.

 That's something to keep in the toolbox the next time you encounter an integer that needs to be filtered.

Laggy Nexus 7 after KitKat Update? Do a factory reset.

Oh KitKat, I really wanted to like you. You took my perfectly functional Nexus7 and its silky smooth menus and app switching and wrecked it. Now I have a whole lot of hurry up and wait. Wait. WAIT!

downloadI haven't been this frustrated with an operating system since Windows 95.
Well, it appears the the solution is really simple: Wipe and Reinstall.


If you've been backing up your device into the cloud, google will automatically reinstall all your apps, contacts etc when you login. My guess is my device was really full when KitKat installed and really needed more space than I had.

If you're a nexus 7 user about to throw away your tablet, give this a try first.