September 2010
2 posts
2 tags
Asynchronous messaging
JavaScript runs in a single thread inside browsers. When the thread is busy, asynchronous events like user actions or XHR responses get queued up and only get processed when the currently running JavaScript execution thread becomes idle. Suppose you want to fire off an event but don’t want the code for handling that event to be run immediately: if (!this.fmt.test(this.value)) { ...
Sep 18th
2 tags
Know when it is no longer JSON
Do you ever call your variables that get populated from a JSON string something with “json” in them? function processData(json) { // if (json.accountType == "savings") ... } Realize that “JSON” only describes the format of the data you’re fetching through XHR or <script>. It is a string representation of an object. When you’re in the JavaScript...
Sep 8th
July 2010
3 posts
3 tags
Rounding with significant figures
A function for rounding a decimal number to a given number of significant figures (or digits): function sigFigs(n, sig) { var mult = Math.pow(10, sig - Math.floor(Math.log(n) / Math.LN10) - 1); return Math.round(n * mult) / mult; } alert(sigFigs(1234567, 3)); // Gives 1230000 alert(sigFigs(0.06805, 3)); // Gives 0.0681 alert(sigFigs(5, 3)); // Gives 5
Jul 9th
3 tags
Parsing query string parameters into a collection
Here’s a utility function for parsing query parameters: function getQueryParams(qs) { qs = qs.split("+").join(" "); var params = {}; var tokens, re = /[?&]?([^=]+)=([^&]*)/g; while (tokens = re.exec(qs)) { params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]); } return params; } //var query =...
Jul 9th
3 tags
Parsing ISO 8601 UTC dates
Here’s a concise implementation for parsing UTC date strings in ISO 8601 format into Date objects in the local time zone. function parseIsoDate(s) { var tokens = /(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d+)/ .exec(s).slice(1); tokens[1] -= 1; return new Date(Date.UTC.apply(null, tokens)); } The new Date(Date.UTC.apply(null, tokens)) trick allows the direct usage...
Jul 9th
June 2010
4 posts
3 tags
String formatter
Here’s a basic string formatter that takes in a format string and a variable number of arguments: // Format string may contain variable references in the form "{i}" // where i is the variable index function format(fmt) { return fmt.replace(/\{(\d+)\}/g, function (s, i) { return arguments[i]; }); } // Usage: var usercnt = 45, groupcnt = 3; format("Found {1} users and {2}...
Jun 30th
3 tags
Reversing a string
JavaScript doesn’t have a reverse() method for strings but has one for arrays. The trick is to convert a string to an array, reverse the array and combine the array back into a string: var s = "Hello World!"; var reversed = s.split("").reverse().join(""); // "!dlroW olleH"
Jun 30th
3 tags
Appending an array of items to an array
Suppose you already have some items in an array and you want to append those items to a target array. You can use the concat method to create a new array that is the concatenation of the two arrays and replace the target array with the result: var items = [ 3, 5, 7 ]; target = target.concat(items); // new instance! You can avoid replacing the array with a new instance every time you need to...
Jun 29th
3 tags
Properly clearing properties
If you want to clear a property of an object, simply setting it to undefined won’t always cut it, based on how you will access/test the property later on: // The setup var obj = { prop: 1 }; // Clear property value obj.prop = undefined; The property is still attached to the object as a property and can be enumerated: for (var key in obj) { // Will iterate through "prop" } typeof...
Jun 29th