jQuery dataTables formatted numbers and currency sorting plugins

0

I recently needed to add some sorting functionality to a few dataTables.  While I was grateful for plentiful example plugins to work with, I found them a bit rough around the edges.  Here are a couple of plugins that I’ve refined a bit for my purposes.

Formatted Numbers Sorting

This plugin originally written by Peter Gallagher and can be found on the dataTables sorting plugins page.  It had no comments and was using the eval statement (test performance variation with & without eval for yourself on jsperf).  I fixed both and then ran it through JSLint.

/*globals $ */
$.fn.dataTableExt.oSort['formatted-num-asc'] = function (x, y) {
	'use strict';

	// Define vars
	var a = [], b = [];

	// Match any character except: digits (0-9), dash (-), period (.), or backslash (/) and replace those characters with empty string.
	x = x.replace(/[^\d\-\.\/]/g, '');
	y = y.replace(/[^\d\-\.\/]/g, '');

	// Handle simple fractions
	if (x.indexOf('/') >= 0) {
		a = x.split("/");
		x = parseInt(a[0], 10) / parseInt(a[1], 10);
	}
	if (y.indexOf('/') >= 0) {
		b = y.split("/");
		y = parseInt(b[0], 10) / parseInt(b[1], 10);
	}

	return x - y;
};
$.fn.dataTableExt.oSort['formatted-num-desc'] = function (x, y) {
	'use strict';

	// Define vars
	var a = [], b = [];

	// Match any character except: digits (0-9), dash (-), period (.), or backslash (/) and replace those characters with empty string.
	x = x.replace(/[^\d\-\.\/]/g, '');
	y = y.replace(/[^\d\-\.\/]/g, '');

	// Handle simple fractions
	if (x.indexOf('/') >= 0) {
		a = x.split("/");
		x = parseInt(a[0], 10) / parseInt(a[1], 10);
	}
	if (y.indexOf('/') >= 0) {
		b = y.split("/");
		y = parseInt(b[0], 10) / parseInt(b[1], 10);
	}

	return y - x;
};

Check it out on github.

Currency Sorting

This plugin originally written by Allen Jardine and can also be found on the dataTables sorting plugins page.  It was good and well commented, but failed when the data was empty or was unexpected.  I extended it to anticipate empty values, any form of NA (eg, na, n/a, NA, N/A) and either single or double dashes (eg. -, –).  I also extended it to handle numbers without a leading currency symbol.  Of course it’s also run through JSLint

/*globals $ */
/*globals $ */
/*globals $ */
$.fn.dataTableExt.oSort['currency-asc'] = function (a, b) {
	'use strict';

	var x, y;

	/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
	x = (a === "-" || a === "--" || a === '' || a.toLowerCase().replace('/', '') === 'na') ? -1 : a.replace(/,/g, "");
	y = (b === "-" || b === "--" || b === '' || b.toLowerCase().replace('/', '') === 'na') ? -1 : b.replace(/,/g, "");

	/* Remove the currency sign */
	if (typeof x === "string" && isNaN(x.substr(0, 1), 10)) {
		x = x.substring(1);
	}
	if (typeof y === "string" && isNaN(y.substr(0, 1), 10)) {
		y = y.substring(1);
	}

	/* Parse and return */
	x = parseFloat(x, 10);
	y = parseFloat(y, 10);

	return x - y;
};
$.fn.dataTableExt.oSort['currency-desc'] = function (a, b) {
	'use strict';

	var x, y;

	/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
	x = (a === "-" || a === "--" || a === '' || a.toLowerCase().replace('/', '') === 'na') ? -1 : a.replace(/,/g, "");
	y = (b === "-" || b === "--" || b === '' || b.toLowerCase().replace('/', '') === 'na') ? -1 : b.replace(/,/g, "");

	/* Remove the currency sign */
	if (typeof x === "string" && isNaN(x.substr(0, 1), 10)) {
		x = x.substring(1);
	}
	if (typeof y === "string" && isNaN(y.substr(0, 1), 10)) {
		y = y.substring(1);
	}

	/* Parse and return */
	x = parseFloat(x, 10);
	y = parseFloat(y, 10);

	return y - x;
};

Check it out on github.

Screen Shot 2011-09-11 at 3.48.57 PM

How to Test Web Development Environment in VMWare Fusion

0

As a Mac user, testing and debugging IE can be a hassle.  The best way that I have found to test my work in IE is setting up a Windows virtual machine.  To view your Mac OSX’s localhost inside of your VMWare client you’ll need to point your Windows hosts file (located at: C:\WINDOWS\system32\drivers\etc\hosts) to your localhost.  You say that 127.0.0.1 doesn’t work?  That’s because VMWare sets up a proxy IP for localhost.

To find the proxy IP address open the Terminal.app and type:

ifconfig vmnet1

This will return something like:

In this case, 172.16.134.1 is your proxy IP.  Just enter that into your Windows hosts file and kill VMWare’s DNS cache.  Clear VMWare’s DNS cache go to Start > Run.  Type ‘cmd’ and press return.  Then at the MS-DOS prompt type:

ipconfig /flushdns

This post was adapted for my own archival purposes from these great posts, which you might find useful:

Screen Shot 2011-08-29 at 11.54.42 AM

dataTables.js: oSettings is null

3

Introduction

jQuery plugin dataTables.js is a great tool for quickly adding sortable, searchable data to your site.  The documentation on their site is great but sometimes doesn’t account for the miriad of ways that I’ve managed to break it.  When I do something wrong, I forget how I fixed it the last time.  Which makes me have to re-learn what went wrong.  So I’m writing this post in the hopes that I won’t have to remember how I fixed this problem the next time.

How to fix it

The error is “a is null” when minified.  Only after replacing the minified version of the dataTables.js file with the non-minified version do you get “oSettings is null”.  Just what exactly is oSettings?  It’s an object literal containing a number of attributes and methods that is passed to most of the internal functions of dataTables.js.  When it is null, it throws the alert message displayed to the right.

Unfortunately, since oSettings is such a common function parameter name knowing its name isn’t very helpful.  You need to find the line number that’s causing the problem.  The last time this happened to me clicking around on the page after receiving the oSettings is null” caused a console.log error message to be thrown that indicated what line the error is on.  If anyone has a better idea for finding the line of the error, please let me know.

Once you know what method your error is a part of, in my case _fnAdjustColumnSizing, then you’re halfway there.  This, combined with the knowledge of what functionality is being used when the alert is thrown, should lead you right to the culprit.  In my case: it was a callback method that was improperly assigned.  Changing my if conditional to accept both undefined and empty string when assigning a certain value as opposed to only an undefined value fixed my problem.

Dropbox

Finding a More Secure Dropbox Replacement

0

Recently, there have been a number of security related concerns raised about Dropbox.  The most damning is that the private key used for encryption is held by the company.  This led me to question what options were available to store more sensitive data in the cloud.  Fortunately, there are almost too many options to choose from.

Here are some of the best options that I was able to find:

There are a number of pro & con blog posts comparing many of these services already so I’ll spare you.

One of the more interesting solutions that I’ve seen suggested, if also somewhat more complicated, is building your own Dropbox replacement.  Admittedly, this solution isn’t quite a full Dropbox replacement, but still a possible replacement nonetheless.

In the end, I ended up choosing Jungle Disk because of its status as an industry leader (so they’re liable to be around for a while), ease of use, and integration with RackSpace (an ISP I’m already familiar with).

To convert from Dropbox to Jungle Disk:

  1. First, create a new folder in your home directory to serve as the root directory for Jungle Disk.
  2. Copy the sensitive data from your Dropbox folder to your newly created Jungle Disk folder.
  3. Change any old symlinks to point at Jungle Disk.
  4. Confirm that all of the data is accessible by opening it as you normally would.
  5. Finally, delete your data from Dropbox so that its not accessible from there anymore.
Screen Shot 2011-08-29 at 10.47.19 AM

Verizon FiOS Speedtest.net Results

2

A while back I wrote a post detailing my transition from Time Warner’s standard package to their top of the line wideband service. The post detailed the level of experience one might be able to expect with each service. Last month I moved down the street and now I have the Verizon FiOS ultimate HD package which includes 35MBs down & 35 MBs up internet service so I thought that I’d take a moment to update the blog with some test data for comparison.

Without further ado…

Ultimate HD TV/Internet Combo ($140/month)

Speedtest.net Results

Update 5/20/2011:

Speedtest.net Results

I’ll be sure to update this post a couple more times over the next few weeks to give a larger sample size.

Screen Shot 2011-08-29 at 10.51.28 AM

My First Video (Canon 7D & iMovie)

0

I filmed this at my friend Jerry Luna’s birthday about 3 weeks ago.  Yesterday morning I finally decided to piece something together and put it online.  It was a lot of fun!  I’ve got to do this more often.

The hardest part was holding the camera steady and keeping everything in focus.  I feel like I did a pretty good job of both considering I didn’t have any sort of rig or proper focus mechanism.

For those that are interested, I used my Canon 7D and a 50mm f/1.4 lens.  There is no audio because, among other things, the onboard microphone is terrible.  In the future, I may be acquiring a few accessories for capturing better audio.

The Dog-pocolypse is Nigh!

The Dog-pocolypse is Nigh!

0

In a small town in Tennessee, cats have learned to walk on their hind legs and ride dogs on their backs.

SquareUp.com's Credit Card Swiping iPhone Attachment

How to Fix SquareUp.com’s Device on iPhone 4

0

SquareUp.com's Credit Card Swiping iPhone Attachment

The SquareUp.com mobile payment system is better than nothing, very cool, but falls short of perfect.

Intro

Square, Inc.’s mobile payment system is very cool but not perfect.

After months of waiting and anticipation my Square, Inc.  accessory finally arrived in the mail a few days ago.  For those who haven’t heard about this device, let me fill you in.  Square is a startup that makes mobile apps and accessories allowing anyone to process credit or debit cards from anywhere.  Sounds cool right?  Watch the intro video.

My initial reaction was that of excitement.  Though, I’d nearly given up hope on the thing after months of delay when at last I received an email a few weeks ago preluding its immanent arrival.  This reaction however was tempered when it didn’t work as expected right away.

The Problem

Apparently there is a known design flaw affecting iPhone 4 owners.  Specifically, the stainless steel band of the iPhone shorts the ground of the accessory’s connector’s base.

The Solutions

The quick and easy fix is to simply cut a small hole in a piece of paper (I used wax paper) and slide it onto the connector (see photo).  Other suggested solutions include using a small rubber (10 gauge) body jewelry o-ring and masking the connector before using white out to paint over the trouble area.

My Thoughts

Square was most likely caught off guard when the iPhone 4′s design was revealed.  I’m guessing that they had passed the point of no return on the unit’s design.  Obviously, this is cutting edge technology and there will be kinks that need to be worked out.  However, the coolness factor of collecting payments from anywhere far outweighs all of these setbacks.  After all, once fixed, it does work fairly well.

Update

UPDATE 10-16-2010: I’ve swapped out the small piece of paper for a 10 gauge rubber o-ring and must say that it is a far superior and perhaps even permanent solution.  The o-ring looks like it an OEM part.  In fact, I think Square should just include one of these with each unit and there would be no problem at all

UPDATE 11-2-2010: Square mailed me a new version of the accessory that replaces the steel bit at the base of the connector with a plastic bit.  It works perfectly out of the box.  Anyone signing up with Square after today will be getting the new version making this blog post obsolete going forward.

Good Riddance BlueHost; VPS Link What Happened?

2

Over the past several months or so, I’ve been consolidating the hosting for my websites to a single vendor.  Considering that I’ve been a web developer for over a decade, this probably should have happened a long time ago.

When I started this project, I had two separate neglected shared hosting accounts at BlueHost and then my main virtual private server at VPS Link.  It finally occurred to me that I could just consolidate all of them onto my VPS Link host and save myself approximately $18 a month.  Not to mention the fact that BlueHost is a terrible company to deal with.  For instance, they won’t give you ssh access to your shared host unless you FAX them a photo id.  The pure idiocy of this should have sent me running for the hills a long time ago. (more…)

Disk Utility Screenshot

24″ iMac Hard Drive Replacement is Super Easy

0

I’d like to take a moment to thank Apple for making it so very easy to upgrade the hard drive in my iMac.  Specifically, Migration Assistant is a beautiful application.  I simply put my old internal hard drive in an enclosure with a firewire 800 port and filled out few simple screens.  Then it copied over all of my applications and settings.  I was shocked that it wasn’t more painful.

Macally Hi-Speed eSATA/FireWire/USB 2.0 Storage Enclosure for 3.5-Inch SATA Hard Disk G-S350SUAB2

The Macally hard drive enclosure with my old drive looks great on my desk and the FireWire 800 is blazing fast!

There are already several great tutorials and how-to’s about disassembling the iMac and physically installing the drive.  The only thing I had a problem with durring the entire hard drive replacement/upgrade and the only thing that wasn’t covered in any of those guides was initializing the drive prior to installing Snow Leopard again.  It turns out that the drive need to be formatted before it would show up in the installation wizard. (more…)

Go to Top