Compare objects recursively in Javascript
October 9th, 2009
This is a need I've run into more than once, yet there doesn't seem to be an elegant off-the-shelf solution that can handle a wide range of use cases, including simple (flat) objects, linear (one-dimensional) arrays, arrays of objects, and complex objects (objects within objects).
So here's a [relatively] straightforward recursive object comparison function that features optional loose type comparison (strict by default) for those times when you're working with data from different sources, (e.g., which give you quoted numbers ("3" == 3) or empty strings ("" == null == undefined == 0 == "0")), but your task at hand doesn't distinguish among these semantic differences.
Read the rest of this entry »
View Rendered Source in Internet Explorer
April 20th, 2009
IE 6 and 7 can be a nightmare for debugging Javascript, especially after working with the quality tools available for other browsers such as Firebug for Firefox. However, one small step in the right direction would be the ability to View Source for the currently rendered page, after Javascript has been executed (since typically the View Source command displays the source code as it was sent by the server, prior to any script execution).
Read the rest of this entry »
Stable quicksort in Javascript
October 28th, 2008
Firefox < v3.0 doesn't have a stable Array.sort() function - that is, it doesn't maintain indexes for elements of equal value. This is undefined in the ECMA spec, and has been fixed in Firefox as of version 3 (and curiously enough has been stable in IE all along). As a result, I set out to find a stable, efficient Array.sort() replacement/supplement.
Read the rest of this entry »
MySQL inner join to perform update from same table
September 9th, 2008
Typically, using SELECT in a subquery to perform an UPDATE on the same table, such as:
UPDATE table1 AS target
SET field1 = (
SELECT field2
FROM table1 AS source
WHERE source.id = target.id
)
is illegal in MySQL. To achieve the desired effect, you can perform an INNER JOIN in the UPDATE:
UPDATE table1 AS target INNER JOIN table1 AS source USING(id) SET target.field1 = source.field2